Solved

my application which check the mail

Posted on 2003-10-23
2
170 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 20 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 40
xampp tool 12 49
java jdbc batch example 8 32
CSV file parsing thru Java 13 28
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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