【问题标题】:Cannot send SMTP email from JBoss java app in Amazon EC2 instance: Could not convert socket to TLS无法从 Amazon EC2 实例中的 JBoss java 应用程序发送 SMTP 电子邮件:无法将套接字转换为 TLS
【发布时间】:2021-12-24 18:12:05
【问题描述】:

我在 Amazon Web Services red hat 8 EC2 实例的 JBoss 6.4.0 中运行 java 应用程序。 当我的应用尝试通过 javax.mail 发送电子邮件时,我收到错误消息“无法将套接字转换为 TLS”。

然后我编写了 AmazonSESSample.java 示例程序并进行了尝试。我在 JBoss 外部的 EC2 实例中运行它,它运行成功。 (AmazonSESSample 程序可以在这里找到:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-smtp.html

然后我注释掉了我的 java 应用程序中的电子邮件代码,并将其替换为 AmazonSESSample.java 中的代码。当我在 JBoss 中使用 AmazonSESSample 代码运行我的 java 应用程序时,我得到了同样的错误:“无法将套接字转换为 TLS”。所以 AmazonSESSample 在 JBoss 之外运行良好,在 JBoss 内部运行时会报错。

这是我的应用程序中的 AmazonSESSample 代码。有人可以帮我解决“无法将套接字转换为 TLS”的错误吗?:

public class AmazonSESSample {
private static final Logger logger = LogManager.getFormatterLogger("AmazonSESSample");

// Replace sender@example.com with your "From" address.
// This address must be verified.
static final String FROM = "email1@gmail.com";
static final String FROMNAME = "Steve";

// Replace recipient@example.com with a "To" address. If your account 
// is still in the sandbox, this address must be verified.
static final String TO = "email2@gmail.com";

// Replace smtp_username with your Amazon SES SMTP user name.
static final String SMTP_USERNAME = "thisIsNotActualghijikl";

// Replace smtp_password with your Amazon SES SMTP password.
static final String SMTP_PASSWORD = "abcdefThisIsNotActual";

// Amazon SES SMTP host name. This example uses the US West (Oregon) region.
// See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
// for more information.
static final String HOST = "email-smtp.us-east-2.amazonaws.com"; 

// The port you will connect to on the Amazon SES SMTP endpoint. 
static final int PORT = 587;

static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";

static final String BODY = String.join(
        System.getProperty("line.separator"),
        "<h1>Amazon SES SMTP Email Test</h1>",
        "<p>This email was sent with Amazon SES using the ", 
        "<a href='https://github.com/javaee/javamail'>Javamail Package</a>",
        " for <a href='https://www.java.com'>Java</a>."
    );

public int sendEmail(DisplayEmailMessage emailMessage) throws UnsupportedEncodingException, MessagingException {
     // Create a Properties object to contain connection configuration information.
    Properties props = System.getProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.port", PORT); 
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");

    // Create a Session object to represent a mail session with the specified properties. 
    Session session = Session.getDefaultInstance(props);

    // Create a message with the specified information. 
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(FROM, FROMNAME));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
    msg.setSubject(SUBJECT);
    msg.setContent(BODY, "text/html");
        
    // Create a transport.
    Transport transport = session.getTransport();
                
    // Send the message.
    try {
        System.out.println("Sending...");
        
        // Connect to Amazon SES using the SMTP username and password you specified above.
        transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
        
        // Send the email.
        transport.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Email sent!");
    }
    catch (Exception ex) {
        System.out.println("The email was not sent.");
        System.out.println("Error message: " + ex.getMessage());
    }
    finally {
        // Close and terminate the connection.
        transport.close();
    }
    return 0;
}

}

这里是 javamail 调试输出: DEBUG: setDebug: JavaMail version 1.4.5.redhat-2 Sending email to 123@gmail.com DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] Starting to connect at Sun Dec 26 13:14:23 UTC 2021 to email 123@gmail.com DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.dreamhost.com", port 587, isSSL false 220 pdx1-sub0-mail-a290.dreamhost.com ESMTP DEBUG SMTP: connected to host "smtp.dreamhost.com", port: 587

EHLO ip-172-31-29-30.us-east-2.compute.internal 250-pdx1-sub0-mail-a290.dreamhost.com 250-流水线 250 尺寸 40960000 250-ETRN 250-STARTTLS 250-AUTH 普通登录 250-AUTH=普通登录 250 增强状态代码 250-8BITMIME 第250章 调试 SMTP:找到扩展“管道”,arg“” 调试 SMTP:找到扩展名“SIZE”,arg“40960000” 调试 SMTP:找到扩展名“ETRN”,arg“” 调试 SMTP:找到扩展“STARTTLS”,arg“” DEBUG SMTP:找到扩展名“AUTH”,arg“PLAIN LOGIN” 调试 SMTP:找到扩展名“AUTH=PLAIN”,arg“LOGIN” 调试 SMTP:找到扩展“ENHANCEDSTATUSCODES”,arg“” 调试 SMTP:找到扩展名“8BITMIME”,arg“” 调试 SMTP:找到扩展名“CHUNKING”,arg“” 启动TLS 220 2.0.0 准备启动 TLS 消息异常 javax.mail.MessagingException:无法将套接字转换为 TLS

【问题讨论】:

  • 我可以在我的 mac(AWS 外部)上运行 JBoss 中的代码,它可以正常发送电子邮件。当我在 JBoss 的 AWS 中运行它时,我得到了错误。我也可以在我的 Mac 和 JBoss 之外的 AWS 上将它作为独立的测试程序运行,并且运行良好。
  • 这里是 javamail 调试输出:

标签: amazon-web-services amazon-ec2 jboss jakarta-mail redhat


【解决方案1】:

我通过将 JBoss 升级到 7.4.0 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2013-06-22
    • 2015-04-23
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多