问题描述
我使用JavaMail来阅读邮件在我的Android应用程序。我试图覆盖所有的组合,即邮件发送/接收到/从自定义服务器/ Gmail的ID / Live ID登录。
该问题出现在一些从Gmail发送带有附件的邮件。我能够接收附件,但内容返回 javax.mail.internet.MimeMultipart@44f2e698
下面是用于接收和读取消息的code:
属性道具= System.getProperties();
props.setProperty(mail.store.protocol,的imap);
尝试 {
/ *创建了会议,并拿到店里进行读邮件。 * /
会话会话= Session.getInstance(道具,NULL);
店中店= session.getStore(IMAPS);
store.connect(imap.gmail.com,用户名,密码);
/ *提到要读取该文件夹的名称。 * /
文件夹的收件箱= store.getFolder(收件箱);
的System.out.println(没有未读邮件+ inbox.getUnreadMessageCount());
/ *采用存储打开收件箱。 * /
inbox.open(Folder.READ_ONLY);
留言信息[] = inbox.getMessages();
Log.d(收件箱,消息计数:+ inbox.getMessageCount());
的for(int i = messages.length - 1; I> 0; --i){
Log.i(ContentType的,ContentType的:+消息[I] .getContentType());
对象msgContent =消息[I] .getContent();
字符串的内容=;
/ *检查内容为纯文本/ HTML或部分* /
如果(msgContent的instanceof多部分){
多节多部分=(多部分)msgContent;
Log.e(BodyPart的,MultiPartCount:+ multipart.getCount());
对于(INT J = 0; J< multipart.getCount(); J ++){
BodyPart的正文部分= multipart.getBodyPart(J);
字符串处置= bodyPart.getDisposition();
如果(处置= NULL和放大器;!及(disposition.equalsIgnoreCase(附件))){// BodyPart.ATTACHMENT不适合使用Gmail
的System.out.println(邮件有一定的依恋);
DataHandler的处理程序= bodyPart.getDataHandler();
的System.out.println(文件名:+ handler.getName());
}
其他 {
的System.out.println(内容:+ bodyPart.getContent());
。内容= bodyPart.getContent()的toString();
}
}
}
其他
含量=消息[I] .getContent()的toString()。
我知道有问题的邮件内容:
-
GETFROM
返回该名称,即它有这种格式的名姓和放大器; [email protected]& GT -
多部分包含2 BodyParts将:
-
BodyPart的1返回含量
javax.mail.internet.MimeMultipart@44f2e698
-
BodyPart的2返回正确的名称,附件
-
尝试对 MimeMultiPart类
这可能会返回一个MimeBodyPart可以调用的上的
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.
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
also return the name i.e it comes in this format FirstName LastName &[email protected]>MultiPart contains 2 BodyParts:
BodyPart 1 returns the content as
javax.mail.internet.MimeMultipart@44f2e698
BodyPart 2 returns the correct name for attachment
Try calling getBodyPart on the MimeMultiPart
That probably returns a MimeBodyPart you can call getContent() onhttp://docs.oracle.com/javaee/5/api/javax/mail/internet/MimeBodyPart.html#content
这篇关于阅读邮件从Gmail发出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!