Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

my application which check the mail

Posted on 2003-10-23
2
Medium Priority
?
182 Views
Last Modified: 2010-03-31
hi,
      i have done the application using java mail API which is used to check the mail in u

r pop3 server account.

the source code of my application follows

//source code

import java.net.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.lang.*;

class getmail
{
 private String host,username,password;
 private String provider = "pop3";
 private Properties props;
 private Session session;
 private Store store;
 private Folder inbox;
 private Message message1;
getmail(String host,String username,String password)
 {
   this.host = host;
   this.username = username;
   this.password = password;
  }
 
getmail(String username,String password)
 {
   this.username = username;
   this.password = password;
   host = "mail.uk2.net";
  }
public String[] connect()
 {
      int no;
      String[] from=null,nomess=null;
              try{
                  props = new Properties();
                  session = Session.getDefaultInstance(props,null);
                  store = session.getStore(provider);
                  store.connect(host,username,password);
                  if(inbox == null)
                  {
                        from = new String[1];
                        from[0]="nomail";
                  }
                  else
                  {
                  inbox = store.getFolder("INBOX");
                  inbox.open(Folder.READ_ONLY);
                              
                  Message[] message = inbox.getMessages();
                  System.out.println("count:"+inbox.getMessageCount());
                  from = new String[message.length];                  
      
                  for(int i=0;i< message.length;i++)
                  {
                         from[i] = InternetAddress.toString(message[i].getFrom());      
                  }
                  inbox.close(false);
                  }
            }
            catch(Exception e)
            {
                  e.printStackTrace();
                  //System.out.println("error:"+e);
            }
      return from;
 }
 
public void disconnect()throws Exception
{
 store.close();
}
                  
public void  openmessage(int i)throws Exception
{
      try{
            inbox.open(Folder.READ_ONLY);
            message1=inbox.getMessage(i);
            
      }
      catch(Exception e){}
}

public String getfrom()throws Exception
{
      return InternetAddress.toString(message1.getFrom());
}

public Date getdate()throws Exception
{
       return message1.getSentDate();
}

public String getsubject()throws Exception
{
       return message1.getSubject();
}

public String getcontent()throws Exception
{
      int i;
      InputStream in ;
      char ch[]=null;
      String content="";
      try{
      in = message1.getInputStream();
      ch = new char[in.available()];
      for(i=0;i<in.available();i++)
      {
            ch[i]=(char)in.read();
            content=content+ch[i];
      }
      
      }
      catch(Exception e){}
      return content;
}
}






public class test
{
 public static void main(String args[])throws Exception
 {
   int checkmail =0;
   getmail get=new getmail("deeppra","pradeep");
   String[] allmess = get.connect();
   if(allmess[0] == "nomail")
      System.out.println("No mail in Inbox");
   else
   {
      checkmail = 1;
      for(int i=0;i< allmess.length;i++)
      {
      
            System.out.println("size:"+allmess.length);
            System.out.println("inside for:"+allmess[i]);
      }
    }
 
   if(checkmail == 1)
   {
      get.openmessage(1);
      System.out.println("from:"+get.getfrom());
      System.out.println("Subject:"+get.getsubject());
      System.out.println("date:"+get.getdate());
      System.out.println("message:");
      System.out.println(get.getcontent());
    }
  get.disconnect();
}
}
//end of code

in the above code it check the pop3 mail account,from mail.uk2.net
username : deeppra@uk2.net
password :pradeep

       when i run the above program it gives only one mail from my inbox,but when i checked

my accout by login to my acccount there was no mail in the inbox and for testing i send the

mail to the mail id deeppra@uk2.net from my yahoo mail account and the mail is received in

the inbox,but then i run the program then also i am getting the same output.plz check where

is the error in the program and give me the modified program which gives me all the mails in

my inbox in the mail account deeppra@uk2.net

thank u
0
Comment
Question by:deeppradeep
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 60 total points
ID: 9612421
> when i run the above program it gives only one mail from my inbox,

It looks like you only open one message from your inbox.
You should loop over the following code for each message, calling
get.openmessage() on each message.

 {
     get.openmessage(1);
     System.out.println("from:"+get.getfrom());
     System.out.println("Subject:"+get.getsubject());
     System.out.println("date:"+get.getdate());
     System.out.println("message:");
     System.out.println(get.getcontent());
    }
0
 

Author Comment

by:deeppradeep
ID: 9663379
> when i run the above program it gives only one mail from my inbox,

>It looks like you only open one message from your inbox.
>You should loop over the following code for each message, calling
>get.openmessage() on each message.

 {
     get.openmessage(1);
     System.out.println("from:"+get.getfrom());
     System.out.println("Subject:"+get.getsubject());
     System.out.println("date:"+get.getdate());
     System.out.println("message:");
     System.out.println(get.getcontent());
    }

i the above comment u told that to loop the above code,but u r opening the first message in the inbox,then how can we get all the message.
and also i am getting the message count as 1,that means it referres as only one mail.
plz give me the modified code for checking all the mails from my inbox.
thank u


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question