从 Gmail 检索未读电子邮件 - JavaMail API + IMAP

发布于 2024-10-24 11:35:47 字数 2687 浏览 4 评论 0原文

现在我已经创建了一个代码来检索未读的电子邮件并读取其正文,然后我们可以存储或执行我们想做的任何操作。

它完全可以工作,但问题是它只给我第一封邮件的正文,而第二封邮件则给我带有 html 标签的正文。

我正在使用 JavaMail API...

我该怎么办?

提前致谢。

此致, 阿里

package pack1;
//import the necessary classes

import java.io.IOException;
import java.util.Properties;

import javax.mail.Flags;
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 InboxReader {

    public static void main(String args[]) {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
            try {
                Session session = Session.getDefaultInstance(props, null);
                Store store = session.getStore("imaps");
                store.connect("imap.gmail.com", "mail", "pass");
                System.out.println(store);

                Folder inbox = store.getFolder("Inbox");
                inbox.open(Folder.READ_ONLY);
                //Message messages[] = inbox.getMessages();
                FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
                Message messages[] = inbox.search(ft);

                int i =0;
                for(Message message:messages) 
                {

                     Multipart mp = (Multipart)messages[i].getContent();  
                     Object p = mp.getBodyPart(i).getContent();  
                     String q = p.toString();//object has the body content  
                     System.out.println(q);//prints the body  
                     System.out.println( messages[i].getSubject()+ " \n"+i);i++;
                }


                    } catch (NoSuchProviderException e) {
                        e.printStackTrace();
                        System.exit(1);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                        System.exit(2);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

    }

}

输出:

a

a 
0
<div dir="ltr">b<br>
</div>

b 
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Unknown Source)
    at javax.mail.Multipart.getBodyPart(Multipart.java:156)
    at javax.mail.internet.MimeMultipart.getBodyPart(MimeMultipart.java:258)
    at pack1.InboxReader.main(InboxReader.java:39)

Now I have created a code to retrieve unread email and read its body and then we can store or do whatever we want to do.

It is completely working but the problem is that it giving me only the body for the first mail and for the second it gives the body with html tags.

I'm using JavaMail API...

How can I do??

Thanks in advance.

Best regards,
Ali

package pack1;
//import the necessary classes

import java.io.IOException;
import java.util.Properties;

import javax.mail.Flags;
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 InboxReader {

    public static void main(String args[]) {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
            try {
                Session session = Session.getDefaultInstance(props, null);
                Store store = session.getStore("imaps");
                store.connect("imap.gmail.com", "mail", "pass");
                System.out.println(store);

                Folder inbox = store.getFolder("Inbox");
                inbox.open(Folder.READ_ONLY);
                //Message messages[] = inbox.getMessages();
                FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
                Message messages[] = inbox.search(ft);

                int i =0;
                for(Message message:messages) 
                {

                     Multipart mp = (Multipart)messages[i].getContent();  
                     Object p = mp.getBodyPart(i).getContent();  
                     String q = p.toString();//object has the body content  
                     System.out.println(q);//prints the body  
                     System.out.println( messages[i].getSubject()+ " \n"+i);i++;
                }


                    } catch (NoSuchProviderException e) {
                        e.printStackTrace();
                        System.exit(1);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                        System.exit(2);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

    }

}

The output :

a

a 
0
<div dir="ltr">b<br>
</div>

b 
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Unknown Source)
    at javax.mail.Multipart.getBodyPart(Multipart.java:156)
    at javax.mail.internet.MimeMultipart.getBodyPart(MimeMultipart.java:258)
    at pack1.InboxReader.main(InboxReader.java:39)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2024-10-31 11:35:47

您使用相同的索引从列表中获取消息以及从该消息中获取一部分。因此,您要从消息 1 中获取第 1 部分,从消息 2 中获取第 2 部分,等等。在某些时候,您会遇到消息 N,其中的部分少于 N 个部分,并且会出现 ArrayIndexOutOfBoundsException 异常。

Multipart mp = (Multipart)messages[i].getContent();  
Object p = mp.getBodyPart(i).getContent();  

另外,您假设所有消息都是多部分的。第一次对非多部分消息调用 Message.getContent() 时,您将收到 ClassCastException,因为它很可能会返回 字符串 代替。

Multipart mp = (Multipart)messages[i].getContent();  

同样,您假设非嵌套多部分。第一次收到包含 multipart/alternative 作为其第一个子部分的顶级 multipart/mixed 消息时,调用 MimeBodyPart.getContent() 将返回另一个 Multipart,因此 p.toString() 将仅返回 Java 对象标识符,而不是您想要的消息内容。

Object p = mp.getBodyPart(i).getContent();  
String q = p.toString();//object has the body content  

为了正确执行此操作,您需要遍历消息结构并确定您关心的“正文”部分。

You're using the same index to get a message from your list as to get a part from that message. So you're fetching part 1 from message 1, part 2 from message 2, etc. At some point, you'll hit a message N that has fewer than N parts, and you get the ArrayIndexOutOfBoundsException.

Multipart mp = (Multipart)messages[i].getContent();  
Object p = mp.getBodyPart(i).getContent();  

Also, you're assuming that all your messages are multipart. The first time you call Message.getContent() on a non-multipart message, you'll get a ClassCastException as it'll most likely be returning you a String instead.

Multipart mp = (Multipart)messages[i].getContent();  

Similarly, you're assuming non-nested multiparts. The first time you get a message with a top-level multipart/mixed containing a multipart/alternative as its first subpart, the call to MimeBodyPart.getContent() will return another Multipart and thus p.toString() will just return a Java object identifier, not the message content you want.

Object p = mp.getBodyPart(i).getContent();  
String q = p.toString();//object has the body content  

To do it right, you need to walk the message structure and determine the "body" part you care about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文