Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

smtp - How to get gmail thread id using JavaMail API in android?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...