/[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 3043 - (hide annotations) (download)
Wed Jun 8 15:06:45 2016 UTC (8 years ago) by torben
File size: 3118 byte(s)
Add code for sending mail with attachment
1 torben 2953 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 torben 3043 import javax.mail.Multipart;
9 torben 2953 import javax.mail.Session;
10     import javax.mail.Transport;
11     import javax.mail.internet.InternetAddress;
12 torben 3043 import javax.mail.internet.MimeBodyPart;
13 torben 2953 import javax.mail.internet.MimeMessage;
14 torben 3043 import javax.mail.internet.MimeMultipart;
15 torben 2953
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 torben 3043
53 torben 2953 Transport.send(msg);
54    
55    
56     } catch (MessagingException e) {
57     logger.warning("Unable to send report mail ", e );
58     }
59     }
60 torben 3043
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);
105    
106    
107     } catch (MessagingException e) {
108     logger.warning("Unable to send report mail ", e );
109     }
110     }
111 torben 2953 }

  ViewVC Help
Powered by ViewVC 1.1.20