Solved

my application which check the mail

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now