Solved

my application which check the mail

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 102
eclipse formatting 6 73
Why does the Android Studio logcat show this error when the user presses the button? 2 42
Apps blocked by Java 9 64
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

895 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