package dk.daoas.adressevedligehold; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.google.common.base.Splitter; import dk.daoas.adressevedligehold.tasks.TaskLogger; public class MailSender { private static TaskLogger logger = TaskLogger.getInstance(); public static void sendMail(String subject, String htmlBody) { ServiceConfig config = ServiceConfig.getInstance(); List mails = Splitter.on(',') .trimResults() .omitEmptyStrings() .splitToList( config.emails ); Properties props = new Properties(); props.put("mail.smtp.host", "mail.dao.int"); try { // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(false); MimeMessage msg = new MimeMessage(session); msg.setFrom( new InternetAddress("no-reply@daoas.dk") ); for (String recipient : mails) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) ); } msg.setSubject( subject ); msg.setContent(htmlBody, "text/html; charset=utf-8"); Transport.send(msg); } catch (MessagingException e) { logger.warning("Unable to send report mail ", e ); } } public static void sendMailWithAttachment(String subject, String htmlBody, String attachmentName, String attachmentBody) { ServiceConfig config = ServiceConfig.getInstance(); List mails = Splitter.on(',') .trimResults() .omitEmptyStrings() .splitToList( config.emails ); Properties props = new Properties(); props.put("mail.smtp.host", "mail.dao.int"); try { // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(false); MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); msg.setFrom( new InternetAddress("no-reply@daoas.dk") ); for (String recipient : mails) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) ); } msg.setSubject( subject ); MimeBodyPart body = new MimeBodyPart(); body.setContent(htmlBody, "text/html; charset=utf-8"); multipart.addBodyPart(body); MimeBodyPart attachPart = new MimeBodyPart(); attachPart.setText(attachmentBody, "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file); attachPart.setFileName(attachmentName); multipart.addBodyPart(attachPart); msg.setContent(multipart); Transport.send(msg); } catch (MessagingException e) { logger.warning("Unable to send report mail ", e ); } } public static void sendMailWithAttachments(String subject, String htmlBody, Map attachments) { ServiceConfig config = ServiceConfig.getInstance(); List mails = Splitter.on(',') .trimResults() .omitEmptyStrings() .splitToList( config.emails ); Properties props = new Properties(); props.put("mail.smtp.host", "mail.dao.int"); try { // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(false); MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); msg.setFrom( new InternetAddress("no-reply@daoas.dk") ); for (String recipient : mails) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient ) ); } msg.setSubject( subject ); MimeBodyPart body = new MimeBodyPart(); body.setContent(htmlBody, "text/html; charset=utf-8"); multipart.addBodyPart(body); for( Entry attachment : attachments.entrySet() ) { MimeBodyPart attachPart = new MimeBodyPart(); attachPart.setFileName(attachment.getKey()); attachPart.setText( attachment.getValue(), "ISO-8859-1");//kunne ellers laves med attachPart.attachFile(file); multipart.addBodyPart(attachPart); } msg.setContent(multipart); Transport.send(msg); } catch (MessagingException e) { logger.warning("Unable to send report mail ", e ); } } }