Need help in retrieving email in gmail

HI!

i just want to ask help on how to retrieve email from my gmail account. i made a simple program that will retrieve the messageCount and print its subject . Heres my code

String host ="pop.gmail.com";

String user ="my_username@gmail.com";

String password ="my_password";

String port ="995";

Properties props =new Properties();

props.put("mail.pop3.host", host);

props.put("mail.pop3.user", user);

props.put("mail.pop3.port", port);

props.put("mail.pop3.starttls.enable","true");

props.put("mail.pop3.auth","true");

props.put("mail.pop3.socketFactory.port", port);

props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");

props.put("mail.pop3.socketFactory.fallback","false");

Session session = Session.getDefaultInstance(props, auth);

try{

Store store = session.getStore("pop3");

try{

store.connect(host, user, password);

}catch (MessagingException ex){

ex.printStackTrace();

}

try{

if(store.isConnected()){

Folder inboxFolder = store.getFolder("INBOX");

inboxFolder.open(Folder.READ_WRITE);

System.out.println("Message Count:" + inboxFolder.getMessageCount());

Message[] msg = inboxFolder.getMessages();

int len = msg.length;

for(int i = 0; len < len; i++){

System.out.println("subject: " + msg[i].getSubject());

}

}

}catch (MessagingException ex){

ex.printStackTrace();

}

}catch (NoSuchProviderException ex){

ex.printStackTrace();

}

My problem with this code is when i try to run the program the messageCount method return 0 and the subjects are not printed.

[3029 byte] By [Mark_Ramosa] at [2007-11-26 16:09:30]
# 1
http://java.sun.com/products/javamail/FAQ.html#gmailNote also that the messages visible to POP clients iscontrolled by settings in GMail. Go to the Settings pagein GMail.
bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

The previous program i made is already working i just change the username@gmail.com into username only. But I encounter another problem with my program. When i try to run the program and get the message count, it only return 2 as the message count value.

Heres my code

AccountAuthentication auth = new AccountAuthentication();

String host = "pop.gmail.com";

String user = "username";

String password = "password";

String port = "995";

Properties props = new Properties();

props.put("mail.pop3.host", host);

props.put("mail.pop3.user", user);

props.put("mail.pop3.port", port);

props.put("mail.pop3.starttls.enable","true");

props.put("mail.pop3.auth", "true");

props.put("mail.pop3.socketFactory.port", port);

props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.pop3.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props, auth);

try {

Store store = session.getStore("pop3");

try {

store.connect(host, user, password);

} catch (MessagingException ex) {

ex.printStackTrace();

}

try {

if(store.isConnected()){

Folder folder = store.getFolder("INBOX");

folder.open(Folder.READ_WRITE);

Message[] msg = folder.getMessages();

int len = folder.getMessageCount();

System.out.println("Message Count: " + len);

for(int i = 0; i < len; i++){

String subject = msg[i].getSubject();

System.out.println("Subject[" + i + "]: " + subject);

}

}else{

System.out.println("Unable to open the Inbox.");

}

} catch (MessagingException ex) {

ex.printStackTrace();

}

} catch (NoSuchProviderException ex) {

ex.printStackTrace();

}

and heres the account authentication code

public class AccountAuthentication extends javax.mail.Authenticator{

String user = "username@gmail.com";

String password = "password";

public PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(user, password);

}

}

and heres the output when i try to run the program

Message Count: 2

Subject[0]: Sun Developer Forums discussion on "Need help in retrieving email in gmail" has been updated by bshannon

Subject[1]: Lean Manufacturing, Jan07: AMR Gives High Marks to Oracle MES, Big Benefits from Six Sigma, and more

im hoping for your reply thanks!

Mark_Ramosa at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Read my previous message again. The answer is there.
bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Thank you "bshannon" for helping me. I've finally solved my problem i just follow your advice that it is in the setting of my gmail account. Thanks again.
Mark_Ramosa at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hi all-

I was wondering if anyone has had any trouble using "javax.mail.Folder.search(SearchTerm)" to retrieve messages from pop.gmail.com?

I am able to retrieve messages via Folder.search with other POP servers but wilth gmail, it always returns a zero-length array.

I able to retrieve messages via the "getMessages()" method so actual connectivity is not the issue here.

THANKS!!!

langala at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

First, you need to know that Folder.search for POP3 is implemented by

downloading all the messages and searching them on the client.

Unlike IMAP, POP3 supports no server-side search capability.

If search is failing to find a message that you know exists, it's probably

because your GMail settings are such that the message isn't being

downloaded to the client.

