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