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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2953 by torben, Tue Feb 16 13:09:00 2016 UTC revision 3044 by torben, Wed Jun 8 15:31:27 2016 UTC
# Line 1  Line 1 
1  package dk.daoas.adressevedligehold;  package dk.daoas.adressevedligehold;
2    
3    import java.io.UnsupportedEncodingException;
4  import java.util.List;  import java.util.List;
5  import java.util.Properties;  import java.util.Properties;
6    
7  import javax.mail.Message;  import javax.mail.Message;
8  import javax.mail.MessagingException;  import javax.mail.MessagingException;
9    import javax.mail.Multipart;
10  import javax.mail.Session;  import javax.mail.Session;
11  import javax.mail.Transport;  import javax.mail.Transport;
12  import javax.mail.internet.InternetAddress;  import javax.mail.internet.InternetAddress;
13    import javax.mail.internet.MimeBodyPart;
14  import javax.mail.internet.MimeMessage;  import javax.mail.internet.MimeMessage;
15    import javax.mail.internet.MimeMultipart;
16    
17  import com.google.common.base.Splitter;  import com.google.common.base.Splitter;
18    
# Line 46  public class MailSender { Line 50  public class MailSender {
50                          msg.setSubject( subject );                                                msg.setSubject( subject );                      
51                          msg.setContent(htmlBody, "text/html; charset=utf-8");                          msg.setContent(htmlBody, "text/html; charset=utf-8");
52    
53    
54                          Transport.send(msg);                          Transport.send(msg);
55    
56    
57                  } catch (MessagingException e) {                  } catch (MessagingException e) {
58                          logger.warning("Unable to send report mail ", e );                          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  }  }

Legend:
Removed from v.2953  
changed lines
  Added in v.3044

  ViewVC Help
Powered by ViewVC 1.1.20