1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
2 |
|
|
3 |
|
import java.io.UnsupportedEncodingException; |
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.Properties; |
import java.util.Properties; |
6 |
|
|
7 |
import javax.mail.Message; |
import javax.mail.Message; |
8 |
import javax.mail.MessagingException; |
import javax.mail.MessagingException; |
9 |
|
import javax.mail.Multipart; |
10 |
import javax.mail.Session; |
import javax.mail.Session; |
11 |
import javax.mail.Transport; |
import javax.mail.Transport; |
12 |
import javax.mail.internet.InternetAddress; |
import javax.mail.internet.InternetAddress; |
13 |
|
import javax.mail.internet.MimeBodyPart; |
14 |
import javax.mail.internet.MimeMessage; |
import javax.mail.internet.MimeMessage; |
15 |
|
import javax.mail.internet.MimeMultipart; |
16 |
|
|
17 |
import com.google.common.base.Splitter; |
import com.google.common.base.Splitter; |
18 |
|
|
50 |
msg.setSubject( subject ); |
msg.setSubject( subject ); |
51 |
msg.setContent(htmlBody, "text/html; charset=utf-8"); |
msg.setContent(htmlBody, "text/html; charset=utf-8"); |
52 |
|
|
53 |
|
|
54 |
Transport.send(msg); |
Transport.send(msg); |
55 |
|
|
56 |
|
|
57 |
} catch (MessagingException e) { |
} catch (MessagingException e) { |
58 |
logger.warning("Unable to send report mail ", e ); |
logger.warning("Unable to send report mail ", e ); |
59 |
|
} |
60 |
|
} |
61 |
|
|
62 |
|
public static void sendMailWithAttachment(String subject, String htmlBody, String attachmentName, String attachmentBody) { |
63 |
|
ServiceConfig config = ServiceConfig.getInstance(); |
64 |
|
|
65 |
|
|
66 |
|
List<String> mails = Splitter.on(',') |
67 |
|
.trimResults() |
68 |
|
.omitEmptyStrings() |
69 |
|
.splitToList( config.emails ); |
70 |
|
|
71 |
|
Properties props = new Properties(); |
72 |
|
props.put("mail.smtp.host", "mail.dao.int"); |
73 |
|
|
74 |
|
try { |
75 |
|
// create some properties and get the default Session |
76 |
|
Session session = Session.getDefaultInstance(props, null); |
77 |
|
session.setDebug(false); |
78 |
|
|
79 |
|
MimeMessage msg = new MimeMessage(session); |
80 |
|
Multipart multipart = new MimeMultipart(); |
81 |
|
|
82 |
|
msg.setFrom( new InternetAddress("no-reply@daoas.dk") ); |
83 |
|
|
84 |
|
|
85 |
|
for (String recipient : mails) { |
86 |
|
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) ); |
87 |
|
} |
88 |
|
|
89 |
|
msg.setSubject( subject ); |
90 |
|
|
91 |
|
|
92 |
|
MimeBodyPart body = new MimeBodyPart(); |
93 |
|
body.setContent(htmlBody, "text/html; charset=utf-8"); |
94 |
|
multipart.addBodyPart(body); |
95 |
|
|
96 |
|
|
97 |
|
MimeBodyPart attachPart = new MimeBodyPart(); |
98 |
|
attachPart.setText(attachmentBody, "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file); |
99 |
|
|
100 |
|
attachPart.setFileName(attachmentName); |
101 |
|
multipart.addBodyPart(attachPart); |
102 |
|
|
103 |
|
msg.setContent(multipart); |
104 |
|
|
105 |
|
|
106 |
|
Transport.send(msg); |
107 |
|
|
108 |
|
} catch (MessagingException e) { |
109 |
|
logger.warning("Unable to send report mail ", e ); |
110 |
} |
} |
111 |
} |
} |
112 |
} |
} |