Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > Java   »   reading the gmail or yahoo mail messages using Javamail

 
Question Tools Search this Question Display Modes
Question
 
 
#1  
Old May 15, 2008, 06:22 PM
vasavi
New Member
vasavi is offline
 
Join Date: May 2008
Posts: 5
vasavi See this member's comment history on his/her Profile page.
reading the gmail or yahoo mail messages using Javamail

hi,

I want to connect to gmail or yahoo mail server to fetch the new messages.
I am trying to connect to gmail using the below code:
but i am getting the error


" javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3St ore.java:161)"


what would be the reason... Please do suggest ways to handle it.


Code:
public class TestMail{ public static void main(String[] args) { String user="xxx"; String passwd="yyy"; new TestMail().receive(user,passwd); } public void receive(String username,String password) { String host = "pop3.gmail.com"; try { Properties prop = new Properties(); prop.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); prop.setProperty("mail.pop3.socketFactory.fallback", "false"); prop.setProperty("mail.pop3.port", "995"); prop.setProperty("mail.pop3.socketFactory.port", "995"); prop.put("mail.pop3.host", host); prop.put("mail.store.protocol", "pop3"); Session session = Session.getDefaultInstance(prop); Store store = session.getStore(); System.out.println("your ID is : "+ username); System.out.println("Connecting..."); store.connect(host, username, password); System.out.println("Connected..."); Folder inbox = store.getDefaultFolder().getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message[] msg = inbox.getMessages(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < msg.length; i++) { System.out.println("Subject:" + msg[i].getSubject()); System.out.println("Read message? [Y to read / N to end]"); String line = reader.readLine(); if ("Y".equalsIgnoreCase(line)) { handleMultipart(msg[i]); System.out.println("****************************"); } else if ("N".equalsIgnoreCase(line)) { break; } else {} } if (inbox != null) { inbox.close(true); } if (store != null) { store.close(); } } catch (Exception e) { e.printStackTrace(); } } private void handle(Message msg) throws Exception { System.out.println("From:" + msg.getFrom()[0].toString()); System.out.println("SentDate:" + msg.getSentDate()); } private void handleText(Message msg) throws Exception { handle(msg); System.out.println("Content:"+msg.getContent()); } private void handleMultipart(Message msg) throws Exception { String disposition; BodyPart part; Multipart mp = (Multipart) msg.getContent(); int mpCount = mp.getCount(); for (int m = 0; m < mpCount; m++) { handle(msg); part = mp.getBodyPart(m); disposition = part.getDisposition(); System.out.println(disposition); if (disposition != null && disposition.equals(Part.ATTACHMENT)) { BufferedReader areader = new BufferedReader(new InputStreamReader (System.in)); System.out.println("you get an attachment : " + part.getFileName()); System.out.println("Save attachment? [Y to read / N to end]"); String line = areader.readLine(); if ("Y".equalsIgnoreCase(line)) { saveAttach(part); } else{} } else if(disposition.equals(Part.INLINE)){ System.out.println(part.getContent()); } else {} } } }

thanks & regards,
vasavi

Reply With Quote
 
     

Answers
 
 
Old May 15, 2008, 06:26 PM   #2  
CFZD
Junior Member
CFZD is offline
 
Join Date: Feb 2008
Posts: 133
CFZD See this member's comment history on his/her Profile page.
I can't get on either! So I guess we are not the only ones.
  Reply With Quote
 
     
 
 
Old May 15, 2008, 08:44 PM   #3  
Scleros
Hardware Expert
Scleros is offline
 
Join Date: Feb 2007
Location: Virginia USA
Posts: 744
Scleros See this member's comment history on his/her Profile page.
See Configuring other mail clients.

Blind stab: That document lists POP3 host as "pop.gmail.com" not "pop3.gmail.com" as in your code.
  Reply With Quote
 
     
 
 
Old May 15, 2008, 10:40 PM   #4  
vasavi
New Member
vasavi is offline
 
Join Date: May 2008
Posts: 5
vasavi See this member's comment history on his/her Profile page.
if speciy "pop3.gmail.com" as host

the error i get is
"java.net.UnknownHostException"
  Reply With Quote
 
     
 
 
Old May 16, 2008, 09:59 AM   #5  
Scleros
Hardware Expert
Scleros is offline
 
Join Date: Feb 2007
Location: Virginia USA
Posts: 744
Scleros See this member's comment history on his/her Profile page.
Quote:
Originally Posted by vasavi
if speciy "pop3.gmail.com" as host...

Yes, the correct host is "pop.gmail.com". Your original post's code had "pop3.gmail.com".
  Reply With Quote
 
     


Question Tools Search this Question
Search this Question:

Advanced Search
Display Modes

 
Similar Sponsors

Similar Questions
Question Asker Topic Answers Last Post
Gmail deleted mail recovery pk224 Internet & the Web 2 Aug 10, 2008 03:23 AM
reading the gmail or yahoo mail messages uisng Javamail vasavi Java 0 May 15, 2008 06:19 PM
i need password for my Gmail Yahoo and Hormail account rammohan29 Other Security 1 Aug 4, 2007 08:55 AM
Got kicked off a website b/c of admin reading private messages. can I sue? udontno Other Law 2 Jul 19, 2007 07:59 AM
Gmail poll for mail frequency RickJ Email 0 May 31, 2007 02:52 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 05:41 PM.

Content Relevant URLs by vBSEO 3.0.0 RC6 © 2006, Crawlability, Inc.