/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/MailSender.java
ViewVC logotype

Contents of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/MailSender.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3068 - (show annotations) (download)
Thu Jul 21 11:48:19 2016 UTC (7 years, 9 months ago) by torben
File size: 3101 byte(s)
Various small corrections and bump findBugs-maven
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.Multipart;
9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.internet.InternetAddress;
12 import javax.mail.internet.MimeBodyPart;
13 import javax.mail.internet.MimeMessage;
14 import javax.mail.internet.MimeMultipart;
15
16 import com.google.common.base.Splitter;
17
18 import dk.daoas.adressevedligehold.tasks.TaskLogger;
19
20 public class MailSender {
21
22 private static TaskLogger logger = TaskLogger.getInstance();
23
24 public static void sendMail(String subject, String htmlBody) {
25 ServiceConfig config = ServiceConfig.getInstance();
26
27
28 List<String> mails = Splitter.on(',')
29 .trimResults()
30 .omitEmptyStrings()
31 .splitToList( config.emails );
32
33 Properties props = new Properties();
34 props.put("mail.smtp.host", "mail.dao.int");
35
36 try {
37 // create some properties and get the default Session
38 Session session = Session.getDefaultInstance(props, null);
39 session.setDebug(false);
40
41 MimeMessage msg = new MimeMessage(session);
42 msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
43
44
45 for (String recipient : mails) {
46 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
47 }
48
49 msg.setSubject( subject );
50 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.setText(attachmentBody, "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
98
99 attachPart.setFileName(attachmentName);
100 multipart.addBodyPart(attachPart);
101
102 msg.setContent(multipart);
103
104
105 Transport.send(msg);
106
107 } catch (MessagingException e) {
108 logger.warning("Unable to send report mail ", e );
109 }
110 }
111 }

  ViewVC Help
Powered by ViewVC 1.1.20