我试图使用Java通过Exchange (Office 365)发送邮件。以下是我的代码:
package com.package;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class Mail {
ResourceBundle rb = ResourceBundle.getBundle("settings", Locale.ENGLISH);
public void sendMail(String body, String subject, String receipients) throws MessagingException ////this is used to send the emails
{
Message message = new MimeMessage(getSession());
message.addRecipient(RecipientType.TO, new InternetAddress(receipients));
message.addFrom(new InternetAddress[] { new InternetAddress(rb.getString("from")) });
message.setSubject(subject);
message.setContent(body, "text/plain");
Transport.send(message);
}
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = new Properties();
System.out.println("Submitter : " + authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable","true");
properties.setProperty("mail.smtp.host", "smtp.office365.com");
properties.setProperty("mail.smtp.port", "587");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
String username, password;
public Authenticator() {
username = rb.getString("from");
password = rb.getString("password");
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args) {
try {
new Mail().sendMail("Testing Mail", "Test", "someuser@ymail.com");
System.out.println("Mail sent");
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.out.println("Unable to send mail");
e.printStackTrace();
}
}
}
当我运行这个程序时,控制台显示的是: :
>>>>>Sending data EHLO HP-WIN8<<<<<<
>>>>>Sending data STARTTLS<<<<<<
>>>>>Sending data EHLO HP-WIN8<<<<<<
>>>>>Sending data AUTH LOGIN<<<<<<
>>>>>Sending data ImFzaGZhcS5tZW1vbkBzdHJlZWJvLmNvbSI=<<<<<<
>>>>>Sending data IlN1bmlAMTk5MSI=<<<<<<
>>>>>Sending data QUIT<<<<<<
Unable to send mail
javax.mail.SendFailedException: Send failure (javax.mail.AuthenticationFailedException: Error authenticating with server)
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
at com.streebo.Mail.sendMail(Mail.java:33)
at com.streebo.Mail.main(Mail.java:66)
Caused by: javax.mail.AuthenticationFailedException: Error authenticating with server
at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:402)
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Service.connect(Service.java:85)
at javax.mail.Service.connect(Service.java:70)
at javax.mail.Transport.send(Transport.java:94)
... 3 more
我已经检查了我的邮件id和密码,他们是正确的,但我仍然得到这个例外。
请检查一下我的程序是否有问题。
发布于 2014-06-15 22:36:47
由于我在使用创建的gmail.com测试帐户时遇到了同样的问题(如果使用了正确的凭据),一个可能的原因是
您可能使用gmail / yahoo / hotmail托管/创建的电子邮件进行测试。
如果您获得了javax.mail.AuthenticationFailedException,尝试使用凭据登录到邮件帐户,gmail要求您键入captcha。成功登录后,您可以使用Java发送邮件。
服务提供商这样做是为了防止垃圾邮件发送者,对于测试,我们创建了一个帐户,而不将其用于任何其他事情。
发布于 2014-04-07 12:32:33
您的资源包是否来自属性文件?您的密码是否有任何特殊字符,可以改变从属性文件读取密码时的解释方式?你有没有打印出你的密码以确保密码在你的程序中是正确的?
根据调试输出,您似乎使用的是JavaMail的GNU版本。请尝试使用JavaMail参考实现代替。
https://stackoverflow.com/questions/22905845
复制相似问题