/[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 3044 - (show annotations) (download)
Wed Jun 8 15:31:27 2016 UTC (7 years, 11 months ago) by torben
File size: 3147 byte(s)
Vedhæft en detaljeret åbne/lukke fil til rapport mailen
1 package dk.daoas.adressevedligehold;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.List;
5 import java.util.Properties;
6
7 import javax.mail.Message;
8 import javax.mail.MessagingException;
9 import javax.mail.Multipart;
10 import javax.mail.Session;
11 import javax.mail.Transport;
12 import javax.mail.internet.InternetAddress;
13 import javax.mail.internet.MimeBodyPart;
14 import javax.mail.internet.MimeMessage;
15 import javax.mail.internet.MimeMultipart;
16
17 import com.google.common.base.Splitter;
18
19 import dk.daoas.adressevedligehold.tasks.TaskLogger;
20
21 public class MailSender {
22
23 private static TaskLogger logger = TaskLogger.getInstance();
24
25 public static void sendMail(String subject, String htmlBody) {
26 ServiceConfig config = ServiceConfig.getInstance();
27
28
29 List<String> mails = Splitter.on(',')
30 .trimResults()
31 .omitEmptyStrings()
32 .splitToList( config.emails );
33
34 Properties props = new Properties();
35 props.put("mail.smtp.host", "mail.dao.int");
36
37 try {
38 // create some properties and get the default Session
39 Session session = Session.getDefaultInstance(props, null);
40 session.setDebug(false);
41
42 MimeMessage msg = new MimeMessage(session);
43 msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
44
45
46 for (String recipient : mails) {
47 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
48 }
49
50 msg.setSubject( subject );
51 msg.setContent(htmlBody, "text/html; charset=utf-8");
52
53
54 Transport.send(msg);
55
56
57 } catch (MessagingException e) {
58 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 }

  ViewVC Help
Powered by ViewVC 1.1.20