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?