【问题标题】:SendFailedException: bad passwordSendFailedException:密码错误
【发布时间】:2013-12-24 09:12:20
【问题描述】:

这个问题已经从一个相当模糊的问题演变为一个更明确的问题。代码来自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);
        }
    }
}

【问题讨论】:

  • 端口是整数(而不是字符串?只是猜测:P)?还可以尝试用 Session.getInstance(props) 替换 getDefaultInstance。另外,如果这没有帮助。尝试使用道具“mail.smtp.connectiontimeout”和“mail.smtp.timeout”并设置限制以获取堆栈跟踪。通常是防火墙/端口/网络问题
  • 第 58 行是连接:transport.connect(host, port, user, password);,顺便说一下,端口是一个 int。我当然尝试使用正确的密码。 `
  • 我知道这是经过几次修改。很奇怪,将属性文件缩减到最低限度实际上错误引入SendTLS
  • 我不知道这个,我最后一次尝试,你使用的是两步登录吗?
  • 不,只是常规登录。我打算明天再试一次,不过不是 tls。

标签: java email smtp gmail jakarta-mail


【解决方案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*

【讨论】:

  • 如果这没有帮助,请发表评论:)
  • 我试试这个,看起来很有趣。为什么我需要一个身份验证器,或者说它是一个调试工具,以获得更多输出? (即密码错误等)
  • 这是一个内部类吗?在Gmail 里面?或者,在与Gmail 相同的.java 文件中,但不是内部文件?或者,在它自己的 .java 文件中? docs.oracle.com/javaee/6/api/javax/mail/… 受保护
  • 在这种情况下,它可能是 Gmail 内部的一个内部类。看看这个,什么时候需要验证器,什么时候不需要
  • @Thufir :您能否按原样运行您的代码(没有验证器和其他内容),修改以下行:transport = (SMTPTransport) session.getTransport();在没有“smtp”部分的情况下运行它。我认为这可能是错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多