gpt4 book ai didi

java - 阅读从 GMail 发送的邮件

转载 作者:行者123 更新时间:2023-11-30 11:39:59 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 JavaMail 阅读邮件。我试图涵盖所有组合,即在自定义服务器/Gmail ID/Live ID 上发送/接收的邮件。

从带有附件的 GMail 发送的一些邮件会出现问题。我能够收到附件,但内容返回 javax.mail.internet.MimeMultipart@44f2e698

这是用于接收和阅读消息的代码:

    Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");

try {
/* Create the session and get the store for read the mail. */
Session session = Session.getInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", Username, Password);

/* Mention the folder name which you want to read. */
Folder inbox = store.getFolder("INBOX");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());

/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);

Message messages[] = inbox.getMessages();
Log.d("Inbox", "Message Count: "+inbox.getMessageCount());

for (int i = messages.length - 1 ; i > 0; --i) {
Log.i("ContentType", "ContentType: "+messages[i].getContentType());

Object msgContent = messages[i].getContent();

String content = "";

/* Check if content is pure text/html or in parts */
if (msgContent instanceof Multipart) {

Multipart multipart = (Multipart) msgContent;

Log.e("BodyPart", "MultiPartCount: "+multipart.getCount());

for (int j = 0; j < multipart.getCount(); j++) {

BodyPart bodyPart = multipart.getBodyPart(j);

String disposition = bodyPart.getDisposition();

if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) { // BodyPart.ATTACHMENT doesn't work for gmail
System.out.println("Mail have some attachment");

DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
}
else {
System.out.println("Content: "+bodyPart.getContent());
content= bodyPart.getContent().toString();
}
}
}
else
content= messages[i].getContent().toString();

我对有问题的邮件的了解:

  • getFrom 也返回名称,即它以这种格式出现 FirstName LastName &ltemailID@gmail.com>

  • MultiPart 包含 2 个 BodyPart:

    • BodyPart 1 返回内容为 javax.mail.internet.MimeMultipart@44f2e698

    • BodyPart 2 返回正确的附件名称

最佳答案

BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698

尝试在 MimeMultiPart 上调用 getBodyPart

这可能会返回一个 MimeBodyPart,您可以调用 getContent()http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content

关于java - 阅读从 GMail 发送的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com