Friday 3 February 2012


POP3 Implementation

Code:
/*POP3 Implementation*/
import java.net.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class PopClient
{
   String user="";
   String pass="";
    int c=13,d=10;
    Socket s;
    BufferedReader br;
    BufferedWriter bw;
    String temp="";
   BufferedReader bri=new BufferedReader(new InputStreamReader(System.in));
PopClient() throws Exception
{
System.out.print("Enter pop3 server :");
connectAndRetreiveEmail(bri.readLine());
}
private void connectAndRetreiveEmail(String strPOP3Server) throws Exception
{
s=new Socket(strPOP3Server,110);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String temp1="";
if(s.isConnected())
{
System.out.println("connected");
System.out.println(br.readLine());
while(true)
{
if(authenticate())
{
while(!temp1.toLowerCase().equals("quit"))
{
System.out.println("\nType appropriate # for required service :");
System.out.println("\t1. To Read Message");
System.out.println("\t2. To Delete Message");
System.out.println("\t3. To Quit And Save Changes to Inbox");
System.out.println("\t4. To Quit Without Saving Changes to Inbox");
temp1=bri.readLine();
if(temp1.equals("1"))
{
displayEmail();
}
else if(temp1.equals("2"))
{
delEmail();
}
else if(temp1.equals("3"))
{
quit();
break;
}
else if(temp1.equals("4"))
{
System.out.println("Quiting without saving changes to Inbox...");
break;
}

}
break;
}
else
{
System.out.println("For giving invalid information your connection is terminated!! Logging Off...");
break;
}
}
}

s.close();
}


private boolean authenticate() throws Exception
{
System.out.println("\n\t\t\t\tAUTHENTICATION");
System.out.println("\t\t\t\t--------------");
System.out.print("Please Enter User Name :");
user=bri.readLine();
System.out.print("Please Enter password :");
pass=bri.readLine();
//System.out.println();
bw.write("User "+user+"\n\r");
bw.flush();
br.readLine();
bw.write("pass "+pass+"\n\r");bw.flush();
System.out.println("Waiting for server response...");
temp=br.readLine();
if(temp.substring(0,0).equals("-ERR"))
{
System.out.println("UserName or Password invalid");
return false;
}
else
{
System.out.println(temp);
return true;
}

}

private void delEmail() throws Exception
{
System.out.println("\n\t\t\t\tEMAIL DELETION");
System.out.println(  "\t\t\t\t----- --------");
showList();
System.out.print("Please Enter the message no. present in the list show above to delete the message : ");
bw.write("dele "+bri.readLine()+"\n\r");
bw.flush();
temp=br.readLine();
if(temp.substring(0,0).equals("-ERR"))
{
System.out.println("U have given invalid message no.");
}
else
System.out.println(temp);
}

private void showList() throws Exception
{
System.out.println("\n\t\t\t\tEMAIL LIST");
System.out.println(  "\t\t\t\t----- ----");
System.out.println("List of the available email messages");
bw.write("List\n\r");
bw.flush();
temp=br.readLine();
while(!temp.equals("."))
{
System.out.println(temp);
temp=br.readLine();
}
System.out.println(temp);

}

private void displayEmail() throws Exception
{

System.out.println("\n\t\t\t\tDISPLAYING EMAIL");
System.out.println(  "\t\t\t\t---------- -----");
showList();
System.out.print("Please Enter the message no. present in the list show above to read the message : ");
bw.write("retr "+bri.readLine()+"\n\r");
bw.flush();
temp=br.readLine();
if(temp.substring(0,0).equals("-ERR"))
{
System.out.println("U have given invalid message no.");
}
else
while(!temp.equals("."))
{
System.out.println(temp);
temp=br.readLine();
}
System.out.println(temp);

}

private void quit() throws Exception
{
System.out.println("Quiting and saving changes to Inbox ....");
bw.write("quit\n\r");
bw.flush();
System.out.println(br.readLine());
}

public static void main(String args[]) throws Exception
{
new PopClient();
}

}

Download activation.jar and mail.jar.
Paste both the jar files in C:\Program Files\Java\jdk1.7.0_02\jre\lib\ext and
C:\Program Files\Java\jre7\lib\ext.
Set path to the folder where the program PopClient.java is saved
Set class path to the folder where program PopClient.java is saved.
Compile the program ie.javac PopClient.java
Run the program ie java PopClient

PREREQUISITE:
Login to your yahoo email account, Options->Mail Options->POP and Forwarding
Select Access Yahoo! Mail via POP option and save the changes.
Firewall settings,AntiVirus settings must be off.

Specify the pop domain (example in.pop.mail.yahoo.com),
user name (example abc@yahoo.in) and
password

Sample Outputs:

Related Posts :



1 comment: