Accessing emails from gmail using IMAP ( javamail API)(使用 IMAP(javamail API)从 gmail 访问电子邮件)
问题描述
我正在尝试借助 JavaMail API 通过 IMAP 访问来自 Gmail 帐户的电子邮件.我想知道为什么该代码适用于一个电子邮件帐户但不适用于另一个.
I am trying to access emails from Gmail accounts through IMAP with the help of the JavaMail API. I was wondering why the code works for one email account but doesn't work for another.
我可以访问两个电子邮件帐户的 Inbox
文件夹.但是对于其中一个电子邮件帐户,无法访问 SPAM([Gmail]/Spam)
等其他文件夹,并且会引发 FolderNotFoundException
异常.有人可以解释发生了什么问题吗?
I am able to access the Inbox
folder of both email accounts. But for one of the email accounts, other folders like SPAM([Gmail]/Spam)
are not able to be accessed and it throws a FolderNotFoundException
exception. Could anybody please explain what is going wrong?
提前谢谢你.
代码如下:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.internet.*;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
public class FolderFetchIMAP {
public static void main(String[] args) throws MessagingException, IOException {
IMAPFolder folder = null;
Store store = null;
String subject = null;
Flag flag = null;
try
{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.googlemail.com","myemailid@gmail.com", "password");
folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for other email account
//folder = (IMAPFolder) store.getFolder("inbox"); This works for both email account
if(!folder.isOpen())
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
System.out.println(messages.length);
for (int i=0; i < messages.length;i++)
{
System.out.println("*****************************************************************************");
System.out.println("MESSAGE " + (i + 1) + ":");
Message msg = messages[i];
//System.out.println(msg.getMessageNumber());
//Object String;
//System.out.println(folder.getUID(msg)
subject = msg.getSubject();
System.out.println("Subject: " + subject);
System.out.println("From: " + msg.getFrom()[0]);
System.out.println("To: "+msg.getAllRecipients()[0]);
System.out.println("Date: "+msg.getReceivedDate());
System.out.println("Size: "+msg.getSize());
System.out.println(msg.getFlags());
System.out.println("Body:
"+ msg.getContent());
System.out.println(msg.getContentType());
}
}
finally
{
if (folder != null && folder.isOpen()) { folder.close(true); }
if (store != null) { store.close(); }
}
}
}
推荐答案
其中一个帐户是否有任何机会使用非英文 UI?
Is one of the accounts using non-english UI by any chance?
Gmail 文件夹名称已根据用户本地化设置进行本地化.
Gmail folder names are localized with respect to the user localization settings.
目前获取本地化文件夹名称的唯一方法是使用 XLIST 命令.
Currently the only way to get the name of the localized folder is by using XLIST command.
这篇关于使用 IMAP(javamail API)从 gmail 访问电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 IMAP(javamail API)从 gmail 访问电子邮件
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01