[JAVA100 example]046, receiving messages using the SMTP protocol

Source: Internet
Author: User

Import java.io.*;


import java.net.*;


/**


* <P>TITLE:SMTP protocol receive mail </p>


* <p>description: Connect POP3 server via socket, use SMTP protocol to receive mail </p>
in mail server

* <p>copyright:copyright (c) 2003</p>


* <p>filename: </p>


* @version 1.0


*/


class Pop3demo


{


/**


*<br> Method Description: Main method, receive user input


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


if (args.length!=3) {


System.out.println ("Use:java pop3demo mailhost user password");


  }


new Pop3demo (). Receive (args[0],args[1],args[2]);


 }


/**


*<br> Method Description: Receive mail


*<br> input parameter: String popserver server Address


*<br> input parameter: String popuser mailbox User name


*<br> input parameter: String Poppassword mailbox Password


*<br> return type:


*/


public static void receive (String popserver, String popuser, string Poppassword)


 {


String pop3server = popserver;


int pop3port = 110;


Socket client = null;


Try


  {


//Create a socket that is connected to the POP3 service program.


client = new Socket (Pop3server, Pop3port);


//Creates a BufferedReader object to read the output from the socket.


InputStream is = Client.getinputstream ();


BufferedReader Sockin;


Sockin = new BufferedReader (new InputStreamReader (IS));


//Creates a PrintWriter object to write content to the socket.


outputstream OS = Client.getoutputstream ();


PrintWriter sockout;


Sockout = new PrintWriter (OS, true); True for Auto-flush


//Display POP3 handshake information.


System.out.println ("S:" + sockin.readline ());





/*--handshake process with POP3 server--*/


System.out.print ("C:");


String cmd = "user" +popuser;


//Send user name to POP3 service program.


System.out.println (CMD);


sockout.println (CMD);


//Read the POP3 Service program's response message.


String reply = Sockin.readline ();


System.out.println ("S:" + Reply);


System.out.print ("C:");


cmd = "pass";


//Send the password to the POP3 service program.


System.out.println (cmd+ "*********");


sockout.println (Cmd+poppassword);


//Read the POP3 Service program's response message.


reply = Sockin.readline ();


System.out.println ("S:" + Reply);





    


System.out.print ("C:");


cmd = "stat";


//Get mail data.


System.out.println (CMD);


sockout.println (CMD);


//Read the POP3 Service program's response message.


reply = Sockin.readline ();


System.out.println ("S:" + Reply);


if (reply==null) return;


System.out.print ("C:");


cmd = "Retr 1";


//will receive the first abundance mail commands to the POP3 service program.


System.out.println (CMD);


sockout.println (CMD);





//entered the RETR command and returned a successful response code, continuously reading the output from the socket,


//until you meet <CRLF>.<CRLF>. The output that is read from the socket is the content of the message.


if (Cmd.tolowercase (). StartsWith ("RETR") &&


Reply.charat (0) ==´+´)


do


    {


reply = Sockin.readline ();


System.out.println ("S:" + Reply);


if (reply!= null && reply.length () > 0)


if (Reply.charat (0) ==´.´)


break;


    }


while (true);


cmd = "quit";


//sends the command to the POP3 service program.


System.out.print (CMD);


sockout.println (CMD);


  }


catch (IOException e)


  {


System.out.println (e.tostring ());


  }


finally


  {


Try


{if (client!= null)


Client.close ();


   }


catch (IOException e)


   {


   }


  }


 }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.