As per gimap documentation javamail new gimap doc, ImapMessage not casting to GmailMessage.
Doc code is:
GmailMessage gmsg = (GmailMessage)msg; System.out.println("Gmail message ID is " + gmsg.getMsgId()); String[] labels = gmsg.getLabels(); for (String s : labels) System.out.println("Gmail message label: " + s);
here msg is a object of ImapMessage.
Error is: java.lang.ClassCastException: com.sun.mail.imap.IMAPMessage cannot be cast to com.sun.mail.gimap.mailMessage
How to solve it?
My Imap connection code is:
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Folder;
String IMAP_PROTOCOL = "imap";
String IMAP_HOST = "imap.gmail.com";
String IMAP_PORT = "993";
private Store store;
private Folder folderInbox;
private Session session;
private Properties getServerProperties(String protocol, String host,
String port) {
Properties properties = new Properties();
// server setting
properties.put(String.format("mail.%s.host", protocol), host);
properties.put(String.format("mail.%s.port", protocol), port);
// SSL setting
properties.setProperty(
String.format("mail.%s.socketFactory.class", protocol),
"javax.net.ssl.SSLSocketFactory");
properties.setProperty(
String.format("mail.%s.socketFactory.fallback", protocol),
"false");
properties.setProperty(
String.format("mail.%s.socketFactory.port", protocol),
String.valueOf(port));
return properties;
}
//Now connect gmail with imap
Properties properties = getServerProperties(protocol, host, port);
session = Session.getDefaultInstance(properties);
store = session.getStore(protocol);
store.connect(userName, password);
// opens the inbox folder
folderInbox = store.getFolder(folderName);
folderInbox.open(Folder.READ_ONLY);
// fetches new messages from server
Message[] messages = folderInbox.getMessages();
for (Message msg: messages
) {
GmailMessage gmsg = (GmailMessage)msg;
System.out.println("Gmail message ID is " + gmsg.getMsgId());
String[] labels = gmsg.getLabels();
for (String s : labels)
System.out.println("Gmail message label: " + s);
}
question from:
https://stackoverflow.com/questions/66047354/how-to-get-gmail-thread-id-using-javamail-api-in-android 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…