1
|
package eu.dnetlib.utils;
|
2
|
|
3
|
import java.util.Properties;
|
4
|
|
5
|
import javax.mail.MessagingException;
|
6
|
import javax.mail.PasswordAuthentication;
|
7
|
import javax.mail.Session;
|
8
|
import javax.mail.Transport;
|
9
|
import javax.mail.internet.AddressException;
|
10
|
import javax.mail.internet.InternetAddress;
|
11
|
import javax.mail.internet.MimeMessage;
|
12
|
|
13
|
public class MailLibrary {
|
14
|
private String mailhost = null;
|
15
|
private String from = null;
|
16
|
private String replyTo = null;
|
17
|
private int smtpPort = 25;
|
18
|
private String username = null;
|
19
|
private String password = null;
|
20
|
private boolean authenticate = false;
|
21
|
private String mode = "plain";
|
22
|
boolean debug = false;
|
23
|
|
24
|
private Properties props = null;
|
25
|
|
26
|
public void init() {
|
27
|
props = new Properties();
|
28
|
|
29
|
if (mode.equals("plain")) {
|
30
|
props.put("mail.transport.protocol", "smtp");
|
31
|
|
32
|
props.put("mail.smtp.host", mailhost);
|
33
|
props.put("mail.smtp.port", smtpPort);
|
34
|
} else if (mode.equals("ssl")) {
|
35
|
props.put("mail.transport.protocol", "smtps");
|
36
|
|
37
|
props.put("mail.smtps.host", mailhost);
|
38
|
props.put("mail.smtps.port", smtpPort);
|
39
|
props.put("mail.smtps.auth", "true");
|
40
|
props.put("mail.smtps.user", username);
|
41
|
props.put("mail.smtps.password", password);
|
42
|
} else {
|
43
|
throw new IllegalArgumentException("Unkown mode '" + mode + "'");
|
44
|
}
|
45
|
|
46
|
props.setProperty("mail.debug", this.debug + "");
|
47
|
}
|
48
|
|
49
|
public void sendEmail(String email, String subject, String text) throws AddressException, MessagingException {
|
50
|
sendEmail(new String[] { email }, subject, text);
|
51
|
}
|
52
|
|
53
|
public void sendEmail(String[] recipients, String subject, String text) throws AddressException, MessagingException {
|
54
|
Session session = Session.getInstance(props,
|
55
|
(this.authenticate ? new SMTPAuthenticator() : null));
|
56
|
|
57
|
session.setDebug(debug);
|
58
|
|
59
|
MimeMessage msg = new MimeMessage(session);
|
60
|
|
61
|
msg.setFrom(new InternetAddress(this.from));
|
62
|
msg.setReplyTo(new InternetAddress[] { new InternetAddress(replyTo) });
|
63
|
msg.setSubject(subject, "UTF8");
|
64
|
msg.setContent(text, "text/plain; charset=UTF-8");
|
65
|
|
66
|
for (String recipient : recipients)
|
67
|
msg.addRecipient(javax.mail.Message.RecipientType.TO,
|
68
|
new InternetAddress(recipient));
|
69
|
|
70
|
Transport tr = session.getTransport();
|
71
|
|
72
|
if (authenticate) {
|
73
|
tr.connect(mailhost, smtpPort, username, password);
|
74
|
} else {
|
75
|
tr.connect();
|
76
|
}
|
77
|
|
78
|
tr.sendMessage(msg, msg.getAllRecipients());
|
79
|
tr.close();
|
80
|
}
|
81
|
|
82
|
private class SMTPAuthenticator extends javax.mail.Authenticator {
|
83
|
public PasswordAuthentication getPasswordAuthentication() {
|
84
|
return new PasswordAuthentication(username, password);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
public String getMailhost() {
|
89
|
return mailhost;
|
90
|
}
|
91
|
|
92
|
public void setMailhost(String mailhost) {
|
93
|
this.mailhost = mailhost;
|
94
|
}
|
95
|
|
96
|
public String getFrom() {
|
97
|
return from;
|
98
|
}
|
99
|
|
100
|
public void setFrom(String from) {
|
101
|
this.from = from;
|
102
|
}
|
103
|
|
104
|
public String getReplyTo() {
|
105
|
return replyTo;
|
106
|
}
|
107
|
|
108
|
public void setReplyTo(String replyTo) {
|
109
|
this.replyTo = replyTo;
|
110
|
}
|
111
|
|
112
|
public int getSmtpPort() {
|
113
|
return smtpPort;
|
114
|
}
|
115
|
|
116
|
public void setSmtpPort(int smtpPort) {
|
117
|
this.smtpPort = smtpPort;
|
118
|
}
|
119
|
|
120
|
public String getUsername() {
|
121
|
return username;
|
122
|
}
|
123
|
|
124
|
public void setUsername(String username) {
|
125
|
this.username = username;
|
126
|
}
|
127
|
|
128
|
public String getPassword() {
|
129
|
return password;
|
130
|
}
|
131
|
|
132
|
public void setPassword(String password) {
|
133
|
this.password = password;
|
134
|
}
|
135
|
|
136
|
public boolean isAuthenticate() {
|
137
|
return authenticate;
|
138
|
}
|
139
|
|
140
|
public void setAuthenticate(boolean authenticate) {
|
141
|
this.authenticate = authenticate;
|
142
|
}
|
143
|
|
144
|
public String getMode() {
|
145
|
return mode;
|
146
|
}
|
147
|
|
148
|
public void setMode(String mode) {
|
149
|
this.mode = mode;
|
150
|
}
|
151
|
|
152
|
public boolean isDebug() {
|
153
|
return debug;
|
154
|
}
|
155
|
|
156
|
public void setDebug(boolean debug) {
|
157
|
this.debug = debug;
|
158
|
}
|
159
|
}
|