![](/img/trans.png)
[英]Read mails with attachments from Microsoft mail Exchange server using Java
[英]read sent mails from mail server
我知道如何从INBOX文件夹中检索邮件...但是现在我想从SENT ITEMS文件夹中检索邮件...我正在使用imap来检索数据...让我知道我应该在此函数中传递什么参数从SENT ITEMS文件Folder folder=store.getFolder("inbox");
获取邮件Folder folder=store.getFolder("inbox");
我应该更改收件箱,因为我想知道那个字符串...
这里没有标准名称。 IMAP规范要求将收件箱称为“ INBOX”,但没有专门定义其他文件夹。 毕竟,这只是一个文件夹的名称-一些提供程序将使用“已发送”,一些提供程序将使用“已发送邮件”,甚至您可能还会看到一些其他变体。
我建议列出服务器知道的文件夹,然后从中选择适当的文件夹(以交互方式进行,或者如果无头运行,则以grep的名义发送“已发送”)。 总的来说,更好的选择是使它成为可配置的参数(如果您的应用程序已经具有属性文件)。
当然,如果这是一个一次性项目,则可以仅对特定服务器的值进行硬编码。 但是,如果您想正确执行操作,则需要保持灵活性。
我找到了解决我问题的方法。...我使用以下代码列出了邮件服务器中的文件夹,并将这些值传递给getFolder()函数...。
Folder[] folderList = store.getDefaultFolder().list();
for (int i = 0; i < folderList.length; i++) {
System.out.println(folderList[i].getFullName());
}
Gmail将已发送的邮件存储在[Gmail]
文件夹内名为“已Sent Mail
[Gmail]
文件夹下。 这样我就可以通过它获取发送的邮件文件夹;
Folder sentMail = store.getFolder( "[Gmail]" ).getFolder( "Sent Mail" );
此代码将检索所有邮件,并将打印内容并存储在本地(如果有附件)
public class MailReader {
Folder inbox;
public MailReader() {
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", "username",
"password");
/* Mention the folder name which you want to read. */
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : "
+ inbox.getMessageCount() + " "
+ inbox.getUnreadMessageCount());
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/*
* Get the messages which is unread in the Inbox Message messages[]
* = inbox.search(new FlagTerm( new Flags(Flag.SEEN), false));
*/
Message messages[] = inbox.getMessages();
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try {
printAllMessages(messages);
inbox.close(true);
store.close();
} catch (Exception ex) {
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception {
for (int i = 0; i < msgs.length; i++) {
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
/* Print the envelope(FromAddress,ReceivedDate,Subject) */
public void printEnvelope(Message message) throws Exception {
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
System.out.println("Received Date : " + receivedDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg) {
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
if (p.getFileName() == null) {
return;
}
System.out.println("filename:" + p.getFileName());
System.out.println(p.ATTACHMENT);
InputStream is = p.getInputStream();
File file = new File(p.getFileName());
FileOutputStream fout = null;
fout = new FileOutputStream(p.getFileName());
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
fout.write(c);
}
if (fout != null) {
fout.close();
}
}
public static void main(String args[]) {
new MailReader();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.