Solved

my application which check the mail

Posted on 2003-10-23
2
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSV file parsing thru Java 13 39
ejb entity bean example 2 31
restrict decimal places for double datatype 10 29
learn programming 8 66
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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