Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用JavaMail和使用SMTP的Amazon SES发送邮件_Java_Email_Amazon Web Services_Smtp_Jakarta Mail - Fatal编程技术网

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

无法使用JavaMail和使用SMTP的Amazon SES发送邮件,java,email,amazon-web-services,smtp,jakarta-mail,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; publ

我正在尝试使用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超时:

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
我错过了一些东西,我不知道是什么

非常感谢您的帮助,您能试试吗

System.setProperty("line.separator", "\r\n");
在将属性设置为符合

要求后,您可以尝试

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

在将属性设置为符合421客户端表达式的要求之后,我遇到了同样的问题。我通过验证传输是否已连接来解决此问题:

 if (!transport.isConnected())//make sure the connection is alive
        transport.connect();
 transport.sendMessage(message, message.getAllRecipients());

对于421客户端,我也有同样的问题。我通过验证传输是否已连接来解决此问题:

 if (!transport.isConnected())//make sure the connection is alive
        transport.connect();
 transport.sendMessage(message, message.getAllRecipients());

这应该没有必要。JavaMail确保在使用SMTP发送时,行以CRLF结尾。重新检查后,问题似乎只在我的专用开发计算机(OSX机器)上发生。在Ubuntu上,它使用或不使用:System.setPropertyline.separator,\r\n;这也可能是一个网络问题,因为Ubuntu测试服务器是一个AWS服务器。这是不必要的。JavaMail确保在使用SMTP发送时,行以CRLF结尾。重新检查后,问题似乎只在我的专用开发计算机(OSX机器)上发生。在Ubuntu上,它使用或不使用:System.setPropertyline.separator,\r\n;这也可能是网络问题,因为Ubuntu测试服务器是AWS服务器。亚马逊SMTP可能有非常严格的超时要求,以防止客户端浪费连接。也许您的应用程序或虚拟机正遭受调度延迟的困扰,从而使其无法快速响应。您可能需要升级到。这将允许您对调试输出使用java.util.logging,这将允许您获取带有时间戳的日志条目,可能会显示延迟的位置。Amazon SMTP可能有非常严格的超时要求,以防止客户端浪费连接。也许您的应用程序或虚拟机正遭受调度延迟的困扰,从而使其无法快速响应。您可能需要升级到。这将允许您对调试输出使用java.util.logging,这将允许您获取带有时间戳的日志条目,可能会显示延迟的位置。