-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmtpEmailSender.java
216 lines (167 loc) · 6.86 KB
/
SmtpEmailSender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package bbtrial.nl.logicgate.ace;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
*
* depends upon http://java.sun.com/products/javamail/
*
* http://java.sun.com/products/javamail/downloads/index.html
*
* @author eric
*
*/
public class SmtpEmailSender implements EmailSender {
public static final int DEFAULT_SMTP_PORT = 25;
private String smtpServer;
private int port;
private String smtpAccount;
private String smtpPassword;
private boolean enableTLS;
private boolean enableDebug;
private String defaultFrom;
public SmtpEmailSender(String smtpServer, int smtpPort, String smtpAccount,
String smtpPassword, boolean enableTLS, String defaultFrom) {
if (smtpServer == null || smtpServer.equals("")) {
this.smtpServer = "localhost";
} else {
this.smtpServer = smtpServer;
}
if (smtpPort <= 0) {
this.port = DEFAULT_SMTP_PORT;
} else {
this.port = smtpPort;
}
this.smtpAccount = smtpAccount;
this.smtpPassword = smtpPassword;
this.enableTLS = enableTLS;
this.enableDebug = false;
this.defaultFrom = defaultFrom;
}
public void setEnableDebug(boolean enableDebug) {
this.enableDebug = enableDebug;
}
@Override
public void sendMail(String to, String subject, String body)
throws AddressException, MessagingException {
sendMail(defaultFrom, to, subject, body, null,
new HashMap<String, String>());
}
@Override
public void sendMail(String from, String to, String subject,
String textBody, String htmlBody, Map<String, String> headers)
throws AddressException, MessagingException {
Session mailSession = createMailSession();
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject(mimeEncode(subject));
msg.setSentDate(new Date());
addHeaders(msg, headers);
Multipart mp = createAndFillMultiPart(textBody, htmlBody);
msg.setContent(mp);
msg.saveChanges();
Transport.send(msg);
}
private Session createMailSession() {
Properties mailProps = createMailSessionProperties();
Session mailSession = getMailSession(mailProps);
mailSession.setDebug(enableDebug);
return mailSession;
}
private void addHeaders(MimeMessage msg, Map<String, String> headers)
throws MessagingException {
// Add any "required" headers ...
msg.addHeader("X-Eric-Conspiracy", "There is no conspiracy");
// even custom headers passed in by the caller
for (Map.Entry<String, String> mapEntry : headers.entrySet()) {
String header = mapEntry.getKey();
String value = mimeEncode(mapEntry.getValue());
msg.addHeader(header, value);
}
}
private Multipart createAndFillMultiPart(String textBody, String htmlBody)
throws MessagingException {
Multipart mp = new MimeMultipart("alternative");
BodyPart bpText = new MimeBodyPart();
bpText.setContent(textBody, "text/plain;charset=UTF-8");
mp.addBodyPart(bpText);
if (htmlBody != null) {
BodyPart bpHtml = new MimeBodyPart();
bpHtml.setContent(htmlBody, "text/html;charset=UTF-8");
mp.addBodyPart(bpHtml);
}
return mp;
}
private Properties createMailSessionProperties() {
Properties mailProps = new Properties();
if (enableDebug) {
mailProps.put("mail.debug", "" + enableDebug);
}
mailProps.put("mail.smtp.host", smtpServer);
mailProps.put("mail.smtp.port", "" + port);
/*
* allow 8-bit mime, which the SMTP protocol says it will STILL fall
* back to 7bit (base64) encoding if the server doesn't support it.
* We've had good luck with well-configured servers before...
*/
mailProps.put("mail.smtp.allow8bitmime", true);
/*
* It's usually a good idea to set TLS as true, because if the server
* does not support it, it should at least fall back to non TLS.
*/
mailProps.put("mail.smtp.starttls.enable", "" + enableTLS);
return mailProps;
}
private Session getMailSession(Properties mailProps) {
// if no smtp account is specified, try to get a generic session.
if (smtpAccount == null || smtpAccount.length() == 0) {
return Session.getInstance(mailProps);
}
// else an smtp account is specified, so turn on authentication.
mailProps.put("mail.smtp.auth", Boolean.TRUE.toString());
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpAccount, smtpPassword);
}
};
// and get a session using simple PasswordAuthentication
return Session.getInstance(mailProps, authenticator);
}
private String mimeEncode(String value) {
try {
return MimeUtility.encodeText(value, "UTF-8", null);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage() + " (" + value + ")", e);
}
}
/* really, only useful for debug */
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(getClass().getSimpleName());
buf.append(" [smtpServer: ").append(smtpServer);
buf.append(", port: ").append(port);
buf.append(", smtpAccount: ").append(smtpAccount);
// buf.append(", smtpPassword: ").append(smtpPassword);
buf.append(", enableTLS: ").append(enableTLS);
buf.append(", enableDebug: ").append(enableDebug);
buf.append("]");
return buf.toString();
}
}