开发者

How to set the email protocol in JavaMail

开发者 https://www.devze.com 2022-12-25 19:04 出处:网络
If I need 开发者_运维知识库to get inbox messages by passing request from servlets to javamail API , how can I know the protocol in which to retrieve inbox messages? Do I have to state the protocol in

If I need 开发者_运维知识库to get inbox messages by passing request from servlets to javamail API , how can I know the protocol in which to retrieve inbox messages? Do I have to state the protocol in request URL?

I've already checked in gmail, where they haven't stated any protocol, then How can I get inbox messages based on particular protocol like: POP3 or IMAP


The protocol is specified in the configuration properties:

mail.store.protocol=pop3

mail.pop3.host=...
...


Receiving email using JavaMail is possible for both POP3 and IMAP. Here's an example:

Properties props = System.getProperties();
session = Session.getInstance(props, null); // get a mail session
store = session.getStore("imap"); // get relevent store type, in this case IMAP
store.connect(mailserver, username, password); // connect to mail server
defaultFolder = store.getDefaultFolder(); // get default or root folder
inboxFolder = defaultFolder.getFolder("INBOX"); // find and get INBOX folder
inboxFolder.open(Folder.READ_ONLY); // open the folder
Message[] inboxMessages = inboxFolder.getMessages(); // get all the messages

Change the store type to "pop3" in Session.getStore() for POP3 access.

Oh, btw, GMail supports both POP and IMAP - it depends on whether the user has enabled those kinds of access. If you login to your GMail account, look under 'Settings' and 'Forwarding and POP/IMAP' tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消