【问题标题】:Why cant send mail through smtp server?为什么不能通过 smtp 服务器发送邮件?
【发布时间】:2016-10-01 14:35:31
【问题描述】:

我尝试使用以下代码将邮件从 hotmail 发送到 yahoo/gmail smtp.live.com 或 smtp-mail.outlook.com 但不断出现错误:

String to = SendTo.getText();
String from = username;

        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties properties = System.getProperties();
        properties.put("mail.smtp.ssl.enable","true");
        properties.put("mail.smtp.host", "smtp.live.com");    // i try smtp-mail.outlook.com also cant
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");


        Authenticator authenticator = new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username,passwd);
    }};

        Session session = Session.getInstance(properties,authenticator); //default session

        if(attachment.getText().isEmpty()){
              try{
            MimeMessage message = new MimeMessage(session); //email message
            message.setFrom(new InternetAddress(from));     //setting header fields
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subjectField.getText()); //subject line

            //mail body
            message.setText(TextArea.getText());
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, to);
            //host = smtp.live.com from=abc@hotmail.com to=def@gmail.com
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("Sent message successfully....");

错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 465;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)

【问题讨论】:

  • 请重新格式化异常以保留换行符并使其更易于阅读。
  • 已解决:对于 hotmail 使用 transport.connect(host,587,username,passwd); properties.put("mail.smtp.starttls.enable", "true");

标签: java smtp sendmail hotmail


【解决方案1】:

主机 smtp.live.com 和主机 smtp-mail.outlook.com 似乎都不接受端口 465 上的连接。

您可能应该改用端口 587。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2011-02-07
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多