1 |
package dk.daoas.adressevedligehold;
|
2 |
|
3 |
import java.util.List;
|
4 |
import java.util.Properties;
|
5 |
|
6 |
import javax.mail.Message;
|
7 |
import javax.mail.MessagingException;
|
8 |
import javax.mail.Session;
|
9 |
import javax.mail.Transport;
|
10 |
import javax.mail.internet.InternetAddress;
|
11 |
import javax.mail.internet.MimeMessage;
|
12 |
|
13 |
import com.google.common.base.Splitter;
|
14 |
|
15 |
import dk.daoas.adressevedligehold.tasks.TaskLogger;
|
16 |
|
17 |
public class MailSender {
|
18 |
|
19 |
private static TaskLogger logger = TaskLogger.getInstance();
|
20 |
|
21 |
public static void sendMail(String subject, String htmlBody) {
|
22 |
ServiceConfig config = ServiceConfig.getInstance();
|
23 |
|
24 |
|
25 |
List<String> mails = Splitter.on(',')
|
26 |
.trimResults()
|
27 |
.omitEmptyStrings()
|
28 |
.splitToList( config.emails );
|
29 |
|
30 |
Properties props = new Properties();
|
31 |
props.put("mail.smtp.host", "mail.dao.int");
|
32 |
|
33 |
try {
|
34 |
// create some properties and get the default Session
|
35 |
Session session = Session.getDefaultInstance(props, null);
|
36 |
session.setDebug(false);
|
37 |
|
38 |
MimeMessage msg = new MimeMessage(session);
|
39 |
msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
|
40 |
|
41 |
|
42 |
for (String recipient : mails) {
|
43 |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
|
44 |
}
|
45 |
|
46 |
msg.setSubject( subject );
|
47 |
msg.setContent(htmlBody, "text/html; charset=utf-8");
|
48 |
|
49 |
Transport.send(msg);
|
50 |
|
51 |
|
52 |
} catch (MessagingException e) {
|
53 |
logger.warning("Unable to send report mail ", e );
|
54 |
}
|
55 |
}
|
56 |
}
|