Reputation: 1068
Hope you all are safe. Recently I have implemented JavaX mail in my application. When I have implemented this code was working fine I was getting mails from my Webmail. But after few days I tried to run this code again, it start giving me the exceptions. I have checked the server-side
nothing is changed. Can anyone please help me out to solve this problem.
public class GMailSender extends javax.mail.Authenticator {
static {
Security.addProvider(new JSSEProvider());
}
private final String user;
private final String password;
private final Session session;
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.smtp.host", "xtenxion.com");
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.user", user);
props.setProperty("mail.smtp.ssl.trust", "xtenxion.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized Boolean sendMail(String subject, String body, String sender, String recipients) {
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(recipients));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
Log.e("SendMail","message sent successfully....");
return true;
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
return false;
}
}
}
Having this exception:
2021-04-29 16:05:11.810 14664-15084/com.xtenxion.letsplayqna E/SendMail: Could not connect to SMTP host: xtenxion.com, port: 465 javax.mail.MessagingException: Could not connect to SMTP host: xtenxion.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: Connection closed by peer at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) at javax.mail.Service.connect(Service.java:310) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) at com.xtenxion.letsplayqna.mail.GMailSender.sendMail(GMailSender.java:56) at com.xtenxion.letsplayqna.activities.ForgetPassword_One_Activity$1$1.doInBackground(ForgetPassword_One_Activity.java:86) at com.xtenxion.letsplayqna.activities.ForgetPassword_One_Activity$1$1.doInBackground(ForgetPassword_One_Activity.java:73) at android.os.AsyncTask$2.call(AsyncTask.java:345) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:784) Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351) at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:665) at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:627) at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366) com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) at javax.mail.Service.connect(Service.java:310) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) at com.xtenxion.letsplayqna.mail.GMailSender.sendMail(GMailSender.java:56) at com.xtenxion.letsplayqna.activities.ForgetPassword_One_Activity$1$1.doInBackground(ForgetPassword_One_Activity.java:86) at com.xtenxion.letsplayqna.activities.ForgetPassword_One_Activity$1$1.doInBackground(ForgetPassword_One_Activity.java:73) at android.os.AsyncTask$2.call(AsyncTask.java:345) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:784)
UPDATE:
public class GMailSender extends javax.mail.Authenticator {
static {
Security.addProvider(new JSSEProvider());
}
private final Session session;
public GMailSender(String user, String password) {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "xtenxion.com");
props.setProperty("mail.smtp.ssl.trust", "xtenxion.com");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
session.setDebug(true);
}
public synchronized Boolean sendMail(String subject, String body, String sender, String recipients) {
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
Log.e("SendMail", "message sent successfully....");
return true;
} catch (Exception e) {
Log.e("SendMail", "Error " + e.getMessage());
return false;
}
}
}
Exception:
Error Could not connect to SMTP host: xtenxion.com, port: 465
I tried several code to connect with my Webmail but still failed. The Webmail address I'm using is [email protected].I don't know what's wrong and how to fix that.
Upvotes: 1
Views: 387
Reputation: 11035
How do I access Gmail with Jakarta Mail?:
String host = "smtp.gmail.com";
String username = "user";
String password = "passwd";
Properties props = new Properties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.ssl.enable", "true");
// set any other needed mail.smtp.* properties here
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport.send(msg, username, password);
The dot protocol properties should be smtps
and imaps
. However you shouldn't need to set them at all. Just remove them. The xtenxion.com
ssl certificate needs to be added to your truststore.
Upvotes: 2