SendFailedException:密码错误

时间:2013-12-24 09:12:20

标签: java email smtp gmail javax.mail

这个问题从一个相当模糊的问题演变成一个更明确的问题。代码来自example by mkyong。至少根据谷歌,it's a bad password,我已经按照他们的登录建议。当然,在输入正确的密码时我得到相同的结果,下面的密码是假的。无论密码如何,它都是相同的输出。

输出:

BUILD SUCCESSFUL
Total time: 1 second
-- listing properties --
mail.smtp.gmail=smtp.gmail.com
mail.smtps.quitwait=true
mail.smtp.username=hawat.thufir@gmail.com
mail.smtp.starttls.enable=true
mail.smtp.connectiontimeout=2000
mail.smtp.user=hawat.thufir
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.password=password
nntp.host=nntp://localhost/
mail.smtp.socketFactory.port=465
mail.smtp.timeout=2000
mail.imap.port=993
mail.imap.timeout=5000
mail.smtp.port=587
mail.smtp.auth=true
mail.imap.host=imap.gmail.com
mail.nntp.newsrc.file=/home/thufir/.newsrc
mail.imap.connectiontimeout=5000
mail.smtp.host=smtp.gmail.com

========message follows==========

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Send failed;
  nested exception is: 
    javax.mail.SendFailedException: 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 g6sm55120721pat.2 - gsmtp
    at net.bounceme.dur.nntp.SendTLS.sendMessage(SendTLS.java:61)
    at net.bounceme.dur.nntp.SendTLS.<init>(SendTLS.java:23)
    at net.bounceme.dur.nntp.SendTLS.main(SendTLS.java:34)
Caused by: javax.mail.SendFailedException: Send failed;
  nested exception is: 
    javax.mail.SendFailedException: 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 g6sm55120721pat.2 - gsmtp
    at javax.mail.Transport.doSend(Transport.java:231)
    at javax.mail.Transport.send(Transport.java:75)
    at net.bounceme.dur.nntp.SendTLS.sendMessage(SendTLS.java:57)
    ... 2 more
thufir@dur:~/NetBeansProjects/MailFromNNTP$ 

代码:

public class SendTLS {

    public SendTLS() {
        try {
            sendMessage();
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        new SendTLS();
    }

    private void sendMessage() throws NoSuchProviderException, MessagingException, IOException {
        Properties props = PropertiesReader.getProps();
        props.list(System.out);
        System.out.println("\n========message follows==========\n");
        final String username = props.getProperty("mail.smtp.username");
        final String password = props.getProperty("mail.smtp.password");
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-email@gmail.com"));
            message.setSubject("Testing Subject");
            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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

因为您要连接到Gmail。使用这些属性

prop.put("mail.smtp.starttls.enable", "true"); 
prop.put("mail.smtp.host", host); 
prop.put("mail.smtp.user", from); 
prop.put("mail.smtp.password", pass); 
prop.put("mail.smtp.port", "465"); 
prop.put("mail.smtp.auth", "true");

同时浏览此http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes

*Post was modified after answering, answer may no longer be relevant*