简单 Java 邮件。无法在 EHLO 中设置本地主机名 (How to set mail.smtp.localhost?)

Simple Java Mail. Can't set the localhost name in EHLO (How to set mail.smtp.localhost?)

我在我的程序中使用简单 Java 邮件库 (http://www.simplejavamail.org),我想设置一个本地主机名,当我发送电子邮件时,它应该在 EHLO 中发送。我试着这样做:

Properties props = new Properties();
props.put("mail.smtp.sendpartial", "true");
props.put("mail.smtp.host", "smtp.mail.ru");
props.put("mail.smtp.localhost", "mail.ru");

Mailer mailer = MailerBuilder
        .withSMTPServer(server, port, login, password)
        .withTransportStrategy(TransportStrategy.SMTPS)
        .withSessionTimeout(10 * 1000)
        .clearEmailAddressCriteria()
        .withProperties(props)
        .withDebugLogging(true)
        .buildMailer();

正如您在代码中看到的那样,我现在应该在 EHLO 中包含 "mail.ru",但它并没有发生,我一直保留着我计算机的名称。我在日志中看到以下内容:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.ru", port 465, isSSL true
220 smtp46.i.mail.ru ESMTP ready (Looking for Mail for your domain? Visit https://biz.mail.ru)
DEBUG SMTP: connected to host "smtp.mail.ru", port: 465

EHLO MyComputersName
250-smtp46.i.mail.ru
250-SIZE 73400320
250-8BITMIME
250-PIPELINING
250 AUTH PLAIN LOGIN XOAUTH2
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2"
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded

我做错了什么?如何设置mail.smtp.localhost?

如果启用了 SSL,请尝试 mail.smtps.localhostmail.smtps.localaddress。否则,可以使用 mail.smtp.localhostmail.smtp.localaddress

在你的情况下使用 TransportStrategy.SMTPS,这意味着 SSL 已明确启用,因此普通连接的 mail.smtp.localhost 将不起作用。

下面是我的参考[从 SMTPTransport class 文件粘贴]:

public synchronized String getLocalHost() {
        if(this.localHostName == null || this.localHostName.length() <= 0) {
            ***this.localHostName = this.session.getProperty("mail." + this.name + ".localhost");***
        }

        if(this.localHostName == null || this.localHostName.length() <= 0) {
            ***this.localHostName = this.session.getProperty("mail." + this.name + ".localaddress");***
        }

        InetAddress localHost;
        try {
            if(this.localHostName == null || this.localHostName.length() <= 0) {
                localHost = InetAddress.getLocalHost();
                this.localHostName = localHost.getCanonicalHostName();
                if(this.localHostName == null) {
                    this.localHostName = "[" + localHost.getHostAddress() + "]";
                }
            }
        } catch (UnknownHostException var2) {
            ;
        }

        if((this.localHostName == null || this.localHostName.length() <= 0) && this.serverSocket != null && this.serverSocket.isBound()) {
            localHost = this.serverSocket.getLocalAddress();
            this.localHostName = localHost.getCanonicalHostName();
            if(this.localHostName == null) {
                this.localHostName = "[" + localHost.getHostAddress() + "]";
            }
        }

        return this.localHostName;
}