【问题标题】:nested exception is: javax.mail.MessagingException: Could not connect to SMTP host: smtp.bizm ail.yahoo.com, port: 465嵌套异常是:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.bizm ail.yahoo.com,端口:465
【发布时间】:2017-01-03 13:31:14
【问题描述】:

我无法连接到使用 SSL 身份验证的 smtp 端口号 465。早些时候,它曾经在默认端口 25 中工作,但已将所有出站电子邮件的出站电子邮件设置更改为指向 465。当我使用 Outlook 时,这工作得很好,但显示它无法连接到端口 465。 以下是错误。

javax.mail.SendFailedException: Sending failed;
nested exception is:
    javax.mail.MessagingException: Could not connect to SMTP host: smtp.bizmail.yahoo.com, port: 465
    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)

下面是我使用的 Javax Mail 设置。

Properties props = System.getProperties(); 
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.class", 
props.setProperty("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.server.username", "test@yahoo.com");
props.put("mail.server.password", "test123");
props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.submitter",authenticator.getPasswordAuthentication().getUserName()); 
Session session = Session.getInstance(props, authenticator);
session.setDebug(sessionDebug);`

【问题讨论】:

  • 欢迎来到 Stack Overflow。如果您想要答案,您需要 (1) 提供有问题的代码,并且 (2) 提供带有堆栈跟踪的 complete 异常。不是图片链接。
  • 无论我写的代码是否正确,只要确认它是否正确,那么为什么它不应该连接到服务器
  • 我给了你关于如何提问的明确说明,但你选择不听。你真的不能指望你给出的答案。
  • 我写了全部细节@kdgregory
  • 好的,现在您可以获得更好的答案。一方面,您可以look at the code that threw this exception 并了解您应该致电getNextException() 以查看根本原因是什么。

标签: java exception


【解决方案1】:
public static void sendEmail(String emailMessage){

    try{

        final String fromEmail = "*****@yahoo.com";      
        final String password = "*****";                    
        final String toEmail = "*****@yahoo.com";        // can be any email id 
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.yahoo.com"); //SMTP Host


        props.put("mail.smtp.port", "465");                 //TLS Port 465
        props.put("mail.smtp.auth", "true");                //enable authentication
        props.put("mail.smtp.starttls.enable", "true");     //enable STARTTLS
        props.put("mail.smtp.ssl.enable", "true");          // enable ssl



        Authenticator auth = new Authenticator() {


            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        };
        Session session = Session.getInstance(props, auth);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));



        message.setSubject("Email From Yahoo JavaMail API");
        message.setText(emailMessage);


        Transport.send(message);
        System.out.println("Email successfully Sent");
    }catch(Exception ex){
        System.out.println(ex);
    }
}

试试这个

【讨论】:

  • 您是否有任何迹象表明此代码将解决 OP 的问题?
  • 我做了所有这些事情,并创建了单独的 Authetication 类,正如你在上面的代码中提到的那样,但仍然出现错误
  • 在 yahoo 中,转到帐户安全选项,然后将“允许使用不太安全登录的应用”设置为“开启”。然后它会工作。
  • 我已经设置了“允许使用不太安全登录的应用”仍然无法正常工作
猜你喜欢
  • 2014-03-08
  • 2011-12-09
  • 2020-12-26
  • 1970-01-01
  • 2016-07-29
  • 2014-10-23
  • 2011-11-01
  • 2013-08-21
  • 1970-01-01
相关资源
最近更新 更多