use of com.sun.mail.smtp.SMTPSSLTransport in project BIMserver by opensourceBIM.
the class EmailMessage method send.
public void send() throws MessagingException, UserException {
Properties props = new Properties();
ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
props.put("mail.smtp.localhost", "bimserver.org");
String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
props.put(smtpProps, serverSettings.getSmtpPort());
if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
Session mailSession = Session.getInstance(props);
Transport transport = null;
try {
if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTP) {
transport = new SMTPTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else {
throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
}
transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
Message message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(to, addressTo);
message.setContent(body, contentType);
message.setFrom(from);
transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
LOGGER.error("Error sending email " + body + " " + e.getMessage());
throw new UserException("Error sending email " + e.getMessage());
}
}
Aggregations