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;
}
}

