我知道如何從INBOX文件夾中收回郵件......但是現在我想從SENT ITEMS文件夾中檢索郵件......我正在使用imap來檢索數據... 讓我知道我應該在這個函數中傳遞什麼參數從SENT ITEMS文件夾中獲取郵件 Folder folder=store.getFolder("inbox");
我應該將收件箱更改爲一些stirng我想知道該字符串...閱讀從郵件服務器發送的郵件
回答
我發現我的問題的解決方案.... 我用這個代碼,列出從郵件服務器 的文件夾和傳遞的GetFolder這些值()函數...它的正常工作..
Folder[] folderList = store.getDefaultFolder().list();
for (int i = 0; i < folderList.length; i++) {
System.out.println(folderList[i].getFullName());
}
這裏沒有標準名稱。 IMAP spec要求將收件箱稱爲「INBOX」,但沒有特別定義其他文件夾。它畢竟只是一個文件夾的名稱 - 一些提供商將使用「已發送」,一些將使用「已發送郵件」,您甚至可能會看到其他一些變體。
我建議列出服務器知道的文件夾,然後從那裏選擇合適的文件夾(交互式地或者在運行無頭的情況下爲名字中的「發送」)。總體來說更好的選擇可能是使其成爲一個可配置的參數(如果您的應用程序已經有一個屬性文件)。
當然,如果這是一次性項目,您可以硬編碼特定服務器的值。但是,如果你想正確地做到這一點,你需要靈活。
謝謝安傑伊·多伊爾......你知道,我們怎麼能列出服務器知道 – Kandha 2010-10-11 09:35:39
此代碼將檢索所有郵件,將打印內容,並存儲在本地,如果有任何附件
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();
}
}
參考:1.http://www.vipan.com/htdocs/javamail.html 2.http://metoojava.wordpress.com/2010/03/ 21/java的碼到接收郵件-使用-javamailapi / – Kandha 2010-11-29 09:46:17
- 1. 從服務器發送郵件php
- 2. 從CRM 2011中的不同郵件服務器發送郵件
- 3. 無法從ASP.NET中的godaddy郵件服務器發送郵件
- 4. 'System.Net.Mail.SmtpException:從遠程郵件服務器發送郵件失敗
- 5. Python:我的服務器郵件被髮送到垃圾郵件
- 6. 發送沒有郵件服務器的郵件
- 7. smtp服務器安裝發送郵件發送郵件任務ssis 2012包
- 8. 郵件不通過發送郵件發送服務
- 9. 從特定的郵件服務器發送郵件時發生SmtpException
- 10. 郵件未在服務器上發送
- 11. SMTP服務器不發送郵件
- 12. 只發送電子郵件服務器
- 13. 用Plesk服務器發送郵件
- 14. SQL服務器:發送電子郵件
- 15. 使用SMTP服務器發送郵件
- 16. 配置xampp服務器發送郵件?
- 17. 如果郵件從自定義smtp服務器發送郵件,Gmail不會列出已發送的郵件
- 18. 從收到的郵件服務器(POP)讀取郵件
- 19. 在Linux服務器上通過郵件發送郵件功能
- 20. 無法將郵件發送到電子郵件服務器
- 21. SMTP郵件服務器發送郵件無法正常工作
- 22. 無法使用PostFix郵件服務器發送電子郵件
- 23. 無法使用JAMES郵件服務器發送郵件?
- 24. 配置郵件服務器發送郵件到PHP腳本
- 25. smtp服務器用於通過java郵件api發送郵件
- 26. 通過郵件服務器發送郵件Asp.net mvc4
- 27. 使用Win API通過Gmail郵件服務器發送郵件
- 28. .NET - 郵件服務器不通過SmtpClient.Send發送郵件
- 29. 如何從代碼發送郵件而不發送郵件被接收方郵件服務器拒絕
- 30. 從郵件服務器的rDNS以外的其他域發送電子郵件
的文件夾@ Kandhasamy:尼斯卡ndhasamy ............. – 2010-10-11 10:39:55