我最初将站点设置为使用本地 ISP 通过我的站点发送电子邮件。我想改变这一点,并开始通过我的 VPS 发送电子邮件。根据在线文档(来 self 的 vps 提供商),我有“POP before SMTP 身份验证”。那么,我此时不需要身份验证,也不需要安全连接。
我更改了用于通过本地 ISP 发送电子邮件的代码中的一些内容。这是我所拥有的:
Transport t = null;
try {
String Username = "mrsmith@mydomain.com";
String Password = "somepassword";
InternetAddress from = new InternetAddress("mrsmith@mydomain.com", "Bob Smith");
Properties props = new Properties();
//commented out, since I don't want to use authentication
//props.setProperty("mail.smtp.auth", "true");
props.put("mail.pop3.host", "mydomain.com";
props.put("mail.smtp.host", "mydomain.com");
String protocol = "smtp";
Session ssn = Session.getInstance(props, null);
ssn.setDebug(true);
t = ssn.getTransport(protocol);
Store s = ssn.getStore();
s.connect();
t.connect(SMTP,Username,Password);
//Create the message
Message msg = new MimeMessage(ssn);
msg.setFrom(from);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setContent(body, "text/html");
t.sendMessage(msg, msg.getAllRecipients());
t.close();
s.close();
我联系了我的 vps 提供商,按照他们的指示调整代码后,我仍然收到“身份验证失败”的消息。这是在端口 25 上成功连接到主机之后的情况。
有人可以指出我在这里缺少什么吗?
编辑以添加附加信息
添加 store.connect("mailhost",用户名,密码); 后对代码的声明,我比以前更进一步了!
这是打印到 tomcat 提示符的一些信息 -
DEBUG : getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc.] POP3: connecting to host "mydomain.com", port 110, isSSL false server ready OK User name accepted, password please Password somepassword OK Mailbox open, 0 messages DEBUG: geProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smpt.SMTPTransport,Sun Microsystems, Inc.] DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "mydomain.com", port 25, isSSL false 220 mydomain.com ESMTP Sendmail 8.14.3/8.14.3; DEC 2010 29 DEBUG SMTP: connected to host "mydomain.com", port: 25 ( a lot of additional debug information was here ) DEBUG SMTP: Attempt to authenticate AUTH LOGIN 535 5.7.0 authentication failed javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:319) at javax.mail.Service.connect(Service.java:169) ... continues with more 'at's.
您在这些信息中发现什么奇怪的地方吗?我不太明白为什么使用相同的用户名和密码对 Pop3 进行身份验证时身份验证失败。
最佳答案
POP before SMTP 身份验证基本上意味着您需要能够证明您可以使用 POP3(使用正确的身份验证)获取邮件,然后才能允许使用 SMTP(无需身份验证)发送邮件。
使用 Java Mail API,您可以像这样对 POP3 服务器进行身份验证:
Store store = ssn.getStore("pop3");
store.connect("mailhost", Username, Password);
您应该在连接到 SMTP 服务器之前执行这些调用,因此在连接到 SMTP 服务器(没有用户名和密码)之前,如下所示
t.connect();
确保您先连接到商店。
在您的代码片段中,当您提供用户名/密码时,您仍在对 smtp 进行身份验证。
另一个选择是使用 Commons Email,它内置了对 pop before smtp 身份验证的支持。查看http://commons.apache.org/email/apidocs/org/apache/commons/mail/Email.html中的以下方法
email.setPopBeforeSMTP(true,popHost,popUsername,popPassword);
使用公共(public)电子邮件,无需在代码中进行流行管道操作(发送前连接)。
关于JavaMail 通过 vps 发送电子邮件,不需要身份验证,但得到 AuthenticationFailedException - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4549736/