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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3069 - (hide annotations) (download)
Fri Jul 22 09:48:05 2016 UTC (7 years, 10 months ago) by torben
File size: 4750 byte(s)
Instead of adding dawa report details inline, put them in .csv attachments
1 torben 2953 package dk.daoas.adressevedligehold;
2    
3     import java.util.List;
4 torben 3069 import java.util.Map;
5     import java.util.Map.Entry;
6 torben 2953 import java.util.Properties;
7 torben 3069 import java.util.Set;
8 torben 2953
9     import javax.mail.Message;
10     import javax.mail.MessagingException;
11 torben 3043 import javax.mail.Multipart;
12 torben 2953 import javax.mail.Session;
13     import javax.mail.Transport;
14     import javax.mail.internet.InternetAddress;
15 torben 3043 import javax.mail.internet.MimeBodyPart;
16 torben 2953 import javax.mail.internet.MimeMessage;
17 torben 3043 import javax.mail.internet.MimeMultipart;
18 torben 2953
19     import com.google.common.base.Splitter;
20    
21     import dk.daoas.adressevedligehold.tasks.TaskLogger;
22    
23     public class MailSender {
24    
25     private static TaskLogger logger = TaskLogger.getInstance();
26    
27     public static void sendMail(String subject, String htmlBody) {
28     ServiceConfig config = ServiceConfig.getInstance();
29    
30    
31     List<String> mails = Splitter.on(',')
32     .trimResults()
33     .omitEmptyStrings()
34     .splitToList( config.emails );
35    
36     Properties props = new Properties();
37     props.put("mail.smtp.host", "mail.dao.int");
38    
39     try {
40     // create some properties and get the default Session
41     Session session = Session.getDefaultInstance(props, null);
42     session.setDebug(false);
43    
44     MimeMessage msg = new MimeMessage(session);
45     msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
46    
47    
48     for (String recipient : mails) {
49     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
50     }
51    
52     msg.setSubject( subject );
53     msg.setContent(htmlBody, "text/html; charset=utf-8");
54    
55 torben 3043
56 torben 2953 Transport.send(msg);
57    
58    
59     } catch (MessagingException e) {
60     logger.warning("Unable to send report mail ", e );
61     }
62     }
63 torben 3043
64     public static void sendMailWithAttachment(String subject, String htmlBody, String attachmentName, String attachmentBody) {
65     ServiceConfig config = ServiceConfig.getInstance();
66    
67    
68     List<String> mails = Splitter.on(',')
69     .trimResults()
70     .omitEmptyStrings()
71     .splitToList( config.emails );
72    
73     Properties props = new Properties();
74     props.put("mail.smtp.host", "mail.dao.int");
75    
76     try {
77     // create some properties and get the default Session
78     Session session = Session.getDefaultInstance(props, null);
79     session.setDebug(false);
80    
81     MimeMessage msg = new MimeMessage(session);
82     Multipart multipart = new MimeMultipart();
83    
84     msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
85    
86    
87     for (String recipient : mails) {
88     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
89     }
90    
91     msg.setSubject( subject );
92    
93    
94     MimeBodyPart body = new MimeBodyPart();
95     body.setContent(htmlBody, "text/html; charset=utf-8");
96     multipart.addBodyPart(body);
97    
98    
99     MimeBodyPart attachPart = new MimeBodyPart();
100 torben 3044 attachPart.setText(attachmentBody, "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
101    
102 torben 3043 attachPart.setFileName(attachmentName);
103     multipart.addBodyPart(attachPart);
104    
105     msg.setContent(multipart);
106    
107    
108     Transport.send(msg);
109    
110     } catch (MessagingException e) {
111     logger.warning("Unable to send report mail ", e );
112     }
113     }
114 torben 3069
115     public static void sendMailWithAttachments(String subject, String htmlBody, Map<String,String> attachments) {
116     ServiceConfig config = ServiceConfig.getInstance();
117    
118    
119     List<String> mails = Splitter.on(',')
120     .trimResults()
121     .omitEmptyStrings()
122     .splitToList( config.emails );
123    
124     Properties props = new Properties();
125     props.put("mail.smtp.host", "mail.dao.int");
126    
127     try {
128     // create some properties and get the default Session
129     Session session = Session.getDefaultInstance(props, null);
130     session.setDebug(false);
131    
132     MimeMessage msg = new MimeMessage(session);
133     Multipart multipart = new MimeMultipart();
134    
135     msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
136    
137    
138     for (String recipient : mails) {
139     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
140     }
141    
142     msg.setSubject( subject );
143    
144    
145     MimeBodyPart body = new MimeBodyPart();
146     body.setContent(htmlBody, "text/html; charset=utf-8");
147     multipart.addBodyPart(body);
148    
149     for( Entry<String,String> attachment : attachments.entrySet() ) {
150     MimeBodyPart attachPart = new MimeBodyPart();
151     attachPart.setFileName(attachment.getKey());
152     attachPart.setText( attachment.getValue(), "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
153    
154    
155     multipart.addBodyPart(attachPart);
156     }
157    
158     msg.setContent(multipart);
159    
160     Transport.send(msg);
161    
162     } catch (MessagingException e) {
163     logger.warning("Unable to send report mail ", e );
164     }
165     }
166 torben 2953 }

  ViewVC Help
Powered by ViewVC 1.1.20