If you're sure the message is accessible to JavaMail, but search isn't

finding it, you'll need to provide more details.

bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
I am very new to JavaMail. I have been going through lots of Tutorial and following the Code given there. But while Compiling the Codes i am facing lots of problems. Could you please give me some Step by Step Intruction To send an email From JSP Page
jofin123a at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
The step by step instructions were in the stuff you read.If they're not working for you, you're probably doing somethingwrong. But since you didn't tell us what you're doing andwhat errors you're getting, it's hard to help you.
bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Thanks for the help bshannon!

The information you supplied will help me trace the problem.

I was actually getting messages thru "getMessages" so now I know the problem is on "my" side and thus "fixable".

This would also explain some other things I'm seeing such as no Received dates being retrieved - which may very well explain the search problems since I am searching by received date.

I was worried that the problem was with Gmail but it's good to know that it's probably some detail I missed. =)

Thanks again for the quick response!!

langala at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
The POP3 protocol also doesn't supply a received date.Be sure to read the javadocs for the com.sun.mail.pop3 package: http://java.sun.com/products/javamail/javadocs/com/sun/mail/pop3/package-summary.html
bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11

Hi bshannon,

I did everything exactly as it's written in help. I got msgshow.java from javamail-1.4\demo (I believe this is your code), compiled and run as

java msgshow -D -T pop3s -H pop.gmail.com -U user -P passwd

And I got error "Could not find trusted certificate". I know that I have to add appropriate certificate to jre\lib\security\cacert file (Windows path).

But where can I obtain gmail.com server certificate?

Could you help me?

vnevarkoa at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 12
http://java.sun.com/products/javamail/FAQ.html#installcert
bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 13

Hi bshannon,

Thank you very much. I folollowed instructions, got and implemented certificates for pop.gmail.com and smtp.gmail.com. It works!. I can read emails now.

BUT, I'm not able to send through my gmail account. I run smtpsend sample program. Problem is not with certificate now. It's all about authentication. I'm passing username and password, but I got "Authentication Required " anyway.

Why did I get "useAuth false", "250-AUTH LOGIN PLAIN" and "AUTH", arg "LOGIN PLAIN"?

Why was exception thrown form SMTPTransport.mailFrom?

java -Dmail.smtp.port=465 -Dmail.smtp.starttls

.enable=true smtpsend -M smtp.gmail.com -U user@gmail.com -P password -s

SendTest -o user@gmail.com -A -S -v -d user@hotmail.com

To: user@hotmail.com

Subject: SendTest

DEBUG: setDebug: JavaMail version 1.4ea

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.

smtp.SMTPSSLTransport,Sun Microsystems, Inc]

DEBUG SMTP: useEhlo true, useAuth false

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true

220 mx.google.com ESMTP f16sm2660734qba

DEBUG SMTP: connected to host "smtp.gmail.com", port: 465

250-mx.google.com at your service

250-SIZE 20971520

250-8BITMIME

250-AUTH LOGIN PLAIN

250 ENHANCEDSTATUSCODES

DEBUG SMTP: Found extension "SIZE", arg "20971520"

DEBUG SMTP: Found extension "8BITMIME", arg ""

DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"

DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""

DEBUG SMTP: use8bit false

MAIL FROM:<user@gmail.com>

530 5.5.1 Authentication Required f16sm2660734qba

com.sun.mail.smtp.SMTPSendFailedException: 530 5.5.1 Authentication Required f16

sm2660734qba

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1

388)

at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)

at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)

at smtpsendemail.main(smtpsendemail.java:217)

Response: 530 5.5.1 Authentication Required f16sm2660734qba

QUIT

DEBUG SMTP: EOF: [EOF]

SMTP SEND FAILED:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.5.1 Authentication Required f16

sm2660734qba

Command: MAIL FROM:<user@gmail.com>

RetCode: 530

Response: 530 5.5.1 Authentication Required f16sm2660734qba

Could help me again?

Thanks a lot

vnevarkoa at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 14

If you follow the instructions in the FAQ, you won't have this problem.

The version of smtpsend.java that comes with JavaMail 1.4 has

a bug that causes it to not set the correct properties when using

the "smtps" protocol (-S option); it instead sets the "smtp" properties.

You can find the fixed version of smtpsend.java in the GlassFish

cvs repository, if you really need it.

bshannona at 2007-7-8 22:31:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 15
I fixed smtpsend.java myself. It works!!! I've sent email successfully using smtps and authentication.Thanks a lot.
vnevarkoa at 2007-7-21 16:40:03 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...