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.context.annotation.Scope;
|
6
|
import org.springframework.stereotype.Service;
|
7
|
|
8
|
import javax.mail.*;
|
9
|
import javax.mail.internet.AddressException;
|
10
|
import javax.mail.internet.InternetAddress;
|
11
|
import javax.mail.internet.MimeMessage;
|
12
|
import java.io.IOException;
|
13
|
import java.util.Properties;
|
14
|
|
15
|
/**
|
16
|
* Created by antleb on 3/9/15.
|
17
|
*/
|
18
|
@Service("mailLibrary")
|
19
|
public class MailLibrary {
|
20
|
|
21
|
@Value("${goldoa.mail.host}")
|
22
|
private String mailhost;
|
23
|
@Value("${goldoa.mail.from}")
|
24
|
private String from ;
|
25
|
@Value("${goldoa.mail.replyTo}")
|
26
|
private String replyTo;
|
27
|
@Value("${goldoa.mail.port}")
|
28
|
private String smtpPort;
|
29
|
@Value("${goldoa.mail.username}")
|
30
|
private String username;
|
31
|
@Value("${goldoa.mail.password}")
|
32
|
private String password;
|
33
|
@Value("${goldoa.mail.authenticate}")
|
34
|
private String authenticate ;
|
35
|
@Value("${goldoa.mail.mode}")
|
36
|
private String mode ;
|
37
|
@Value("${goldoa.mail.debug}")
|
38
|
private String debug ;
|
39
|
|
40
|
private Properties props ;
|
41
|
|
42
|
private Log log = LogFactory.getLog(MailLibrary.class);
|
43
|
|
44
|
@PostConstruct
|
45
|
public void init() {
|
46
|
props = new Properties();
|
47
|
|
48
|
log.debug("mode : " + this.mode);
|
49
|
log.debug("mailhost : " + this.mailhost);
|
50
|
log.debug("from : " + this.from);
|
51
|
log.debug("replyto : " + this.replyTo);
|
52
|
log.debug("smtpPort : " + this.smtpPort);
|
53
|
log.debug("username : " + this.username);
|
54
|
log.debug("password : " + this.password);
|
55
|
log.debug("authenticate : " + this.authenticate);
|
56
|
log.debug("debug : " + this.debug);
|
57
|
|
58
|
if (mode.equals("plain")) {
|
59
|
props.put("mail.transport.protocol", "smtp");
|
60
|
|
61
|
props.put("mail.smtp.host", mailhost);
|
62
|
props.put("mail.smtp.port", smtpPort);
|
63
|
} else if (mode.equals("ssl")) {
|
64
|
props.put("mail.transport.protocol", "smtps");
|
65
|
|
66
|
props.put("mail.smtps.host", mailhost);
|
67
|
props.put("mail.smtps.port", smtpPort);
|
68
|
props.put("mail.smtps.auth", "true");
|
69
|
props.put("mail.smtps.user", username);
|
70
|
props.put("mail.smtps.password", password);
|
71
|
} else {
|
72
|
throw new IllegalArgumentException("Unkown mode '" + mode + "'");
|
73
|
}
|
74
|
|
75
|
props.setProperty("mail.debug", this.debug + "");
|
76
|
|
77
|
}
|
78
|
|
79
|
public void sendEmail(String email, String subject, String text) throws AddressException, MessagingException {
|
80
|
sendEmail(new String[]{email}, subject, text);
|
81
|
}
|
82
|
|
83
|
public void sendEmail(String[] recipients, String subject, String text) throws AddressException, MessagingException {
|
84
|
|
85
|
Session session = Session.getInstance(props,
|
86
|
(Boolean.parseBoolean(this.authenticate) ? new SMTPAuthenticator() : null));
|
87
|
|
88
|
session.setDebug(Boolean.parseBoolean(this.debug));
|
89
|
|
90
|
Message msg = new MimeMessage(session);
|
91
|
|
92
|
msg.setFrom(new InternetAddress(this.from));
|
93
|
msg.setReplyTo(new InternetAddress[]{new InternetAddress(replyTo)});
|
94
|
msg.setSubject(subject);
|
95
|
msg.setContent(text, "text/plain");
|
96
|
|
97
|
|
98
|
for (String recipient : recipients)
|
99
|
msg.addRecipient(javax.mail.Message.RecipientType.TO,
|
100
|
new InternetAddress(recipient));
|
101
|
|
102
|
// Transport tr = session.getTransport();
|
103
|
//
|
104
|
// if (Boolean.parseBoolean(this.authenticate)) {
|
105
|
// tr.connect(mailhost, Integer.parseInt(smtpPort), username, password);
|
106
|
// } else {
|
107
|
// tr.connect();
|
108
|
// }
|
109
|
// try {
|
110
|
//
|
111
|
// System.out.println(msg.getContent().toString());
|
112
|
// } catch (IOException e) {
|
113
|
// e.printStackTrace();
|
114
|
// }
|
115
|
// tr.sendMessage(msg, msg.getAllRecipients());
|
116
|
// tr.close();
|
117
|
}
|
118
|
|
119
|
private class SMTPAuthenticator extends javax.mail.Authenticator {
|
120
|
public PasswordAuthentication getPasswordAuthentication() {
|
121
|
return new PasswordAuthentication(username, password);
|
122
|
}
|
123
|
}
|
124
|
|
125
|
public String getMailhost() {
|
126
|
return mailhost;
|
127
|
}
|
128
|
|
129
|
public void setMailhost(String mailhost) {
|
130
|
this.mailhost = mailhost;
|
131
|
}
|
132
|
|
133
|
public String getFrom() {
|
134
|
return from;
|
135
|
}
|
136
|
|
137
|
public void setFrom(String from) {
|
138
|
this.from = from;
|
139
|
}
|
140
|
|
141
|
public String getReplyTo() {
|
142
|
return replyTo;
|
143
|
}
|
144
|
|
145
|
public void setReplyTo(String replyTo) {
|
146
|
this.replyTo = replyTo;
|
147
|
}
|
148
|
|
149
|
public String getSmtpPort() {
|
150
|
return smtpPort;
|
151
|
}
|
152
|
|
153
|
public void setSmtpPort(String smtpPort) {
|
154
|
this.smtpPort = smtpPort;
|
155
|
}
|
156
|
|
157
|
public String getUsername() {
|
158
|
return username;
|
159
|
}
|
160
|
|
161
|
public void setUsername(String username) {
|
162
|
this.username = username;
|
163
|
}
|
164
|
|
165
|
public String getPassword() {
|
166
|
return password;
|
167
|
}
|
168
|
|
169
|
public void setPassword(String password) {
|
170
|
this.password = password;
|
171
|
}
|
172
|
|
173
|
public String isAuthenticate() {
|
174
|
return authenticate;
|
175
|
}
|
176
|
|
177
|
public void setAuthenticate(String authenticate) {
|
178
|
this.authenticate = authenticate;
|
179
|
}
|
180
|
|
181
|
public String getMode() {
|
182
|
return mode;
|
183
|
}
|
184
|
|
185
|
public void setMode(String mode) {
|
186
|
this.mode = mode;
|
187
|
}
|
188
|
|
189
|
public String isDebug() {
|
190
|
return debug;
|
191
|
}
|
192
|
|
193
|
public void setDebug(String debug) {
|
194
|
this.debug = debug;
|
195
|
}
|
196
|
}
|