发送邮件时无法向SMTP主机发送命令。

问题描述 投票:0回答:1

当我试图发送邮件时,它正在连接到smtp服务器,但随后我得到了 "无法发送命令到SMTP主机 "的错误。以下是启用调试后的日志。

DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtxxxxxxxxxaclecloud.com", port 25, isSSL false
220 smtpcxxxxx.net ESMTP smtp-in
DEBUG SMTP: connected to host "smxxxxxxxxxxxecloud.com", port: 25

EHLO RAXXXA-IN
250-smtxxxxxxdynback.net
250-STARTTLS
250 Ok
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "Ok", arg ""
STARTTLS
220 Ready to start TLS
EHLO RAJXXHA-IN
The email was not sent.
Error message: Can't send command to SMTP host

下面是我的代码片段。

Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);

//props.put("mail.smtp.ssl.enable", "true"); //the default value is false if not set
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.auth.login.disable", "true");  //the default authorization order is "LOGIN PLAIN DIGEST-MD5 NTLM". 'LOGIN' must be disabled since Email Delivery authorizes as 'PLAIN'
props.put("mail.smtp.starttls.enable", "true");   //TLSv1.2 is required
props.put("mail.smtp.starttls.required", "true");  //Oracle Cloud Infrastructure required
try {
    // 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, 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.

    System.out.println("Sending Email now...standby...");

    // Connect to OCI Email Delivery using the SMTP credentials specified.
    transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);

    // Send 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());
}
java javamail
1个回答
0
投票

你没有在 "props "对象中设置HOST变量。

Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");

在这里加上这一行。-

props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
//props.put("mail.smtp.ssl.enable", "true"); //the default value is false if not set
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.auth.login.disable", "true");  //the default authorization order is "LOGIN PLAIN DIGEST-MD5 NTLM". 'LOGIN' must be disabled since Email Delivery authorizes as 'PLAIN'
props.put("mail.smtp.starttls.enable", "true");   //TLSv1.2 is required
props.put("mail.smtp.starttls.required", "true");  //Oracle Cloud Infrastructure required
try {
    // 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, 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.

    System.out.println("Sending Email now...standby...");

    // Connect to OCI Email Delivery using the SMTP credentials specified.
    transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);

    // Send 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());
}

希望对大家有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.