1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
2 |
|
|
|
import java.io.UnsupportedEncodingException; |
|
3 |
import java.util.List; |
import java.util.List; |
4 |
|
import java.util.Map; |
5 |
|
import java.util.Map.Entry; |
6 |
import java.util.Properties; |
import java.util.Properties; |
7 |
|
|
8 |
import javax.mail.Message; |
import javax.mail.Message; |
106 |
|
|
107 |
Transport.send(msg); |
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) { |
} catch (MessagingException e) { |
162 |
logger.warning("Unable to send report mail ", e ); |
logger.warning("Unable to send report mail ", e ); |
163 |
} |
} |