Java Mail显示身份验证失败

时间:2011-11-02 02:04:02

标签: java javamail

我正在使用java邮件从服务器发送邮件。我正在使用下面的代码,但是我收到了这个错误..

 Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException
    at mail.mailSSL.main(mailSSL.java:53)
 Caused by: javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:306)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at mail.mailSSL.main(mailSSL.java:48)

我的代码:

public class mailSSL {
    public static void main(String args[]){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("raviteja.satyavda@gmail.com","raviteja@it");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("raviteja.satyavada@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("ravitejas89@gmail.com"));
            message.setSubject("SFTS MAIL TESING");
            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

我该如何解决?

2 个答案:

答案 0 :(得分:1)

你的班级与我的证书完美配合。你有Gmail帐户吗?当我尝试你(通过代码)时,我得到以下异常:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at                   
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 ib2sm1330072vdb.1

答案 1 :(得分:0)

我认为有一个消息异常未被捕获。