Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve mails from POP 3 server

 
Himanshu Bisht
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!!

I want to make an application which connects to my mail server(say rediff for example) takes a user id and password and downloads all the mails in the INBOX.

I have a program but it always give unknown host error.Can anyone suggest how to go about it and what all things i will be needing.

Thanks
Himanshu
 
Jeremy Wilson
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is code I used
Store store=null;
Folder folder=null;
try
{

Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
folder = store.getDefaultFolder();
if (folder == null) throw new Exception("No default folder");
folder = folder.getFolder("INBOX");
if (folder == null) throw new Exception("No POP3 INBOX");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
for (int msgNum = 0; msgNum < msgs.length; msgNum++)
{
System.out.println(msgs[msgNum].getSubject());
}

}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
// -- Close down nicely --
try
{
if (folder!=null) folder.close(false);
if (store!=null) store.close();
}
catch (Exception ex2) {ex2.printStackTrace();}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic