/[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 3078 - (hide annotations) (download)
Fri Jul 29 06:11:38 2016 UTC (7 years, 10 months ago) by torben
File size: 4823 byte(s)
Enhance handling of mail sending
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    
8     import javax.mail.Message;
9     import javax.mail.MessagingException;
10 torben 3043 import javax.mail.Multipart;
11 torben 2953 import javax.mail.Session;
12     import javax.mail.Transport;
13     import javax.mail.internet.InternetAddress;
14 torben 3043 import javax.mail.internet.MimeBodyPart;
15 torben 2953 import javax.mail.internet.MimeMessage;
16 torben 3043 import javax.mail.internet.MimeMultipart;
17 torben 2953
18     import com.google.common.base.Splitter;
19    
20     import dk.daoas.adressevedligehold.tasks.TaskLogger;
21    
22     public class MailSender {
23    
24     private static TaskLogger logger = TaskLogger.getInstance();
25    
26     public static void sendMail(String subject, String htmlBody) {
27     ServiceConfig config = ServiceConfig.getInstance();
28    
29    
30 torben 3078 String mailStr = config.emails.replace(';', ',');//Tag højde for at der kan være brugt semikolon
31 torben 2953 List<String> mails = Splitter.on(',')
32     .trimResults()
33     .omitEmptyStrings()
34 torben 3078 .splitToList( mailStr );
35 torben 2953
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