1 |
package dk.daoas.adressevedligehold;
|
2 |
|
3 |
import java.util.List;
|
4 |
import java.util.Map;
|
5 |
import java.util.Map.Entry;
|
6 |
import java.util.Properties;
|
7 |
|
8 |
import javax.mail.Message;
|
9 |
import javax.mail.MessagingException;
|
10 |
import javax.mail.Multipart;
|
11 |
import javax.mail.Session;
|
12 |
import javax.mail.Transport;
|
13 |
import javax.mail.internet.InternetAddress;
|
14 |
import javax.mail.internet.MimeBodyPart;
|
15 |
import javax.mail.internet.MimeMessage;
|
16 |
import javax.mail.internet.MimeMultipart;
|
17 |
|
18 |
import com.google.common.base.Splitter;
|
19 |
|
20 |
import dk.daoas.adressevedligehold.tasks.TaskLogger;
|
21 |
|
22 |
public class MailSender {
|
23 |
|
24 |
private static TaskLogger logger = TaskLogger.getInstance();
|
25 |
|
26 |
public static void sendMail(String subject, String htmlBody) {
|
27 |
ServiceConfig config = ServiceConfig.getInstance();
|
28 |
|
29 |
|
30 |
List<String> mails = Splitter.on(',')
|
31 |
.trimResults()
|
32 |
.omitEmptyStrings()
|
33 |
.splitToList( config.emails );
|
34 |
|
35 |
Properties props = new Properties();
|
36 |
props.put("mail.smtp.host", "mail.dao.int");
|
37 |
|
38 |
try {
|
39 |
// create some properties and get the default Session
|
40 |
Session session = Session.getDefaultInstance(props, null);
|
41 |
session.setDebug(false);
|
42 |
|
43 |
MimeMessage msg = new MimeMessage(session);
|
44 |
msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
|
45 |
|
46 |
|
47 |
for (String recipient : mails) {
|
48 |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
|
49 |
}
|
50 |
|
51 |
msg.setSubject( subject );
|
52 |
msg.setContent(htmlBody, "text/html; charset=utf-8");
|
53 |
|
54 |
|
55 |
Transport.send(msg);
|
56 |
|
57 |
|
58 |
} catch (MessagingException e) {
|
59 |
logger.warning("Unable to send report mail ", e );
|
60 |
}
|
61 |
}
|
62 |
|
63 |
public static void sendMailWithAttachment(String subject, String htmlBody, String attachmentName, String attachmentBody) {
|
64 |
ServiceConfig config = ServiceConfig.getInstance();
|
65 |
|
66 |
|
67 |
List<String> mails = Splitter.on(',')
|
68 |
.trimResults()
|
69 |
.omitEmptyStrings()
|
70 |
.splitToList( config.emails );
|
71 |
|
72 |
Properties props = new Properties();
|
73 |
props.put("mail.smtp.host", "mail.dao.int");
|
74 |
|
75 |
try {
|
76 |
// create some properties and get the default Session
|
77 |
Session session = Session.getDefaultInstance(props, null);
|
78 |
session.setDebug(false);
|
79 |
|
80 |
MimeMessage msg = new MimeMessage(session);
|
81 |
Multipart multipart = new MimeMultipart();
|
82 |
|
83 |
msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
|
84 |
|
85 |
|
86 |
for (String recipient : mails) {
|
87 |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
|
88 |
}
|
89 |
|
90 |
msg.setSubject( subject );
|
91 |
|
92 |
|
93 |
MimeBodyPart body = new MimeBodyPart();
|
94 |
body.setContent(htmlBody, "text/html; charset=utf-8");
|
95 |
multipart.addBodyPart(body);
|
96 |
|
97 |
|
98 |
MimeBodyPart attachPart = new MimeBodyPart();
|
99 |
attachPart.setText(attachmentBody, "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
|
100 |
|
101 |
attachPart.setFileName(attachmentName);
|
102 |
multipart.addBodyPart(attachPart);
|
103 |
|
104 |
msg.setContent(multipart);
|
105 |
|
106 |
|
107 |
Transport.send(msg);
|
108 |
|
109 |
} catch (MessagingException e) {
|
110 |
logger.warning("Unable to send report mail ", e );
|
111 |
}
|
112 |
}
|
113 |
|
114 |
public static void sendMailWithAttachments(String subject, String htmlBody, Map<String,String> attachments) {
|
115 |
ServiceConfig config = ServiceConfig.getInstance();
|
116 |
|
117 |
|
118 |
List<String> mails = Splitter.on(',')
|
119 |
.trimResults()
|
120 |
.omitEmptyStrings()
|
121 |
.splitToList( config.emails );
|
122 |
|
123 |
Properties props = new Properties();
|
124 |
props.put("mail.smtp.host", "mail.dao.int");
|
125 |
|
126 |
try {
|
127 |
// create some properties and get the default Session
|
128 |
Session session = Session.getDefaultInstance(props, null);
|
129 |
session.setDebug(false);
|
130 |
|
131 |
MimeMessage msg = new MimeMessage(session);
|
132 |
Multipart multipart = new MimeMultipart();
|
133 |
|
134 |
msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
|
135 |
|
136 |
|
137 |
for (String recipient : mails) {
|
138 |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
|
139 |
}
|
140 |
|
141 |
msg.setSubject( subject );
|
142 |
|
143 |
|
144 |
MimeBodyPart body = new MimeBodyPart();
|
145 |
body.setContent(htmlBody, "text/html; charset=utf-8");
|
146 |
multipart.addBodyPart(body);
|
147 |
|
148 |
for( Entry<String,String> attachment : attachments.entrySet() ) {
|
149 |
MimeBodyPart attachPart = new MimeBodyPart();
|
150 |
attachPart.setFileName(attachment.getKey());
|
151 |
attachPart.setText( attachment.getValue(), "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
|
152 |
|
153 |
|
154 |
multipart.addBodyPart(attachPart);
|
155 |
}
|
156 |
|
157 |
msg.setContent(multipart);
|
158 |
|
159 |
Transport.send(msg);
|
160 |
|
161 |
} catch (MessagingException e) {
|
162 |
logger.warning("Unable to send report mail ", e );
|
163 |
}
|
164 |
}
|
165 |
}
|