/** * Convenience method to create a message associated with the session ses. If success is true, * String reasonFailed is ignored. If success is false and reasonFailed is null, it will be set to * "Unknown". * * @param ses * @param time * @param attempt * @param result * @return * @throws MessagingException * @throws MessagingException * @throws MessagingException * @throws AddressException */ private SMTPMessage createMessage( Session ses, Date time, boolean success, String result, String reasonFailed) throws MessagingException { SMTPMessage message = new SMTPMessage(ses); String tag; if (success) { tag = "[Success]"; } else { tag = "[Failed]"; } try { InternetAddress from = new InternetAddress(); from.setAddress(ses.getProperty("mail.smtp.from")); from.setPersonal("Hermes2 Admin"); message.setFrom(from); message.setSubject(tag + "Hermes2 housecleaning"); String theContent = "======== Report for Hermes2 Housecleaning ======== " + "\n" + "\n Hermes2 housecleaning has recently been invoked" + "\n Date: " + time + "\n Result: " + result; if (success) { message.setText(theContent); } else { if (reasonFailed == null) { reasonFailed = "Unknown"; } message.setText( theContent + "\n Reason for failure: " + reasonFailed + "\n\n" + " Please refer to the log files for more details."); } } catch (MessagingException e) { AdminError("Unable to create the message. Messaging Exception thrown."); stackTraceToLog(e); throw e; } catch (UnsupportedEncodingException e) { AdminError("Unsupported encoding in email 'from' name."); } return message; }
// @Test public void testRelayMta() throws Exception { Provisioning prov = Provisioning.getInstance(); Server server = prov.getLocalServer(); server.setShareNotificationMtaHostname("mta02.zimbra.com"); server.setShareNotificationMtaPort(25); server.setShareNotificationMtaAuthRequired(true); server.setShareNotificationMtaConnectionType(ShareNotificationMtaConnectionType.STARTTLS); server.setShareNotificationMtaAuthAccount("test-jylee"); server.setShareNotificationMtaAuthPassword("test123"); SMTPMessage out = new SMTPMessage(JMSession.getRelaySession()); InternetAddress address = new JavaMailInternetAddress("*****@*****.**"); out.setFrom(address); address = new JavaMailInternetAddress("*****@*****.**"); out.setRecipient(javax.mail.Message.RecipientType.TO, address); out.setSubject("test mail"); out.setText("hello world"); out.saveChanges(); ZimbraLog.smtp.setLevel(Level.trace); Transport.send(out); }
// TODO: make sure this handles SSL transport (useAuth is true) and regular public void sendAlert( short alertType, long dataCenterId, Long podId, Long clusterId, String subject, String content) throws MessagingException, UnsupportedEncodingException { AlertVO alert = null; if ((alertType != AlertManager.ALERT_TYPE_HOST) && (alertType != AlertManager.ALERT_TYPE_USERVM) && (alertType != AlertManager.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) && (alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) && (alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) { alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, clusterId); } if (alert == null) { // set up a new alert AlertVO newAlert = new AlertVO(); newAlert.setType(alertType); newAlert.setSubject(subject); newAlert.setClusterId(clusterId); newAlert.setPodId(podId); newAlert.setDataCenterId(dataCenterId); newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert newAlert.setLastSent(new Date()); _alertDao.persist(newAlert); } else { if (s_logger.isDebugEnabled()) { s_logger.debug( "Have already sent: " + alert.getSentCount() + " emails for alert type '" + alertType + "' -- skipping send email"); } return; } if (_smtpSession != null) { SMTPMessage msg = new SMTPMessage(_smtpSession); msg.setSender(new InternetAddress(_emailSender, _emailSender)); msg.setFrom(new InternetAddress(_emailSender, _emailSender)); for (InternetAddress address : _recipientList) { msg.addRecipient(RecipientType.TO, address); } msg.setSubject(subject); msg.setSentDate(new Date()); msg.setContent(content, "text/plain"); msg.saveChanges(); SMTPTransport smtpTrans = null; if (_smtpUseAuth) { smtpTrans = new SMTPSSLTransport( _smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); } else { smtpTrans = new SMTPTransport( _smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); } smtpTrans.connect(); smtpTrans.sendMessage(msg, msg.getAllRecipients()); smtpTrans.close(); } }