Не удается отправить команду на SMTP-хост при отправке почты - PullRequest
0 голосов
/ 26 мая 2020

При попытке отправить почту он подключается к 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());
}

1 Ответ

0 голосов
/ 14 июня 2020

Вы не установили переменную HOST в объекте «props»

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

Надеюсь, это поможет.

...