我在我的程序中使用Simple Java Mail库(http://www.simplejavamail.org),我想设置一个localhost名称,当我发送电子邮件时应该在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.localhost
或mail.smtps.localaddress
。否则,可以使用mail.smtp.localhost
或mail.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;
}