Search in sources :

Example 1 with SMTPSSLTransport

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());
    }
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) Properties(java.util.Properties) SMTPTransport(com.sun.mail.smtp.SMTPTransport) SMTPSSLTransport(com.sun.mail.smtp.SMTPSSLTransport) MimeMessage(javax.mail.internet.MimeMessage) ServerSettings(org.bimserver.models.store.ServerSettings) UserException(org.bimserver.shared.exceptions.UserException) Transport(javax.mail.Transport) SMTPSSLTransport(com.sun.mail.smtp.SMTPSSLTransport) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Session(javax.mail.Session)

Aggregations

SMTPSSLTransport (com.sun.mail.smtp.SMTPSSLTransport)1 SMTPTransport (com.sun.mail.smtp.SMTPTransport)1 Properties (java.util.Properties)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 URLName (javax.mail.URLName)1 MimeMessage (javax.mail.internet.MimeMessage)1 ServerSettings (org.bimserver.models.store.ServerSettings)1 UserException (org.bimserver.shared.exceptions.UserException)1