Can't connect to SMTP server with Java

岁酱吖の 提交于 2020-01-05 23:20:54

问题


I want to send an email from Java using the Java Mail API (javax.mail.*). I can access the SMTP-server via Thunderbird with the following settings:

  • Server: math.uni-freiburg.de
  • Port: 465
  • Username: MY_USERNAME
  • Authentification: password, normal
  • Security: SSL/TLS

I know nothing else about the SMTP server.

With my code I always get the error: Could not connect to SMTP host: math.uni-freiburg.de, port: 465, response: -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); 

I already tried to change some of the properties but since I don't really know what they do this wasn't very successfull.

Any ideas what I could change?

来源:https://stackoverflow.com/questions/26513888/cant-connect-to-smtp-server-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!