java - 无法使用 SMTP 使用 JavaMail 和 Amazon SES 发送邮件

标签 java email amazon-web-services smtp jakarta-mail

我正在尝试使用 JavaMail (1.4.7) 通过 Amazon SMTP 发送电子邮件。

这是我的代码

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class AmazonSESSample {

        static final String FROM = "noreply@corp.com";
        static final String TO = "me@corp.com";

        static final String BODY = "this is the body";
        static final String SUBJECT = "hello test";
        static final String USER = "...";
        static final String PASS = "...";

        // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use
        // STARTTLS to encrypt the connection.
        static final int PORT = 465;
        static final String PROTOCOL = "smtps";
        static final String HOST = "email-smtp.us-west-2.amazonaws.com";

        public static void main(String[] args) throws Exception {

                // Create a Properties object to contain connection configuration information.
                Properties props = System.getProperties();
                props.put("mail.transport.protocol", PROTOCOL);
                props.put("mail.smtps.host", HOST);
                props.put("mail.smtps.port", PORT); 
                props.put("mail.smtps.user", FROM);
                props.put("mail.debug","true");

                // Set properties indicating that we want to use STARTTLS to encrypt the connection.
                // The SMTP session will begin on an unencrypted connection, and then the client
                // will issue a STARTTLS command to upgrade to an encrypted connection.
//        props.put("mail.smtps.auth", "true");
//        props.put("mail.smtp.starttls.enable", "true");
//        props.put("mail.smtp.starttls.required", "true");
//        props.put("mail.smtps.ssl.enable", "true");

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

                // Create a message with the specified information. 
                MimeMessage msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(FROM));
                msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
                msg.setSubject(SUBJECT);
                msg.setContent(BODY,"text/plain");
                //msg.saveChanges();

                // Create a transport.        
                Transport transport = session.getTransport("smtps");

                // Send the message.
                try
                {
                        System.out.println("connecting...");

                        // Connect to Amazon SES using the SMTP username and password you specified above.
                        transport.connect(HOST, USER, PASS);
                        System.out.println("connected");
                        // Send the email.
                        System.out.println("sending...");
                        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();          
                }
        }
}

我偶尔会遇到两种类型的错误。

有时我会收到“421 Timeout waiting for data from client”:

DEBUG: JavaMail version 1.4.7
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
connecting...
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062959777 MTyV0IkyBqUpkEgbX2Av
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465

EHLO 10.8.1.217
250-email-smtp.amazonaws.com
250-8BITMIME
250-SIZE 10485760
250-AUTH PLAIN LOGIN
250 Ok
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10485760"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "Ok", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
connected
sending...
DEBUG SMTP: use8bit false
MAIL FROM:<noreply@pingidentity.com>
250 Ok
RCPT TO:<adavid@pingidentity.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   adavid@pingidentity.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: noreply@pingidentity.com
To: adavid@pingidentity.com
Message-ID: <1653497584.0.1432736817561.JavaMail.javamailuser@localhost>
Subject: hello test
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

this is the body
.
421 Timeout waiting for data from client.
DEBUG SMTP: got response code 421, with response: 421 Timeout waiting for data from client.

RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 421 Timeout waiting for data from client.

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1889)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
    at AmazonSESSample.main(AmazonSESSample.java:68)
The email was not sent.
Error message: 421 Timeout waiting for data from client.

QUIT
Exception in thread "main" javax.mail.MessagingException: Can't send command to SMTP host;
    nested exception is:
    java.net.SocketException: Connection closed by remote host
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2157)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2144)
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1210)
    at AmazonSESSample.main(AmazonSESSample.java:79)
Caused by: java.net.SocketException: Connection closed by remote host
    at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1490)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:70)
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:128)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2155)
    ... 3 more

有时我会得到“EOF:[EOF]”:

    DEBUG: JavaMail version 1.4.7
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
connecting...
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062893701 2HRiJbNHu9DYCQClBXLu
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465

EHLO 10.8.1.217
421 Timeout waiting for data from client.
HELO 10.8.1.217
DEBUG SMTP: EOF: [EOF]
The email was not sent.
Error message: [EOF]

我错过了一些东西,我不知道是什么......

将不胜感激

最佳答案

你能试试吗

System.setProperty("line.separator", "\r\n");

在将您的属性设置为符合 RFC 822 之后

关于java - 无法使用 SMTP 使用 JavaMail 和 Amazon SES 发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485886/

相关文章:

javascript - 将 aws-sdk 与 amplify 和 React 结合使用

java - Rijndael 实现中的输出长度异常

java - 如何使用Xpath选择特定容器的span元素(<div>由CSS类定义)?

php - 如何从特定的 ip 地址发送电子邮件?

html - 为什么 GMail 使用引用可打印编码打破带有缩小标记/HTML 的电子邮件的内联 (CSS) 样式?

linux - 按主题内容对邮件进行排序

api - AutoScaleGroup 中的 AWS EC2 CloudWatch Mem Util 和 Disk Util

java - 使用正则表达式解析log4j日志文件

java - 在android中为TextView添加发光

amazon-web-services - 将 SSD 存储添加到 AWS EC2 保留实例