javax.mail.AuthenticationFailedException:使用服务器进行身份验证时出错

时间:2014-04-07 07:17:08

标签: java email javamail

我正在尝试使用Java Mail API通过Exchange Server(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和密码并且它们是正确的,但我仍然得到这个例外。

请检查我的程序是否有问题。

2 个答案:

答案 0 :(得分:1)

由于我在使用测试帐户创建gmail.com时遇到了同样的问题(提供了正确的凭据),一个可能的原因可能是

您可能使用了使用gmail / yahoo / hotmail托管/创建的电子邮件进行测试。

如果您收到javax.mail.AuthenticationFailedException,请尝试使用凭据登录邮件帐户,gmail会要求您键入验证码。成功登录后,您可以使用Java mail API发送邮件。

服务提供商这样做是为了防止垃圾邮件发送者,因为测试我们创建了一个帐户,并且不会将其用于其他任何事情。

答案 1 :(得分:0)

您的资源包是否来自属性文件?您的密码是否有任何特殊字符可能会改变从属性文件中读取时的解释方式?您是否打印出密码以确保其程序正确无误?

根据调试输出,您似乎正在使用GNU版本的JavaMail。请尝试改为使用JavaMail reference implementation