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 package org.melati.util;
45
46 import java.io.File;
47 import java.io.IOException;
48
49 import javax.activation.DataHandler;
50 import javax.activation.FileDataSource;
51 import javax.mail.Address;
52 import javax.mail.Message;
53 import javax.mail.MessagingException;
54 import javax.mail.Multipart;
55 import javax.mail.Session;
56 import javax.mail.Transport;
57 import javax.mail.internet.InternetAddress;
58 import javax.mail.internet.MimeBodyPart;
59 import javax.mail.internet.MimeMessage;
60 import javax.mail.internet.MimeMultipart;
61
62
63
64
65 public final class Email {
66
67 private Email() {
68 }
69
70
71
72
73
74
75
76
77
78
79 public static void send(String smtpServer, String from, String to,
80 String replyto, String subject, String text)
81 throws EmailException, IOException {
82 File[] empty = {};
83 sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty);
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public static void sendToList(String smtpServer, String from,
97 String[] toList, String replyto, String subject,
98 String message) throws EmailException, IOException {
99 File[] empty = {};
100
101 for (int i = 0; i < toList.length; i++)
102 sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public static void sendWithAttachments(String smtpServer, String from,
117 String to, String replyto, String subject, String text, File[] attachments)
118 throws EmailException, IOException {
119
120
121 Message message = initialiseMessage(smtpServer, from, to, replyto, subject);
122 try {
123
124 MimeBodyPart mbp1 = new MimeBodyPart();
125 mbp1.setText(text);
126 Multipart mp = new MimeMultipart();
127 mp.addBodyPart(mbp1);
128 for (int i = 0; i < attachments.length; i++) {
129 File f = attachments[i];
130 if (f != null) {
131
132 MimeBodyPart mbp2 = new MimeBodyPart();
133
134 FileDataSource fds = new FileDataSource(f);
135 mbp2.setDataHandler(new DataHandler(fds));
136 mbp2.setFileName(fds.getName());
137 mp.addBodyPart(mbp2);
138 }
139 }
140
141 message.setContent(mp);
142 } catch (Exception e) {
143 e.printStackTrace();
144 throw new EmailException("Problem creating message: " + e.toString());
145 }
146
147 post(message);
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public static void sendAsHtmlWithAttachments(String smtpServer, String from,
164 String to, String replyto, String subject, String plainText,
165 String htmlText, File[] referenced, File[] attachments)
166 throws EmailException, IOException {
167
168
169 Message message = initialiseMessage(smtpServer, from, to, replyto, subject);
170 try {
171 Multipart mp = new MimeMultipart("related");
172 MimeBodyPart mbp1 = new MimeBodyPart();
173
174 mbp1.setContent(plainText, "text/plain");
175 mp.addBodyPart(mbp1);
176 MimeBodyPart mbp2 = new MimeBodyPart();
177 mbp2.setContent(htmlText, "text/html");
178 mp.addBodyPart(mbp2);
179
180 if (referenced != null) {
181 for (int i = 0; i < referenced.length; i++) {
182 File f = referenced[i];
183 if (f != null) {
184 MimeBodyPart mbp3 = new MimeBodyPart();
185 FileDataSource fds = new FileDataSource(f);
186 mbp3.setDataHandler(new DataHandler(fds));
187 mbp3.setFileName(fds.getName());
188 mp.addBodyPart(mbp3);
189 }
190 }
191 }
192 if (attachments != null) {
193 for (int i = 0; i < attachments.length; i++) {
194 File f = attachments[i];
195 if (f != null) {
196 MimeBodyPart mbp4 = new MimeBodyPart();
197 if (f.getName() == null) {
198 System.out.println("name is null");
199 }
200 FileDataSource fds = new FileDataSource(f);
201 mbp4.setDataHandler(new DataHandler(fds));
202 mbp4.setFileName(fds.getName());
203 mp.addBodyPart(mbp4);
204 }
205 }
206 }
207
208 message.setContent(mp);
209 } catch (Exception e) {
210 e.printStackTrace();
211 throw new EmailException("Problem creating message: " + e.toString());
212 }
213
214 post(message);
215 }
216
217 private static Message initialiseMessage(String smtpServer, String from,
218 String to, String replyto, String subject) throws EmailException {
219
220
221
222 java.util.Properties properties = System.getProperties();
223 properties.put("mail.smtp.host", smtpServer);
224
225
226 properties.put("mail.smtp.localhost", smtpServer);
227 Session session = Session.getInstance(properties, null);
228 MimeMessage message = new MimeMessage(session);
229
230 Address fromAddress;
231 try {
232 fromAddress = new InternetAddress(from);
233 message.setFrom(fromAddress);
234
235 Address[] toAddresses = InternetAddress.parse(to);
236 message.setRecipients(Message.RecipientType.TO, toAddresses);
237
238
239
240
241
242
243
244 if (replyto != null) {
245 Address[] replyTos = InternetAddress.parse(replyto);
246 message.setReplyTo(replyTos);
247 }
248 message.setSubject(subject);
249 } catch (Exception e) {
250 e.printStackTrace();
251 throw new EmailException("Problem sending message: " + e.toString());
252 }
253 return message;
254 }
255
256 private static void post(Message message) throws EmailException {
257 try {
258 Transport.send(message);
259 } catch (MessagingException e) {
260 e.printStackTrace();
261 throw new EmailException("Problem sending message: " + e.toString());
262 }
263
264 }
265
266
267
268 public static String mailAddress(String name, String email) {
269 return name + " <" + email + ">";
270 }
271
272 }