Reading mails sent from GMail(阅读从 GMail 发送的邮件)
问题描述
我正在使用 JavaMail 在我的 Android 应用程序中阅读邮件.我已尝试涵盖所有组合,即在自定义服务器/Gmail ID/Live ID 上发送/接收的邮件.
I am using JavaMail to read mail in my Android app. I have tried to cover all combinations i.e Mail sent/received on/from Custom Server/Gmail ID/Live ID.
从 GMail WITH Attachment 发送的一些邮件会出现问题.我能够接收附件,但内容返回 javax.mail.internet.MimeMultipart@44f2e698
The problem occurs with SOME of the mails sent from GMail WITH Attachment. I am able to receive the attachment, but the content returns javax.mail.internet.MimeMultipart@44f2e698
这是用于接收和阅读消息的代码:
Here's the code used to receive and read messages:
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();
我对有问题的邮件的了解:
What I know about the problematic mails:
getFrom
也返回名称,即它采用这种格式 FirstName LastName <emailID@gmail.com>
getFrom
also return the name i.e it comes in this format FirstName LastName <emailID@gmail.com>
MultiPart 包含 2 个 BodyPart:
MultiPart contains 2 BodyParts:
BodyPart 1 将内容返回为
javax.mail.internet.MimeMultipart@44f2e698
BodyPart 2 返回正确的附件名称
BodyPart 2 returns the correct name for attachment
推荐答案
BodyPart 1 将内容返回为javax.mail.internet.MimeMultipart@44f2e698
BodyPart 1 returns the content as javax.mail.internet.MimeMultipart@44f2e698
尝试在 MimeMultiPart
这可能会返回一个 MimeBodyPart,您可以调用 getContent() onhttp://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content
That probably returns a MimeBodyPart you can call getContent() on http://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content
这篇关于阅读从 GMail 发送的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:阅读从 GMail 发送的邮件
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01