java - SMTP 主机 :smtp. Outlook.com 中的连接错误,端口:25

标签 java email outlook

我尝试了一个使用 gmail 的示例程序,该程序成功运行,并尝试对 Outlook 进行修改,主要更改服务器和端口名称。错误如下:

javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.outlook.com,端口:25;嵌套异常是: javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?

这是我的代码如下:

package mailsend;

import javax.mail.Authenticator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Outlook {
    final String senderEmailID = "xxx";
    final String senderPassword = "xxx";
    final String emailSMTPserver = "smtp.outlook.com";
    //final String emailSMTPserver = "Smtp.live.com";
    final String emailServerPort = "25";
    String receiverEmailID = null;
    static String emailSubject = "Test Mail";
    static String emailBody = ":)";

    //mail.smtp.ssl.enable = "true";
    public Outlook(String receiverEmailID, String Subject, String Body) {
        this.receiverEmailID = receiverEmailID;
        this.emailSubject = Subject;
        this.emailBody = Body;
        Properties props = new Properties();
        //System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
        props.put("mail.smtp.user", senderEmailID);
        props.put("mail.smtp.host", emailSMTPserver);
        props.put("mail.smtp.port", emailServerPort);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", emailServerPort);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.enable", "true");
        /*prop key="mail.smtp.starttls.enable"   */
        SecurityManager security = System.getSecurityManager();
        //mail.smtp.ssl.enable "true"

        try {
            // SMTPAuthenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, new SMTPAuthenticator("xxx", xxx"));
            MimeMessage msg = new MimeMessage(session);
            msg.setText(emailBody);
            msg.setSubject(emailSubject);
            msg.setFrom(new InternetAddress(senderEmailID));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmailID));
            Transport.send(msg);
            //sendMessage(msg, msg.getAllRecipients());
            System.out.println("Message send Successfully:)");
        }

        catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    public class SMTPAuthenticator extends Authenticator {
        String user;
        String pwd;

        SMTPAuthenticator(String senderEmailID, String senderPassword) {

            super();
            this.user = senderEmailID;
            this.pwd = senderPassword;

        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pwd);
        }
    }

    public static void main(String[] args) {

        Outlook obj1 = new Outlook("xxx", "hi", "test");

    }
}

最佳答案

您必须在端口 587 上使用 TLS 连接到 smtp-mail.outlook.com。

关于java - SMTP 主机 :smtp. Outlook.com 中的连接错误,端口:25,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47405543/

相关文章:

vba - 展望 VBA 宏 : Best way to indicate 'please wait'

java - Runnable 的时间确定性执行

Java方法代码错误

java - java间歇性发送电子邮件

mysql - 最重要的电子邮件 header 单独存储在数据库中

c++ - 从 Outlook 对待办事项/任务/约会执行 CRUD?

vba - 从 Outlook 打开时的 Excel 安全设置

java - 模运算符空指针异常

java - 如何实现JDatePicker的FocusListener?

email - 邮件不是在命令行上使用 mutt 发送的