// @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(); } }