Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';无法使用Java连接到SMTP服务器_Java_Email_Ssl_Smtp_Jakarta Mail - Fatal编程技术网

Can';无法使用Java连接到SMTP服务器

Can';无法使用Java连接到SMTP服务器,java,email,ssl,smtp,jakarta-mail,Java,Email,Ssl,Smtp,Jakarta Mail,我想使用JavaMail API(javax.Mail.*)从Java发送一封电子邮件。我可以使用以下设置通过Thunderbird访问SMTP服务器: 服务器:math.uni-freiburg.de 港口:465 用户名:我的用户名 身份验证:密码,正常 安全性:SSL/TLS 我对SMTP服务器一无所知 使用我的代码,我总是会遇到错误:无法连接到SMTP主机:math.uni-freiburg.de,端口:465,响应:-1 String SMTP_HOST = "math.uni-f

我想使用JavaMail API(javax.Mail.*)从Java发送一封电子邮件。我可以使用以下设置通过Thunderbird访问SMTP服务器:

  • 服务器:math.uni-freiburg.de
  • 港口:465
  • 用户名:我的用户名
  • 身份验证:密码,正常
  • 安全性:SSL/TLS
我对SMTP服务器一无所知

使用我的代码,我总是会遇到错误:无法连接到SMTP主机:math.uni-freiburg.de,端口:465,响应:-1

String SMTP_HOST = "math.uni-freiburg.de"
String SMTP_USER = "MY_USERNAME@math.uni-freiburg.de";
String SMTP_PASSWORD = "MY_PASSWORD";
String SMTP_PORT = "465";

final Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
//props.put("mail.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.tls", "true");
props.put("mail.smtp.ssl.checkserveridentity", "true");

final javax.mail.Authenticator auth = new javax.mail.Authenticator() {
   @Override
   public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(SMTP_USER, SMTP_PASSWORD);
   }
};

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


Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("MY_USERNAME@math.uni-freiburg.de", "MY NAME"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("asdfasdf@gmail.com", "asdf asdf"));
msg.setSubject("SUBJECT");
msg.setText("THE MESSAGE");
msg.saveChanges();
Transport.send(msg); 
我已经尝试过改变一些属性,但是因为我不知道它们是怎么做的,所以没有成功


有什么想法可以改变吗?

如果您遇到任何异常,请在com.sun.mail.SMTPTransport.protocolConnect上发布完整堆栈trace.javax.mail.MessagingException:无法连接到SMTP主机:math.uni-freiburg.de,端口:465,响应:-1位于com.sun.mail.SMTPTransport.openServer(SMTPTransport.java:1922)(SMTPTransport.java:638)在javax.mail.Service.connect(Service.java:317)在javax.mail.Service.connect(Service.java:176)在javax.mail.Service.connect(Service.java:125)在javax.mail.Transport.send0(Transport.java:194)在javax.mail.Transport.send(Transport.java:124)在smtpTest.Email.send(Email.java:102)上我相信还有更多的…
是由…
部分引起的,这就是我从e.printStackTrace()得到的;由于端口465是SMTPS,它将立即使用SSL握手,而不会使用STARTTLS命令升级普通连接。因此,
mail.smtp.STARTTLS.enable
应为false。此外,
mail.smtp.SSL.enable
应为true。