2
Gmail 계정에서받은 편지함의 읽지 않은 메일을 가져 오려고합니다. IMAP는 올바른 수행하는 동안 작은 데모 프로그램을 작성하고 Gmail의 POP3 상황JavaMail API를 사용할 때 Gmail Imap 대 Pop3
- 사용 가능한 폴더 목록을 얻으려고 수많은 예기치 않게 작동하는 것을 발견, POP3 모든 레이블을 단지 INBOX를 반환하지 . 여기에 코드를 삽입하고 있습니다.
POP3
public static Result getPop3FolderList()
{
Properties props = System.getProperties();
props.put("mail.store.protocol", "pop3s");
props.put("mail.pop3.host", "pop.gmail.com");
props.put("mail.pop3.user", Application.email);
props.put("mail.pop3.socketFactory", 995);
props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.port", 995);
Session session = Session.getInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Application.email, Application.pwd);
}
});
try{
Store store=session.getStore("pop3");
store.connect(Application.email,Application.pwd);
javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
String opHtml = "<ul>";
for (javax.mail.Folder folder : folders) {
if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
opHtml += "<li>" + folder.getFullName()+ "+" + folder.getMessageCount() + "</li>";
}
}
opHtml += "</ul>";
return ok(opHtml).as("text/html");
} catch(MessagingException e) {
return ok("Error in getting list.<br />" + e.getMessage()).as("text/html");
}
}
IMAP
public static Result getImapFolderList()
{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Application.email, Application.pwd);
}
});
javax.mail.Store store = session.getStore("imaps");
store.connect("imap.gmail.com", Application.email, Application.pwd);
javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
String opHtml = "<ul>";
for (javax.mail.Folder folder : folders) {
if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
opHtml += "<li>" + folder.getFullName()+ ":" + folder.getMessageCount() + "</li>";
}
}
opHtml += "</ul>";
return ok(opHtml).as("text/html");
} catch (MessagingException e) {
return ok("Error in getting list.<br />").as("text/html");
}
}
- 심지어 메일을 검색 할 때 읽지 않은 메일 필터를 넣으면 Gmail은받은 편지함의 일부가 아니지만 오래 보관 된 읽은 메일을 반환합니다. 반면에 IMAP은 예상대로 작동합니다.
추가 정보 : 단지 새로운 메일이 아니라 내가 잘못 POP3를 사용하거나 Gmail에서 고장 시작
오전 내가 활성화 POP3?