i want to send an email using gmail as smtp server.
this is my code, and i do not get it to work... after running testSettings() i get the debug output and then it just stops. no timeout, no error, nothing....
public void testSettings() {
final String username = Settings.get("benutzername");
final String password = Settings.get("passwort");
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", Settings.get("port"));
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", Settings.get("server"));
props.put("mail.smtp.port", Settings.get("port"));
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.ssl.checkserveridentity", "false");
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.connectiontimeout", "10000");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("test");
Transport transport = session.getTransport("smtps");
transport.send(message);
// Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
// throw new RuntimeException(e);
}
}
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
The following error occurs: http://pastie.org/private/rkoknss6ppiufjd9swqta
To utilize your mail server's SSL, open your email client, set the incoming/outgoing server to your email access domain (xxxx-xxxx.accessdomain.com), and set the appropriate SSL port numbers: IMAP (SSL): 993. POP (SSL): 995. SMTP (SSL): 465.
The javax. mail. Transport class is another provider-implemented class and is used for sending a message over a specific protocol.
StartTLS is a protocol command used to inform the email server that the email client wants to upgrade from an insecure connection to a secure one using TLS or SSL. StartTLS is used with SMTP and IMAP, while POP3 uses the slightly different command for encryption, STLS.
Create a new session object by calling getDefaultInstance() method and passing properties as argument to get all of the important properties like hostname of the SMTP server etc. Create a MimeMessage object by passing the session object created in previous step. The final step is to send email using the javax. mail.
From reading this: http://www.oracle.com/technetwork/java/javamail/faq-135477.html#commonmistakes
The use of
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");**
and
props.put("mail.smtp.socketFactory.port", "465");
is kind of outdated. To simplify the code, use:
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
Instead of
props.put("mail.transport.protocol", "smtps");
Transport transport = session.getTransport("smtps");
Use
props.put("mail.transport.protocol", "smtp");
Transport transport =session.getTransport("smtp");
Use smtp, not smtps
I used JDK 8, Netbeans 8, JavaMail 1.5.2 and this example works fine:
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Test Mail");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
If you are not able connect with port 465, try port 587
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With