Project

General

Profile

1
package eu.dnetlib.goldoa.service.utils;
2

    
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.stereotype.Service;
7

    
8
import javax.annotation.PostConstruct;
9
import javax.mail.*;
10
import javax.mail.internet.AddressException;
11
import javax.mail.internet.InternetAddress;
12
import javax.mail.internet.MimeMessage;
13
import java.io.IOException;
14
import java.util.Properties;
15

    
16
/**
17
 * Created by antleb on 3/9/15.
18
 */
19
@Service("mailLibrary")
20
public class MailLibrary {
21

    
22
	@Value("${goldoa.mail.host}")
23
	private String mailhost;
24
	@Value("${goldoa.mail.from}")
25
	private String from ;
26
	@Value("${goldoa.mail.replyTo}")
27
	private String replyTo;
28
	@Value("${goldoa.mail.port}")
29
	private String smtpPort;
30
	@Value("${goldoa.mail.username}")
31
	private String username;
32
	@Value("${goldoa.mail.password}")
33
	private String password;
34
	@Value("${goldoa.mail.authenticate}")
35
	private String authenticate ;
36
	@Value("${goldoa.mail.mode}")
37
	private String mode ;
38
	@Value("${goldoa.mail.debug}")
39
	private String debug ;
40

    
41
	private Properties props ;
42

    
43
	private Log log = LogFactory.getLog(MailLibrary.class);
44

    
45
	@PostConstruct
46
	public void init() {
47
		props = new Properties();
48

    
49
		log.debug("mode : "  + this.mode);
50
		log.debug("mailhost : "  + this.mailhost);
51
		log.debug("from : "  + this.from);
52
		log.debug("replyto : "  + this.replyTo);
53
		log.debug("smtpPort : "  + this.smtpPort);
54
		log.debug("username : "  + this.username);
55
		log.debug("password : "  + this.password);
56
		log.debug("authenticate : "  + this.authenticate);
57
		log.debug("debug : "  + this.debug);
58

    
59
		if (mode.equals("plain")) {
60
			props.put("mail.transport.protocol", "smtp");
61

    
62
			props.put("mail.smtp.host", mailhost);
63
			props.put("mail.smtp.port", smtpPort);
64
		} else if (mode.equals("ssl")) {
65
			props.put("mail.transport.protocol", "smtps");
66

    
67
			props.put("mail.smtps.host", mailhost);
68
			props.put("mail.smtps.port", smtpPort);
69
			props.put("mail.smtps.auth", "true");
70
			props.put("mail.smtps.user", username);
71
			props.put("mail.smtps.password", password);
72
		} else {
73
			throw new IllegalArgumentException("Unkown mode '" + mode + "'");
74
		}
75

    
76
		props.setProperty("mail.debug", this.debug + "");
77

    
78
	}
79

    
80
	public void sendEmail(String email, String subject, String text) throws AddressException, MessagingException {
81
		sendEmail(new String[]{email}, subject, text);
82
	}
83

    
84
	public void sendEmail(String[] recipients, String subject, String text) throws AddressException, MessagingException {
85

    
86
		Session session = Session.getInstance(props,
87
				(Boolean.parseBoolean(this.authenticate) ? new SMTPAuthenticator() : null));
88

    
89
		session.setDebug(Boolean.parseBoolean(this.debug));
90

    
91
		Message msg = new MimeMessage(session);
92

    
93
		msg.setFrom(new InternetAddress(this.from));
94
		msg.setReplyTo(new InternetAddress[]{new InternetAddress(replyTo)});
95
		msg.setSubject(subject);
96
		msg.setContent(text, "text/plain");
97

    
98

    
99
		for (String recipient : recipients)
100
			msg.addRecipient(javax.mail.Message.RecipientType.TO,
101
					new InternetAddress(recipient));
102

    
103
		Transport tr = session.getTransport();
104

    
105
		if (Boolean.parseBoolean(this.authenticate)) {
106
			tr.connect(mailhost, Integer.parseInt(smtpPort), username, password);
107
		} else {
108
			tr.connect();
109
		}
110
		try {
111

    
112
			System.out.println(msg.getContent().toString());
113
		} catch (IOException e) {
114
			e.printStackTrace();
115
		}
116
		tr.sendMessage(msg, msg.getAllRecipients());
117
		tr.close();
118
	}
119

    
120
	private class SMTPAuthenticator extends javax.mail.Authenticator {
121
		public PasswordAuthentication getPasswordAuthentication() {
122
			return new PasswordAuthentication(username, password);
123
		}
124
	}
125

    
126
	public String getMailhost() {
127
		return mailhost;
128
	}
129

    
130
	public void setMailhost(String mailhost) {
131
		this.mailhost = mailhost;
132
	}
133

    
134
	public String getFrom() {
135
		return from;
136
	}
137

    
138
	public void setFrom(String from) {
139
		this.from = from;
140
	}
141

    
142
	public String getReplyTo() {
143
		return replyTo;
144
	}
145

    
146
	public void setReplyTo(String replyTo) {
147
		this.replyTo = replyTo;
148
	}
149

    
150
	public String getSmtpPort() {
151
		return smtpPort;
152
	}
153

    
154
	public void setSmtpPort(String smtpPort) {
155
		this.smtpPort = smtpPort;
156
	}
157

    
158
	public String getUsername() {
159
		return username;
160
	}
161

    
162
	public void setUsername(String username) {
163
		this.username = username;
164
	}
165

    
166
	public String getPassword() {
167
		return password;
168
	}
169

    
170
	public void setPassword(String password) {
171
		this.password = password;
172
	}
173

    
174
	public String isAuthenticate() {
175
		return authenticate;
176
	}
177

    
178
	public void setAuthenticate(String authenticate) {
179
		this.authenticate = authenticate;
180
	}
181

    
182
	public String getMode() {
183
		return mode;
184
	}
185

    
186
	public void setMode(String mode) {
187
		this.mode = mode;
188
	}
189

    
190
	public String isDebug() {
191
		return debug;
192
	}
193

    
194
	public void setDebug(String debug) {
195
		this.debug = debug;
196
	}
197
}
(5-5/7)