从 Gmail 检索未读电子邮件 - JavaMail API + IMAP
现在我已经创建了一个代码来检索未读的电子邮件并读取其正文,然后我们可以存储或执行我们想做的任何操作。
它完全可以工作,但问题是它只给我第一封邮件的正文,而第二封邮件则给我带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用相同的索引从列表中获取消息以及从该消息中获取一部分。因此,您要从消息 1 中获取第 1 部分,从消息 2 中获取第 2 部分,等等。在某些时候,您会遇到消息 N,其中的部分少于 N 个部分,并且会出现 ArrayIndexOutOfBoundsException 异常。
另外,您假设所有消息都是多部分的。第一次对非多部分消息调用
Message.getContent()
时,您将收到ClassCastException
,因为它很可能会返回字符串
代替。同样,您假设非嵌套多部分。第一次收到包含
multipart/alternative
作为其第一个子部分的顶级multipart/mixed
消息时,调用MimeBodyPart.getContent()
将返回另一个Multipart
,因此p.toString()
将仅返回 Java 对象标识符,而不是您想要的消息内容。为了正确执行此操作,您需要遍历消息结构并确定您关心的“正文”部分。
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
.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 aClassCastException
as it'll most likely be returning you aString
instead.Similarly, you're assuming non-nested multiparts. The first time you get a message with a top-level
multipart/mixed
containing amultipart/alternative
as its first subpart, the call toMimeBodyPart.getContent()
will return anotherMultipart
and thusp.toString()
will just return a Java object identifier, not the message content you want.To do it right, you need to walk the message structure and determine the "body" part you care about.