Reading Gmail Inbox problem.

I have 2 problems.

1. by default it reads all the mails from the "all mail " folder. if i comment the code folder = store.getDefaultFolder(); it doesnt work.

2. i need to save all the attachments in a separate folder., given in saveFile() method but by default all attachment names are coming as null. Please help me out with tht

import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

publicclass FetchMail

{

String host =null;

String user=null;

String pswd =null;

Authenticator auth =null;

String bodytext =null;

Vector attachments;

public FetchMail(Hashtable hashConfigParams)

{

host = (String)hashConfigParams.get("popServer");

auth =new SMTPAuthenticator((String)hashConfigParams.get("popUser"), (String)hashConfigParams.get("popPswd"));

attachments=new Vector();

}

publicvoid receive()throws Exception

{

Store store=null;

Folder folder=null;

Folder inboxfolder =null;

try{

Properties props = System.getProperties();

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

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

props.setProperty("mail.pop3.port","995");

props.setProperty("mail.pop3.socketFactory.port","995");// for gmail */

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

props.put("mail.debug","true");

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

store = session.getStore("pop3");

store.connect();

folder = store.getDefaultFolder();

if (folder ==null)thrownew Exception("No default folder");

inboxfolder = folder.getFolder("INBOX");

if (inboxfolder ==null)

{

thrownew Exception("No POP3 INBOX");

}

inboxfolder.open(Folder.READ_ONLY);

Message[] msgs = inboxfolder.getMessages();

for (int i = 0; i < msgs.length; i++)

{

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

printMessage(msgs[i]);

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

finally

{

try

{

if (folder!=null)

folder.close(false);

if (store!=null)

store.close();

}

catch (Exception ex2)

{

ex2.printStackTrace();

}

}

}

publicvoid printMessage(Message message)

{

try

{

String body="";

String from = ((InternetAddress)message.getFrom()[0]).getPersonal();// gets name of sender

if (from==null)

{

from = ((InternetAddress)message.getFrom()[0]).getAddress();//get email id of sender

}

System.out.println("FROM: "+from);

String subject = message.getSubject();

System.out.println("SUBJECT: "+subject);

Part messagePart = message;

Object content = messagePart.getContent();

if (contentinstanceof Multipart)

{

messagePart=((Multipart)content).getBodyPart(0);

}

String contentType = messagePart.getDisposition();

if ((contentType !=null) && ((contentType.equals(Part.ATTACHMENT) || contentType.equals(Part.INLINE))))

{

Attachment attachment=new Attachment();

attachment.setFilename(messagePart.getFileName());

ByteArrayOutputStream bos=new ByteArrayOutputStream();

InputStream ip = messagePart.getInputStream();

byte[] buffer=newbyte[8192];

int count=0;

while((count=ip.read(buffer))>=0)

bos.write(buffer,0,count);

ip.close();

attachment.setContent(bos.toByteArray());

attachments.add(attachment);

saveFile(attachments);

}

BufferedReader reader=new BufferedReader(new InputStreamReader(messagePart.getInputStream()));

//start reading line after line

String b=reader.readLine();

while (b!=null)

{

b=reader.readLine();

body += b +"\n";

}

if(body.equals(""))

System.out.println("NO BODY FOR THIS MSG");

else

System.out.println("BODY: \n" + body);

System.out.println("ENd OF BODY--");

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

publicvoid saveFile(Vector vecAttachments)throws IOException

{

try

{

String path="C:\\trainee\\tempdir";

for(int i=0; i < vecAttachments.size();i++)

{

System.out.println(" ATTACHMENT" +i +" : " + vecAttachments.get(i));

Attachment Obj = (Attachment)vecAttachments.get(i);

File file =new File(path +"\\"+ Obj.getFilename());

if(file.exists())

{

file.delete();

}

file.createNewFile();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

publicstaticvoid main(String args[])

{

Hashtable hashObj =new Hashtable();

hashObj.put("popServer","pop.gmail.com");

hashObj.put("popUser","nb123@gmail.com");

hashObj.put("popPswd","password");

try

{

FetchMail objFm =new FetchMail(hashObj);

objFm.receive();

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

class Attachment{

private String contenttype;

private String filename;

privatebyte[] content;

private String contentid;

public String getContenttype(){

return contenttype;

}

publicvoid setContenttype(String contenttype){

this.contenttype = contenttype;

}

public String getFilename(){

return filename;

}

publicvoid setFilename(String filename){

this.filename = filename;

}

publicbyte[] getContent(){

return content;

}

publicvoid setContent(byte[] content){

this.content = content;

}

public String getContentid(){

return contentid;

}

publicvoid setContentid(String contentid){

this.contentid = contentid;

}

}

[12356 byte] By [nb123a] at [2007-11-26 15:19:34]
# 1

Which messages are seen by the client is a function of the POP3 protocol

(see the FAQ) and the gmail settings you've chosen (see the gmail settings page).

Whether attachments have names or not is controlled by the program that creates

the message. There is no requirement that attachments have names, so your

program probably needs to handle that case.

If you think there might be something wrong with how you're handling messages

in your program, such that you're not getting the attachment name correctly, try

using the msgshow.java demo program that comes with JavaMail.

bshannona at 2007-7-8 11:46:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
i checked for the settings in gmail. there's only a pop filter for all mail and incoming mail from now onwards...it doesnt serve the purpose of reading mail only from the inbox.PLEASE HELP.
nb123a at 2007-7-8 11:46:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

dont forget to change the username and password

/*

* FetchMail.java

*

* Created on 04 December 2006, 11:53

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

import java.util.*;

import java.io.*;

import java.sql.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.mail.search.*;

import javax.activation.*;

public class FetchMail {

public static void receive(String popServer, String popUser, String popPassword)

throws Exception {

Store store=null;

Folder folder=null;

try {

Properties props = System.getProperties();

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

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

props.setProperty("mail.pop3.port", "995");

props.setProperty("mail.pop3.socketFactory.port", "995");

Session session = Session.getInstance(props);

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++) {

String subject=msgs[msgNum].getSubject();;

printMessage(msgs[msgNum]);

//msg.invalidate(true);

}

} catch (Exception ex) {

ex.printStackTrace();

} finally {

try {

if (folder!=null) folder.close(false);

if (store!=null) store.close();

} catch (Exception ex2) {

ex2.printStackTrace();

}

}

}

public static void printMessage(Message message) {

try {

String from = ((InternetAddress)message.getFrom()[0]).getPersonal();

if (from==null) {

from = ((InternetAddress)message.getFrom()[0]).getAddress();

}

System.out.println("FROM: "+from);

String subject = message.getSubject();

System.out.println("SUBJECT: "+subject);

System.out.println("--");

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String args[]){

FetchMail fm=new FetchMail();

try {

fm.receive("pop.gmail.com","yourusername","yourpassword");

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

G_Abubakra at 2007-7-8 11:46:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

> i checked for the settings in gmail. there's only a

> pop filter for all mail and incoming mail from now

> onwards...it doesnt serve the purpose of reading mail

> only from the inbox.

The POP3 protocol only knows about one mailbox.

Which messages gmail makes available to the client

in that one mailbox are entirely up to gmail.

bshannona at 2007-7-8 11:46:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
hi G_Abubakr,its still not reading from the inbox only. i understand it must be somethign to do with the gmail settingsbut could u please help me out with the Attachment part. how do i save it locally?a code snippet would be really helpful.
nb123a at 2007-7-8 11:46:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...