繁体   English   中英

JavaMail API的Outlook电子邮件设置

[英]Outlook Email Setup for JavaMail API

我在设置Outlook邮件到OutlookMail时遇到了麻烦,我按照这里这里的说明进行操作

Properties props = new Properties();
          props.put("mail.smtp.user", d_email);
          props.put("mail.smtp.host", "smtp-mail.outlook.com");
          props.put("mail.smtp.port", "587");
          props.put("mail.smtp.starttls.enable","true");
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.socketFactory.port", "587");
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.socketFactory.fallback", "true");

          try
          {
          Authenticator auth = new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(d_email, d_password);
              }
            };

          Session session = Session.getInstance(props, auth);

          MimeMessage msg = new MimeMessage(session);
          msg.setText("Hey, this is the testing email.");
          msg.setSubject("Testing");
          msg.setFrom(new InternetAddress(d_email));
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress("example@gmail.com"));
          Transport.send(msg);

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

我一直在收到错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    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:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)

我一直在搜索谷歌和stackoverflow,我想这是必须使用SSL而不是TLS因为outlook不接受TLS,否则连接将被远程主机关闭,但我从互联网上找到的解决方案没有解决。 为了成功发送测试邮件,我错过了什么?

我是邮件API的新手,任何建议或帮助将不胜感激。 谢谢。

编辑:我有一个微软的电子邮件帐户我注册的类似于'xxxx@outlook.com'

像这样删除SocketFactory参数,并享受发送电子邮件。

props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true"); 

我没有在文档中找到必要的参数。

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

请没有这些道具重试:

props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "true");

我没有必要将它们设置为office365。 outlook.com应该是类似的

试试这个代码它对我有用

    Properties props = null;
    if (props == null) {
        props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.host", "smtp.live.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.user", User);
        props.put("mail.smtp.pwd", Pwd);
    }
    Session session = Session.getInstance(props, null);
    session.setDebug(true);
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(User));
    if (Objet != null) {
        msg.setSubject(Objet);

    }
    msg.setText(Content);
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailId));
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.live.com", 587, User, Pwd);
    transport.sendMessage(msg, msg.getAllRecipients());
    System.out.println("Mail sent successfully at " + emailId);
    transport.close();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM