package dk.thoerup.circuitbreaker.notification; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.logging.*; import dk.thoerup.circuitbreaker.CircuitBreaker; public class MailNotifier extends AsyncNotifier { Logger logger = Logger.getLogger(MailNotifier.class.getName()); private String recipient; private String from; Properties props; public MailNotifier(CircuitBreaker cb, String from, String recipient, String mailhost) { super(cb); this.from = from; this.recipient = recipient; props = new Properties(); props.put("mail.smtp.host", mailhost); } @Override public void sendAsync(String breakerName, Event evnt) { try { // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(true); // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom( new InternetAddress(from) ); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient) ); msg.setSubject("Circuitbreaker " + breakerName + " : " + evnt.toString()); msg.setText("--"); Transport.send(msg); } catch (Exception e) { logger.warning("Unable to send CircuitBreaker notification mail " + e ); } } }