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

标签 java jakarta-mail

尝试发送邮件时,它正在连接到 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());
}

最佳答案

您没有在“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());
}

我希望这会有所帮助。

关于java - 发送邮件时无法向 SMTP 主机发送命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62020882/

相关文章:

java - 在 Clojure 中 : Error executing a Java call with let inside a function but not in REPL

java - Wicket 口中的日历

java - 如何在 Hazelcast 中完成分区

java - 根据正则表达式拆分字符串

maven - 依赖 javax.mail :mail:1. 4 未找到

java - 不同内容的批量邮件

java - 通过javamail连接错误

java - 使用 Java 的 Kinect v2

java - 读取javaMail中所有未读消息/最近消息

Java邮件 : MessagingException: Got bad greeting from SMTP host — Error Code 250