/[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 3068 by torben, Thu Jul 21 11:48:19 2016 UTC revision 3069 by torben, Fri Jul 22 09:48:05 2016 UTC
# Line 1  Line 1 
1  package dk.daoas.adressevedligehold;  package dk.daoas.adressevedligehold;
2    
3  import java.util.List;  import java.util.List;
4    import java.util.Map;
5    import java.util.Map.Entry;
6  import java.util.Properties;  import java.util.Properties;
7    import java.util.Set;
8    
9  import javax.mail.Message;  import javax.mail.Message;
10  import javax.mail.MessagingException;  import javax.mail.MessagingException;
# Line 104  public class MailSender { Line 107  public class MailSender {
107    
108                          Transport.send(msg);                          Transport.send(msg);
109    
110                    } catch (MessagingException e) {
111                            logger.warning("Unable to send report mail ", e );
112                    }              
113            }
114            
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) {                  } catch (MessagingException e) {
163                          logger.warning("Unable to send report mail ", e );                          logger.warning("Unable to send report mail ", e );
164                  }                                }              

Legend:
Removed from v.3068  
changed lines
  Added in v.3069

  ViewVC Help
Powered by ViewVC 1.1.20