Search in sources :

Example 1 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project GNS by MobilityFirst.

the class Email method simpleMail.

/**
   *
   * @param subject
   * @param recipient
   * @param text
   * @param suppressWarning
   * @return true if the mail was sent
   */
public static boolean simpleMail(String subject, String recipient, String text, boolean suppressWarning) {
    try {
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        Properties props = new Properties();
        props.setProperty("mail.smtp.ssl.enable", "true");
        //props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
        message.setSubject(subject);
        message.setText(text);
        SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
        try {
            t.connect(SMTP_HOST, Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
            t.sendMessage(message, message.getAllRecipients());
            getLogger().log(Level.FINE, "Email response: {0}", t.getLastServerResponse());
        } finally {
            t.close();
        }
        getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
        return true;
    } catch (GeneralSecurityException | MessagingException e) {
        if (!suppressWarning) {
            getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
        }
        return false;
    }
}
Also used : MailSSLSocketFactory(com.sun.mail.util.MailSSLSocketFactory) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) Properties(java.util.Properties) Session(javax.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 2 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport 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)

Example 3 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project mxisd by kamax-io.

the class EmailSmtpConnector method send.

@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
    if (StringUtils.isBlank(senderAddress)) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    if (StringUtils.isBlank(content)) {
        throw new InternalServerError("Notification content is empty");
    }
    try {
        InternetAddress sender = new InternetAddress(senderAddress, senderName);
        MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
        // FIXME set version
        msg.setHeader("X-Mailer", "mxisd");
        msg.setSentDate(new Date());
        msg.setFrom(sender);
        msg.setRecipients(Message.RecipientType.TO, recipient);
        msg.saveChanges();
        log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        transport.setStartTLS(cfg.getTls() > 0);
        transport.setRequireStartTLS(cfg.getTls() > 1);
        log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
        transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
        try {
            transport.sendMessage(msg, InternetAddress.parse(recipient));
            log.info("Invite to {} was sent", recipient);
        } finally {
            transport.close();
        }
    } catch (UnsupportedEncodingException | MessagingException e) {
        throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) Date(java.util.Date) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 4 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project service-proxy by membrane.

the class EmailTokenProvider method sendEmail.

private void sendEmail(String sender, String recipient, String subject, String text) {
    try {
        Properties props = System.getProperties();
        props.put("mail.smtp.port", "" + smtpPort);
        props.put("mail.smtp.socketFactory.port", "" + smtpPort);
        if (ssl) {
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.starttls.required", "true");
        }
        if (smtpUser != null) {
            props.put("mail.smtp.auth", "true");
        }
        Session session = Session.getInstance(props, null);
        final MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(sender));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
        msg.setSubject(subject);
        msg.setText(text, Constants.UTF_8);
        msg.setSentDate(new Date());
        SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp");
        t.connect(smtpHost, smtpUser, smtpPassword);
        t.sendMessage(msg, msg.getAllRecipients());
        t.close();
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 5 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project cloudstack by apache.

the class SMTPMailSender method sendMail.

public void sendMail(SMTPMailProperties mailProps) {
    if (session == null) {
        logger.error("Unable to send mail due to null session.");
        return;
    }
    try {
        SMTPMessage message = createMessage(mailProps);
        if (ArrayUtils.isEmpty(message.getAllRecipients())) {
            logger.error(String.format("Unable to send mail [%s] due to the recipient list of the message being empty.", mailProps.getSubject()));
            return;
        }
        SMTPTransport smtpTrans = createSmtpTransport();
        smtpTrans.connect();
        smtpTrans.sendMessage(message, message.getAllRecipients());
        smtpTrans.close();
    } catch (MessagingException | UnsupportedEncodingException ex) {
        logger.error(String.format("Unable to send mail [%s] to the recipcients [%s].", mailProps.getSubject(), mailProps.getRecipients().toString()), ex);
    }
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Aggregations

SMTPTransport (com.sun.mail.smtp.SMTPTransport)9 MessagingException (javax.mail.MessagingException)7 Session (javax.mail.Session)5 MimeMessage (javax.mail.internet.MimeMessage)5 InternetAddress (javax.mail.internet.InternetAddress)4 Properties (java.util.Properties)3 Message (javax.mail.Message)3 Transport (javax.mail.Transport)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 SMTPMessage (com.sun.mail.smtp.SMTPMessage)1 SMTPSSLTransport (com.sun.mail.smtp.SMTPSSLTransport)1 MailSSLSocketFactory (com.sun.mail.util.MailSSLSocketFactory)1 ServiceException (com.zimbra.common.service.ServiceException)1 DataImport (com.zimbra.cs.account.DataSource.DataImport)1 SmtpTransport (com.zimbra.cs.mailclient.smtp.SmtpTransport)1 JMSession (com.zimbra.cs.util.JMSession)1 FeatureNotAvailable (io.kamax.mxisd.exception.FeatureNotAvailable)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 GeneralSecurityException (java.security.GeneralSecurityException)1