/[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 3044 by torben, Wed Jun 8 15:31:27 2016 UTC revision 3071 by torben, Fri Jul 22 09:56:28 2016 UTC
# Line 1  Line 1 
1  package dk.daoas.adressevedligehold;  package dk.daoas.adressevedligehold;
2    
 import java.io.UnsupportedEncodingException;  
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    
8  import javax.mail.Message;  import javax.mail.Message;
# Line 105  public class MailSender { Line 106  public class MailSender {
106    
107                          Transport.send(msg);                          Transport.send(msg);
108    
109                    } catch (MessagingException e) {
110                            logger.warning("Unable to send report mail ", e );
111                    }              
112            }
113            
114            public static void sendMailWithAttachments(String subject, String htmlBody, Map<String,String> attachments) {
115                    ServiceConfig config = ServiceConfig.getInstance();
116                    
117                    
118                    List<String> mails = Splitter.on(',')
119                                                                             .trimResults()
120                                                                             .omitEmptyStrings()
121                                                                             .splitToList( config.emails );
122    
123                    Properties props = new Properties();
124                    props.put("mail.smtp.host", "mail.dao.int");
125    
126                    try {
127                            // create some properties and get the default Session
128                            Session session = Session.getDefaultInstance(props, null);
129                            session.setDebug(false);
130    
131                            MimeMessage msg = new MimeMessage(session);
132                            Multipart multipart = new MimeMultipart();
133                            
134                            msg.setFrom( new InternetAddress("no-reply@daoas.dk") );
135    
136    
137                            for (String recipient : mails) {
138                                    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) );
139                            }
140                            
141                            msg.setSubject( subject );      
142                            
143                            
144                            MimeBodyPart body = new MimeBodyPart();                
145                            body.setContent(htmlBody, "text/html; charset=utf-8");
146                            multipart.addBodyPart(body);
147                            
148                            for( Entry<String,String> attachment : attachments.entrySet() ) {
149                                    MimeBodyPart attachPart = new MimeBodyPart();
150                                    attachPart.setFileName(attachment.getKey());
151                                    attachPart.setText( attachment.getValue(), "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file);
152                                                            
153                                    
154                                    multipart.addBodyPart(attachPart);
155                            }
156                                                                            
157                            msg.setContent(multipart);
158    
159                            Transport.send(msg);
160    
161                  } catch (MessagingException e) {                  } catch (MessagingException e) {
162                          logger.warning("Unable to send report mail ", e );                          logger.warning("Unable to send report mail ", e );
163                  }                                }              

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

  ViewVC Help
Powered by ViewVC 1.1.20