我的电子邮件程序未正确检索邮件。
之前用于显示javax.mail.internet.MimeMultipart@787171之类的内容 现在,我使用BodyPart()解决了它,它无法检索已经检索过的电子邮件。有趣的是,电子邮件没有从我的帐户中删除,它只是不会计数或再次显示它们。此外,这是一个很长的镜头,但有什么我可以这样做,如果它包含一个附件,它将显示文本,但忽略附件?
输出: 0.退出 1.切换用户 2.撰写电子邮件 3.阅读电子邮件 4.更改详细信息 5.添加用户 6.删除用户 3
电子邮件号码:1 学科: 来自:XXXX XXXX 文本: 测试
这是电子邮件检索背后的代码:
import javax.mail.internet.*;
import java.util.Properties;
import javax.mail.*;
import java.util.*;
public class CheckingMails {
public static void check(String user,
String password)
{String host = "pop.gmail.com";
String storetype="pop3";
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("Number of emails: " + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number: " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
MimeMultipart mmp = (MimeMultipart) message.getContent();
System.out.println("Text: ");
for (int xyz=1; xyz<mmp.getCount(); xyz++) {
System.out.println(mmp.getBodyPart(i).getContent());
}
}
//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();
}
}
public static void main(String[] args) {
String host = "pop.gmail.com";// change accordingly
String mailStoreType = "pop3";
String username = "yourmail@gmail.com";// change accordingly
String password = "*****";// change accordingly
//check(host, mailStoreType, username, password);
}
}