简单的Java邮件。无法在EHLO中设置本地主机名称(如何设置mail.smtp.localhost?)

时间:2019-02-24 04:21:07

标签: java javamail

我在程序中使用了简单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?

1 个答案:

答案 0 :(得分:1)

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

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

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

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