[英]Read mails again and again from gmail using JavaMail Api in java
我正在使用 JavaMail Api 從 gmail 帳戶讀取郵件。 但問題是我只能讀一次。 有什么辦法可以一遍又一遍地閱讀郵件嗎???
我的 Java 代碼是:
import java.util.Properties;
import javax.mail.BodyPart;
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;
public class Main {
// main function. Project run starts from main function...
public static void main(String[] args) {
String host = "pop.gmail.com";// change accordingly
String mailStoreType = "pop3";
String username = "your_email@gmail.com";// change accordingly
String password = "your_password";// change accordingly
check(host, mailStoreType, username, password);
}
// function to make connection and get mails from server known as "Pop" server
public static void check(String host, String storeType, String user, String password)
{
try {
//create properties field
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
//create the folder object and open it
Folder emailFolder = store.getFolder("Inbox");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
Object obj = message.getContent();
Multipart mp = (Multipart)obj;
BodyPart bp = mp.getBodyPart(0);
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("To: " + message.getAllRecipients().toString());
System.out.println("Received Date:" + message.getReceivedDate());
System.out.println("Text: " + bp.getContent().toString());
}
//close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在這段代碼中,我使用 pop 服務器讀取電子郵件,現在我在其中提供電子郵件和密碼並運行。 它工作正常,但它只讀取一次電子郵件,下次如果我運行程序,kit 給我的消息數等於 0...我想一次又一次地閱讀消息,次數不限...任何幫助都會值得贊賞...
如果您想每次都收到所有電子郵件,IMAP 服務器將是最好的選擇。
您可以將郵件服務器更改為
IMAP.gmail.com
端口將是 993(考慮到您使用的是 gmail 帳戶)。
提供的 Link sidgate 將是您的最佳示例。
Gmail 有關於如何處理 pop3 郵件請求的用戶設置。 我遇到了同樣的問題,最后查看了這個頁面: https : //javaee.github.io/javamail/FAQ#gmailsettings 。
要每次查看所有電子郵件,您需要啟用該標志(在設置頁面中):Enable POP for all mail(即使是已經下載的郵件)
對我有用。 希望有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.