如何解决javax.mail.AuthenticationFailedException问题

时间:2014-02-15 10:05:24

标签: java javamail

这个程序试图发送电子邮件但是抛出一个运行时异常:AuthenticationFailedException我已经引用了stackoverflow命令并回答了我已经实现的同样的事情,但我仍然遇到这样的异常,任何人都可以解决这个问题。<登记/> 的异常

 javax.mail.AuthenticationFailedException
            at javax.mail.Service.connect(Service.java:267)
            at javax.mail.Service.connect(Service.java:137)
            at javax.mail.Service.connect(Service.java:86)
            at javax.mail.Transport.send0(Transport.java:150)
            at javax.mail.Transport.send(Transport.java:80)
            at com.treamis.transport.vehicle.javaMail.send(javaMail.java:81)
            at com.treamis.transport.vehicle.MysqlBackup.backupDataWithDatabase(Mysq
    lBackup.java:97)
            at com.treamis.transport.vehicle.MysqlBackup.run(MysqlBackup.java:118)
            at java.util.TimerThread.mainLoop(Timer.java:555)
            at java.util.TimerThread.run(Timer.java:505)
    Sms sent xl sheet is generated is generated

java邮件代码

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class javaMail {

    private String SMTP_PORT = "465";
    private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private String SMTP_HOST_NAME = "smtp.gmail.com";
    private Properties smtpProperties;

    public javaMail() {
        initProperties();
    }

    private void initProperties() {
        smtpProperties = new Properties();
        smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
        smtpProperties.put("mail.smtp.auth", "true");
        smtpProperties.put("mail.debug", "true");
        smtpProperties.put("mail.smtp.port", SMTP_PORT);
        smtpProperties.put("mail.smtp.socketFactory.port", SMTP_PORT);
        smtpProperties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        smtpProperties.put("mail.smtp.socketFactory.fallback", "false");
    }

    public String send(String[] to, final String from, final String pwd, String subject, String body) {
        javaMail tjm = new javaMail();
        try {
            Properties props = tjm.getSmtpProperties();
// -- Attaching to default Session, or we could start a new one --
//            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
//                protected PasswordAuthentication getPasswordAuthentication() {
//                    return new PasswordAuthentication(from, pwd);
//                }
//            });
            Session session = Session.getInstance(props, new GMailAuthenticator(from, pwd));
//            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
//    protected PasswordAuthentication getPasswordAuthentication() {
//        return new PasswordAuthentication(userName, password);
//    }
//});
            Message msg = new MimeMessage(session);
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("Test mail one");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(body);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(body);
            multipart.addBodyPart(messageBodyPart);
            msg.setContent(multipart);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] addressTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                addressTo[i] = new InternetAddress(to[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
//            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            Transport.send(msg);
            System.out.println("Message sent OK.");
            return "success";
        } catch (Exception ex) {
            ex.printStackTrace();
             ex.getMessage();
        }
        return null;
    }

    public Properties getSmtpProperties() {
        return smtpProperties;
    }

    public void setSmtpProperties(Properties smtpProperties) {
        this.smtpProperties = smtpProperties;
    }
}

GMailAuthenticator代码  * /

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


class GMailAuthenticator extends Authenticator {
     String user;
     String pw;
     public GMailAuthenticator (String username, String password)
     {
        super();
        this.user = username;
        this.pw = password;
     }
    public PasswordAuthentication getPasswordAuthentication()
    {
       return new PasswordAuthentication(user, pw);
    }
}

1 个答案:

答案 0 :(得分:0)

AuthenticationFailedException表示服务器认为您为其提供了错误的用户名或密码。你会说“当然我没有给它错误的用户名或密码,我不是那么愚蠢!”好吧,服务器不同意你的意见。

尝试打开JavaMail session debugging,协议跟踪可能会提供有关出错的其他线索。您可能还需要将“mail.debug.auth”属性设置为“true”以获取有关登录过程的其他详细信息。

另外,请参阅代码中的某些common mistakes的JavaMail常见问题解答。

确保没有反病毒或防火墙拦截您连接服务器的尝试。