Tôi đang cố truy cập tài khoản gmail của mình và truy xuất thông tin của tất cả các email chưa đọc từ đó.Lấy tất cả các email chưa đọc bằng cách sử dụng javamail với giao thức POP3
Tôi đã viết mã của mình sau khi giới thiệu nhiều liên kết. Tôi đang đưa ra một vài liên kết để tham khảo.
Send & Receive emails through a GMail account using Java
Java Code to Receive Mail using JavaMailAPI
Để kiểm tra mã của tôi, tôi đã tạo một tài khoản Gmail. Tôi đã nhận được 4 tin nhắn từ Gmail. Tôi chạy ứng dụng của mình sau khi kiểm tra số lượng thư. Điều đó cho thấy kết quả chính xác. 4 thư chưa đọc. Tất cả thông tin đã được hiển thị (ví dụ: ngày, người gửi, nội dung, chủ đề, v.v.)
Sau đó, tôi đăng nhập vào tài khoản mới của mình, đọc một trong các email và chạy lại ứng dụng của tôi. Hiện tại, số lượng thư chưa đọc phải là 3, nhưng nó hiển thị "Số thư chưa đọc: 0"
Tôi đang sao chép mã ở đây.
public class MailReader
{
Folder inbox;
// Constructor of the calss.
public MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, "[email protected]",
"paasword");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* 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(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* 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 (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
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]);
}
}
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();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.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 ..
InputStream is = p.getInputStream();
// 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) {
System.out.write(c);
}
}
public static void main(String args[]) {
new MailReader();
}
}
Tôi đã tìm kiếm trên google, nhưng tôi thấy rằng bạn nên sử dụng Flags.Flag.SEEN để đọc email chưa đọc. Nhưng điều đó không hiển thị kết quả chính xác trong trường hợp của tôi.
Ai đó có thể chỉ ra nơi tôi có thể đang phạm sai lầm?
Nếu bạn cần toàn bộ mã, tôi có thể chỉnh sửa bài đăng của mình.
Lưu ý: Tôi đã chỉnh sửa câu hỏi của mình để bao gồm toàn bộ mã thay vì đoạn trích mà tôi đã đăng trước đó.
Không nên 'Tin nhắn tin nhắn []' là 'Thông báo [tin nhắn ]'? – berbt
Thực ra cả hai đều hợp lệ. Đọc các câu trả lời trên [tại đây] (http://stackoverflow.com/a/1200681/1777090) để biết giải thích chi tiết :) @berbt –
Một chút kiến thức bất ngờ mới. Cảm ơn các liên kết! – berbt