使用Java管理gmail。通过“发送到”过滤收件箱

时间:2016-09-01 23:30:17

标签: java email

这是我访问收件箱的示例代码。它有效,但我需要通过收件人过滤邮件,因为我正在使用不同网站的别名(mymail+alias1 @ gmail.com,mymail + alias2 @ gmail.com,mymail + alias3 @ gmail.com,...)。

并且没有message.getDeliveredTo()或类似内容。任何的想法?另一个库?我正在使用java mail api。

String host = "pop.gmail.com";
String username = "mymail@gmail.com";
String password = "password";

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.getInstance(properties);

Store store = emailSession.getStore("pop3s");
store.connect(host, username, password);

Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);

Message[] messages = emailFolder.getMessages();

for (int i = 0; i < messages.length; i++) {
  MimeMessage message = (MimeMessage) messages[i];
  System.out.println("---------------------------------");
  System.out.println(message.getSender());
}

1 个答案:

答案 0 :(得分:1)

Address[] getRecipients()Message方法。每个地址都有其收据类型(TO,CC,BCC)。请参阅Java Mail API:https://javamail.java.net/nonav/docs/api/javax/mail/Message.html#getRecipients-javax.mail.Message.RecipientType-