I am trying to send mail from android /java using JavaMail (activation.jar + additionnal.jar + mail.jar). below is my code :
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.NoSuchProviderException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import javax.mail.MessagingException;
import javax.mail.Transport;
protected Void doInBackground(Void... params) {
// Perform networking operations here
// initialize-----------
fromEmail = "[email protected]";
fromPassword = "xxxxx";
toEmailList .add( "[email protected]");
emailSubject = "emailSubject";
emailBody = "emailBody";
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.auth",true );
emailProperties.put("mail.smtp.host","smtp.gmail.com" );
emailProperties.put("mail.smtp.port",587 );
emailProperties.put("mail.smtp.ssl.enable",true );
emailProperties.put("mail.smtp.ssl.required",true );
javax.mail.Session session = javax.mail.Session.getInstance(emailProperties, new javax.mail.Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(fromEmail, fromPassword);
}
});
// Create message
mailSession = javax.mail.Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
try {
emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
try {
for (String toEmail : toEmailList) {
emailMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toEmail));
// emailMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toEmail));
}
} catch (MessagingException e) {
throw new RuntimeException(e);
}
try {
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");// for a html email
} catch (MessagingException e) {
throw new RuntimeException(e);
}
//----- Send Message ---------
Transport transport = null;
try {
transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromEmail, fromPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
I am getting error at.catch (MessagingException e) with data:javax.mail.MessagingException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: failed to connect to smtp.gmail.com/173.194.76.108 (port 25) from /:: (port 55294): connect failed: ETIMEDOUT (Connection timed out)