i am trying the below example to send mail using ssl picked from here and changed with my credentials as below and get exception as shown, can you please help me to get through it
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SimpleSSLMail {
private static final String SMTP_HOST_NAME = "xx.xx.xx.xx";
private static final int SMTP_HOST_PORT = 25 ;//465;
private static final String SMTP_AUTH_USER = "xxx@yyy.com";
private static final String SMTP_AUTH_PWD = "xxxx";
public static void main(String[] args) throws Exception{
new SimpleSSLMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
// props.put("mail.smtps.quitwait", "false");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("myid@gmail.com"));
transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
Exception:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host:xx.xx.xx.xx, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
This type of problem occurs due to a network problem . May be you require authentication, you may have wrong username , password .
Anyways you are missing in a lot of things in your code.
Example to obtain a proper SSL connection with gmail server
I don’t see this any where in your code. Then where is the method to authenticate ?
Just check out the following links :
Sending E-mail Using Gmail In Java Here you have a choice of using TLS or SSl
Java Mail API
E-mailing through Java
keys used in java mailing system for your reference