Reputation: 8995
I have the following method for retrieving messages from Gmail using imap
public static void main(String[] args)
{
Properties props = new Properties();
try
{
props.load(new FileInputStream(new File("smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "******@gmail.com", "mypass");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
Message[] messages = inbox.getMessages();
System.out.println("------------------------------");
for (int i = 0; i < 10; i++)
{
System.out.println("Mail Subject:- " + messages[i].getSubject());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
my smtp.properties contains
mail.smtp.host=smtp.gmail.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465
I get the following when I run the program
javax.mail.MessagingException: Remote host closed connection during handshake;
nested exception is:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:695)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at gmailsmpt.Main.main(Main.java:25)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:532)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:337)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:116)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:121)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:710)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:659)
... 3 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 14 more
How can I fix the issue?
Upvotes: 3
Views: 9150
Reputation: 11
The problem is that the default TLS version in JAVA jre1.8.0_141 1.0, however due to vulnerabilities in 1.0 a lot of email servers no longer accept connections. To instead use 1.2 directly in code this instruction worked for me when creating properties for sending mail:
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
For example:
Properties props = System.getProperties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtps.auth", mailAuth);
props.put("mail.transport.protocol", mailProtocol);
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
// Get a Session object
Session sess = Session.getInstance(props, null);
// construct the message
MimeMessage msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(mailUser));
Also note, you could just set that same property in the properties file you're loading (smtp.properties
) at the start of your try
block.
Upvotes: 1
Reputation: 11
try adding below to the properties: properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Upvotes: 0
Reputation: 29971
First, clean up all these common mistakes. You don't need any socket factories.
Then try these connection debugging tips.
Possibly you have a firewall or anti-virus program that's preventing you from connecting.
Upvotes: 1
Reputation: 415
Are there more stacktraces? Older Java versions have problems with TLS handshakes with RSA keys with more than 1024 bit (may be also with keys having exactly bit byte).
This was fixed in Java 1.7 or Java 1.8.
Upvotes: 0