Reputation: 1
I am trying to connect to Gmail using IMAP and read an OTP for authentication purposes. I am using this logic. Still, I am receiving an SSL handshake exception.
javax.mail.MessagingException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate);
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.crato.qa.EmailExtractor.ReadingEmails.check(ReadingEmails.java:43)
at com.crato.qa.EmailExtractor.ReadingEmails.main(ReadingEmails.java:110)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:162)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:247)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:448)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
... 4 more
I have tried this code.
package com.crato.qa.EmailExtractor;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
public class ReadingEmails {
public static void check(String host, String storeType, String user, String password) {
try {
// create properties
Properties properties = new Properties();
properties.setProperty("mail.imap.host", host);
properties.put("mail.imap.port", "993");
properties.setProperty("mail.imap.starttls.enable", "true");
properties.setProperty("mail.imap.ssl.trust", host);
properties.setProperty("mail.imap.ssl.enable", "true");
properties.setProperty("mail.imap.starttls.enable", "true");
// properties.setProperty("mail.imap.ssl.protocols", "TLSv1.2");
//Session emailSession = Session.getDefaultInstance(props);
Session emailSession = Session.getInstance(properties, null);
// create the imap store object and connect to the imap server
Store store = emailSession.getStore("imaps");
store.connect(host, user, password);
// create the inbox object and open it
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
// retrieve the messages from the folder in an array and print it
Message[] messages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
message.setFlag(Flag.SEEN, false);
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContentType());
System.out.println("Date : "+ message.getSentDate());
//System.out.println("Text: " + message.match());
System.out.println("Mutlipart text: "+getTextFromMultipartRelatedMessage(message));
break;
}
inbox.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// Helper method to extract text from a "multipart/RELATED" email message
public static String getTextFromMultipartRelatedMessage(Message message) throws Exception {
String result = "";
if (message.isMimeType("multipart/RELATED")) {
Multipart multipart = (Multipart) message.getContent();
int count = multipart.getCount();
System.out.println("Mail Count= "+count);
for (int i = 0; i < count; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disposition = bodyPart.getDisposition();
System.out.println(bodyPart.getContentType());
// Handle inline content here
if (bodyPart.isMimeType("TEXT/PLAIN")) {
result = result + "\n" + bodyPart.getContent();
} else if (bodyPart.isMimeType("text/html")) {
result = result + "\n" + bodyPart.getContent();
}
}
}
return result;
}
public static void main(String[] args) {`your text`
String host = "imap.gmail.com";
String mailStoreType = "imap";
String username = "[email protected]";
String password = "epzm qffw vpcn tzuw";
check(host, mailStoreType, username, password);
}
}
Upvotes: 0
Views: 99