Search in sources :

Example 11 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project adempiere by adempiere.

the class EMail method getTransport.

/**
	 * Get Transport
	 * @param session
	 * @return
	 * @throws MessagingException
	 */
public Transport getTransport(Session session) throws MessagingException {
    Transport transport = null;
    if (getAuthMechanism().equals(MEMailConfig.AUTHMECHANISM_OAuth)) {
        transport = new SMTPTransport(session, null);
        transport.connect(getSmtpHost(), getPort(), m_user, "");
    } else {
        transport = session.getTransport("smtp");
        transport.connect();
    }
    //	Log
    log.fine("transport=" + transport);
    log.fine("transport connected");
    //	Default Return
    return transport;
}
Also used : Transport(javax.mail.Transport) SMTPTransport(com.sun.mail.smtp.SMTPTransport) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 12 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project opentest by mcdcorp.

the class SendEmailSmtp method run.

@Override
public void run() {
    String server = this.readStringArgument("server");
    String subject = this.readStringArgument("subject");
    String body = this.readStringArgument("body", null);
    String bodyHtml = this.readStringArgument("bodyHtml", null);
    String userName = this.readStringArgument("userName");
    String password = this.readStringArgument("password");
    String to = this.readStringArgument("to");
    Integer port = this.readIntArgument("port", 25);
    Boolean useTls = this.readBooleanArgument("useTls", true);
    String cc = this.readStringArgument("cc", null);
    String from = this.readStringArgument("from", "sendemail@getopentest.com");
    try {
        Properties prop = System.getProperties();
        prop.put("mail.smtp.host", server);
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.port", port.toString());
        Session session = Session.getInstance(prop, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        msg.setSubject(subject);
        if (bodyHtml != null) {
            msg.setContent(bodyHtml, "text/html");
        } else if (body != null) {
            msg.setText(body);
        } else {
            throw new RuntimeException("Can't send an email without a body. You must provide it " + "using the \"body\" or \"bodyHtml\" argument.");
        }
        msg.setSentDate(new Date());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        transport.setStartTLS(useTls);
        transport.connect(server, userName, password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    } catch (Exception exc) {
        throw new RuntimeException("Failed to send email", exc);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 13 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project greenmail by greenmail-mail-test.

the class SMTPCommandTest method mailSenderEmpty.

@Test
public void mailSenderEmpty() throws IOException, MessagingException {
    Session smtpSession = greenMail.getSmtp().createSession();
    try (SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL)) {
        // Closed by transport
        Socket smtpSocket = new Socket(hostAddress, port);
        smtpTransport.connect(smtpSocket);
        assertThat(smtpTransport.isConnected()).isTrue();
        smtpTransport.issueCommand("MAIL FROM: <>", -1);
        assertThat("250 OK").isEqualToNormalizingWhitespace(smtpTransport.getLastServerResponse());
    }
}
Also used : Socket(java.net.Socket) Session(jakarta.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 14 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project greenmail by greenmail-mail-test.

the class SMTPCommandTest method authLogin.

@Test
public void authLogin() throws IOException, MessagingException, UserException {
    Session smtpSession = greenMail.getSmtp().createSession();
    try (SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL)) {
        // Closed by transport
        Socket smtpSocket = new Socket(hostAddress, port);
        smtpTransport.connect(smtpSocket);
        assertThat(smtpTransport.isConnected()).isTrue();
        // Should fail, as user does not exist
        smtpTransport.issueCommand("AUTH LOGIN ", 334);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 VXNlcm5hbWU6");
        smtpTransport.issueCommand(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.US_ASCII)), -1);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 UGFzc3dvcmQ6");
        smtpTransport.issueCommand(Base64.getEncoder().encodeToString("testpass".getBytes(StandardCharsets.US_ASCII)), -1);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace(AuthCommand.AUTH_CREDENTIALS_INVALID);
        // Try again but create user
        greenMail.getUserManager().createUser("test@localhost", "test", "testpass");
        smtpTransport.issueCommand("AUTH LOGIN ", 334);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 VXNlcm5hbWU6");
        smtpTransport.issueCommand(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.US_ASCII)), -1);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace("334 UGFzc3dvcmQ6");
        smtpTransport.issueCommand(Base64.getEncoder().encodeToString("testpass".getBytes(StandardCharsets.US_ASCII)), -1);
        assertThat(smtpTransport.getLastServerResponse()).isEqualToNormalizingWhitespace(AuthCommand.AUTH_SUCCEDED);
    }
}
Also used : Socket(java.net.Socket) Session(jakarta.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 15 with SMTPTransport

use of com.sun.mail.smtp.SMTPTransport in project apollo by apolloconfig.

the class DefaultEmailService method send.

@Override
public void send(Email email) {
    if (!portalConfig.isEmailEnabled()) {
        return;
    }
    SMTPTransport t = null;
    try {
        Properties prop = System.getProperties();
        Session session = Session.getInstance(prop, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(email.getSenderEmailAddress()));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getRecipientsString(), false));
        msg.setSubject(email.getSubject());
        msg.setDataHandler(new DataHandler(new HTMLDataSource(email.getBody())));
        String host = portalConfig.emailConfigHost();
        String user = portalConfig.emailConfigUser();
        String password = portalConfig.emailConfigPassword();
        t = (SMTPTransport) session.getTransport("smtp");
        t.connect(host, user, password);
        msg.saveChanges();
        t.sendMessage(msg, msg.getAllRecipients());
        logger.debug("email response: {}", t.getLastServerResponse());
    } catch (Exception e) {
        logger.error("send email failed.", e);
        Tracer.logError("send email failed.", e);
    } finally {
        if (t != null) {
            try {
                t.close();
            } catch (Exception e) {
            // nothing
            }
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) IOException(java.io.IOException) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Session(javax.mail.Session)

Aggregations

SMTPTransport (com.sun.mail.smtp.SMTPTransport)16 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)9 MimeMessage (javax.mail.internet.MimeMessage)9 Properties (java.util.Properties)7 Message (javax.mail.Message)7 MessagingException (javax.mail.MessagingException)7 IOException (java.io.IOException)4 Date (java.util.Date)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 Session (jakarta.mail.Session)3 Socket (java.net.Socket)3 DataHandler (javax.activation.DataHandler)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Transport (javax.mail.Transport)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 Test (org.junit.Test)2 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)1 SMTPAddressSucceededException (com.sun.mail.smtp.SMTPAddressSucceededException)1