-
Notifications
You must be signed in to change notification settings - Fork 182
/
GreenMailUtil.java
453 lines (411 loc) · 15.9 KB
/
GreenMailUtil.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) 2014 Wael Chatila / Icegreen Technologies. All Rights Reserved.
* This software is released under the Apache license 2.0
*/
package com.icegreen.greenmail.util;
import java.io.*;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.icegreen.greenmail.user.GreenMailUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Wael Chatila
* @version $Id: $
* @since Jan 29, 2006
*/
public class GreenMailUtil {
private static final Logger log = LoggerFactory.getLogger(GreenMailUtil.class);
/**
* used internally for {@link #random()}
*/
private static int generateCount = 0;
private static final String GENERATE_SET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789";
private static final int GENERATE_SET_SIZE = GENERATE_SET.length();
private static final Random RANDOM = new Random();
private GreenMailUtil() {
//empty
}
/**
* Writes the content of an input stream to an output stream
*
* @throws IOException on io error.
*/
public static void copyStream(final InputStream src, OutputStream dest) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = src.read(buffer)) > -1) {
dest.write(buffer, 0, read);
}
dest.flush();
}
/**
* Convenience method which creates a new {@link MimeMessage} from an input stream.
*
* @return the created mime message.
*/
public static MimeMessage newMimeMessage(InputStream inputStream) {
try {
final Properties props = new Properties();
props.setProperty("mail.mime.allowutf8", System.getProperty("mail.mime.allowutf8","true"));
return new MimeMessage(Session.getDefaultInstance(props), inputStream);
} catch (MessagingException e) {
throw new IllegalArgumentException("Can not generate mime message for input stream " + inputStream, e);
}
}
/**
* Convenience method which creates a new {@link MimeMessage} from a string
*/
public static MimeMessage newMimeMessage(String mailString) {
return newMimeMessage(EncodingUtil.toStream(mailString, EncodingUtil.CHARSET_EIGHT_BIT_ENCODING));
}
public static boolean hasNonTextAttachments(Part m) {
try {
Object content = m.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart mm = (MimeMultipart) content;
for (int i = 0; i < mm.getCount(); i++) {
BodyPart p = mm.getBodyPart(i);
if (hasNonTextAttachments(p)) {
return true;
}
}
return false;
} else {
return !m.getContentType().trim().toLowerCase().startsWith("text");
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
/**
* Counts the number of lines.
*
* @param str the input string
* @return Returns the number of lines terminated by '\n' in string
*/
public static int getLineCount(String str) {
if (null == str || str.isEmpty()) {
return 0;
}
int count = 1;
for (char c : str.toCharArray()) {
if ('\n' == c) {
count++;
}
}
return count;
}
/**
* @return The content part of an email (or a Part)
*
* Note: You might have to use MimeUtility.decodeText(contentPart)
* on the result to decode the (possibly) quoted-printable encoded special characters.
*/
public static String getBody(Part msg) {
String all = getWholeMessage(msg);
int i = all.indexOf("\r\n\r\n");
return i < 0 ? "" /* empty body */ : all.substring(i + 4);
}
/**
* @return The headers of an email (or a Part)
*/
public static String getHeaders(Part msg) {
String all = getWholeMessage(msg);
int i = all.indexOf("\r\n\r\n");
return i < 0 ? all : all.substring(0, i);
}
/**
* @return The both header and body for an email (or a Part)
*/
public static String getWholeMessage(Part msg) {
try {
ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
msg.writeTo(bodyOut);
return bodyOut.toString(EncodingUtil.EIGHT_BIT_ENCODING).trim();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public static byte[] getBodyAsBytes(Part msg) {
return getBody(msg).getBytes(EncodingUtil.CHARSET_EIGHT_BIT_ENCODING);
}
public static byte[] getHeaderAsBytes(Part part) {
return getHeaders(part).getBytes(EncodingUtil.CHARSET_EIGHT_BIT_ENCODING);
}
/**
* @return same as {@link #getWholeMessage(jakarta.mail.Part)}
*/
public static String toString(Part msg) {
return getWholeMessage(msg);
}
/**
* Generates a random generated password consisting of letters and digits
* with a length variable between 5 and 8 characters long.
* Passwords are further optimized for displays
* that could potentially display the characters <i>1,l,I,0,O,Q</i> in a way
* that a human could easily mix them up.
*
* @return the random string.
*/
public static String random() {
int nbrOfLetters = RANDOM.nextInt(3) + 5;
return random(nbrOfLetters);
}
public static String random(int nbrOfLetters) {
StringBuilder ret = new StringBuilder();
for (/* empty */; nbrOfLetters > 0; nbrOfLetters--) {
int pos = (RANDOM.nextInt(GENERATE_SET_SIZE) + (++generateCount)) % GENERATE_SET_SIZE;
ret.append(GENERATE_SET.charAt(pos));
}
return ret.toString();
}
/**
* Sends a text message using the default test setup for SMTP.
*
* @param to the to address.
* @param from the from address.
* @param subject the subject.
* @param msg the text message.
* @see ServerSetupTest#SMTP
*/
public static void sendTextEmailTest(String to, String from, String subject, String msg) {
sendTextEmail(to, from, subject, msg, ServerSetupTest.SMTP);
}
/**
* Sends a text message using the default test setup for SMTPS.
*
* @param to the to address.
* @param from the from address.
* @param subject the subject.
* @param msg the text message.
* @see ServerSetupTest#SMTPS
*/
public static void sendTextEmailSecureTest(String to, String from, String subject, String msg) {
sendTextEmail(to, from, subject, msg, ServerSetupTest.SMTPS);
}
public static String getAddressList(Address[] addresses) {
if (null == addresses) {
return null;
}
StringBuilder ret = new StringBuilder();
for (int i = 0; i < addresses.length; i++) {
if (i > 0) {
ret.append(", ");
}
ret.append(addresses[i].toString());
}
return ret.toString();
}
public static MimeMessage createTextEmail(String to, String from, String subject, String msg, final ServerSetup setup) {
try {
Session session = getSession(setup);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setSubject(subject);
mimeMessage.setSentDate(new Date());
mimeMessage.setFrom(from);
mimeMessage.setRecipients(Message.RecipientType.TO, to);
mimeMessage.setText(msg);
return mimeMessage;
} catch (MessagingException e) {
throw new IllegalArgumentException("Can not generate message", e);
}
}
/**
* Sends a text message using given server setup for SMTP.
*
* @param to the to address.
* @param from the from address.
* @param subject the subject.
* @param msg the test message.
* @param setup the SMTP setup.
*/
public static void sendTextEmail(String to, String from, String subject, String msg, final ServerSetup setup) {
sendMimeMessage(createTextEmail(to, from, subject, msg, setup));
}
/**
* Send the message using the JavaMail session defined in the message
*
* @param mimeMessage Message to send
*/
public static void sendMimeMessage(MimeMessage mimeMessage) {
try {
Transport.send(mimeMessage);
} catch (MessagingException e) {
throw new IllegalStateException("Can not send message " + mimeMessage, e);
}
}
/**
* Send the message using the JavaMail session defined in the message
*
* @param mimeMessage Message to send
* @param username Username for authentication.
* @param password Password for authentication.
*/
public static void sendMimeMessage(MimeMessage mimeMessage, String username, String password) {
try {
Transport.send(mimeMessage, username, password);
} catch (MessagingException e) {
throw new IllegalStateException("Can not send message " + mimeMessage, e);
}
}
/**
* Send the message with the given attributes and the given body using the specified SMTP settings
*
* @param to Destination address(es)
* @param from Sender address
* @param subject Message subject
* @param body Message content. May either be a MimeMultipart or another body that java mail recognizes
* @param contentType MIME content type of body
* @param serverSetup Server settings to use for connecting to the SMTP server
*/
public static void sendMessageBody(String to, String from, String subject, Object body, String contentType, ServerSetup serverSetup) {
try {
Session smtpSession = getSession(serverSetup);
MimeMessage mimeMessage = new MimeMessage(smtpSession);
mimeMessage.setRecipients(Message.RecipientType.TO, to);
mimeMessage.setFrom(from);
mimeMessage.setSubject(subject);
mimeMessage.setContent(body, contentType);
sendMimeMessage(mimeMessage);
} catch (MessagingException e) {
throw new IllegalStateException("Can not send message", e);
}
}
public static void sendAttachmentEmail(String to, String from,
String subject, String msg, final byte[] attachment,
final String contentType, final String filename,
final String description, final ServerSetup setup) {
MimeMultipart multiPart = createMultipartWithAttachment(msg, attachment, contentType, filename, description);
sendMessageBody(to, from, subject, multiPart, null, setup);
}
/**
* Create new multipart with a text part and an attachment
*
* @param msg Message text
* @param attachment Attachment data
* @param contentType MIME content type of body
* @param filename File name of the attachment
* @param description Description of the attachment
* @return New multipart
*/
public static MimeMultipart createMultipartWithAttachment(String msg, final byte[] attachment, final String contentType,
final String filename, String description) {
try {
MimeMultipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
multiPart.addBodyPart(textPart);
textPart.setText(msg);
MimeBodyPart binaryPart = new MimeBodyPart();
multiPart.addBodyPart(binaryPart);
DataSource ds = new DataSource() {
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(attachment);
}
@Override
public OutputStream getOutputStream() throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write(attachment);
return byteStream;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public String getName() {
return filename;
}
};
binaryPart.setDataHandler(new DataHandler(ds));
binaryPart.setFileName(filename);
binaryPart.setDescription(description);
return multiPart;
} catch (MessagingException e) {
throw new IllegalArgumentException("Can not create multipart message with attachment", e);
}
}
/**
* Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
*
* @param setup the setup type, such as <code>ServerSetup.IMAP</code>
* @return the JavaMail session.
*/
public static Session getSession(final ServerSetup setup) {
return getSession(setup, null);
}
/**
* Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
*
* @param setup the setup type, such as <code>ServerSetup.IMAP</code>
* @param mailProps additional mail properties.
* @return the JavaMail session.
*/
public static Session getSession(final ServerSetup setup, Properties mailProps) {
return getSession(setup, mailProps, false);
}
/**
* Gets a JavaMail Session for given server type such as IMAP and additional props for JavaMail.
*
* @param setup the setup type, such as <code>ServerSetup.IMAP</code>
* @param mailProps additional mail properties.
* @param debug sets JavaMail debug properties.
* @return the JavaMail session.
*/
public static Session getSession(final ServerSetup setup, Properties mailProps, boolean debug) {
Properties props = setup.configureJavaMailSessionProperties(mailProps, debug);
log.debug("Mail session properties are {}", props);
return Session.getInstance(props, null);
}
/**
* Sets a quota for a users.
*
* @param user the user.
* @param quota the quota.
*/
public static void setQuota(final GreenMailUser user, final Quota quota) {
Session session = GreenMailUtil.getSession(ServerSetupTest.IMAP);
try {
Store store = session.getStore("imap");
store.connect(user.getEmail(), user.getPassword());
try {
((QuotaAwareStore) store).setQuota(quota);
} finally {
store.close();
}
} catch (Exception ex) {
throw new IllegalStateException("Can not set quota " + quota
+ " for user " + user, ex);
}
}
/**
* Gets the quotas for the user.
*
* @param user the user.
* @param quotaRoot the quota root, eg 'INBOX.
* @return array of current quotas, or an empty array if not set.
*/
public static Quota[] getQuota(final GreenMailUser user, final String quotaRoot) {
Session session = GreenMailUtil.getSession(ServerSetupTest.IMAP);
try {
Store store = session.getStore("imap");
store.connect(user.getEmail(), user.getPassword());
try {
return ((QuotaAwareStore) store).getQuota(quotaRoot);
} finally {
store.close();
}
} catch (Exception ex) {
throw new IllegalStateException("Can not get quota for quota root "
+ quotaRoot + " for user " + user, ex);
}
}
}