How to use Java to write a Java Applet that can send and receive E-mail according to SMTP and POP3 protocols.

xiaoxiao2021-03-06  82

One. Java network programming initial

1. Establish TCP / IP connection

Constructor Socket (String Host, INT Port) in the Socket class is used to create a socket and connect to the specified host (Host) and port (port). The host name cannot be specified by the Java Applet safety mechanism, and the name of the host in which the called Applet is located is the same. Therefore, you can get the URL of the Class file currently read by the browser first by the getCodeBase method, and then returns the name of the host represented by the string from the URL by the gethost method. For example from

Http://person.zj.cninfo.net/~caveman/ This URL can return to the hostname "Person.zj.cninfo.Net" represented by the string.

Regarding the value of the standard port address is generally: SMTP is 25, POP3 is 110, FTP is 21, etc., of course, some hosts are used by non-standard ports, it is best to determine it before use.

2. Data transmission and reception

The GetOutputStream method in the Socket class can obtain the output data stream corresponding to the current Socket, and write the data into the output data stream with the WriteBytes method in the DataOutputStream class.

Using the GetInputStream method in the Socket class, you can get the input data stream corresponding to the current Socket, with the Readline method in the DataInputStream class, can read the data in the input data stream.

3. Turn off TCP / IP connection

The TCP / IP connection can be turned off in the CLOSE method in the Socket class.

two. SMTP protocol Send E-mail Java Applet

1. SMTP protocol and server

The SMTP (Simple Mail Transfer Protocol) protocol is currently popular online, and there are 14 commands in the SMTP protocol. However, it is sufficient to send an E-mail only need to use the following 5 commands (see Table 1).

Table 1

Leading function

Helo shakes hands with the SMTP server to transfer this domain name

Mail from: Transfer sender's mailbox name

RCPT to: Transfer recipient's mailbox name

Data Send letter data (including letterhead and letter)

Quit Exits the connection to the SMTP server

In addition to the SMTP server provided by ISP, some SMTP ports of the server stored for free personal homepage are also open. If the server does not increase the RELAY limit (such as Netease Nease.net), then It can be used as a SMTP server. Such as Person.zj.cninfo.net, etc., put a Java applet to be compiled and call it on these servers, and users can send E-mail online.

2. Main procedures in SMTPTester.java

The Java Applet uses three TextField components TF1, TF2, and TF3, which are used to enter the user's mailbox name, the recipient's mailbox name, and the title of the letter; also used two text districts ( TEXTAREA) Parts TA1 and TA2, TA1 For users to enter the letter content, TA2 is used to display the response information and error information of the SMTP server; of course, a button (Button component bu 1. After the user enters the correct information, use the mouse Click on it, the letters can be sent out. Event capture handler See programs 1:

Public Boolean Handleevent (Event Event1) {

IF ((Event1.target! = BU1) || (Event1.id! = 1001)))

/ * If not the button BU1 is pressed by the mouse * /

{

Return False;

}

IF ((tf1.gettext (). Length ()! = 0) && (tf2.gettext (). Length ()! = 0))

/ * If the sender name of the sender and the recipient is filled, execute the sender bu1run * /

{bu1.disable ();

Bu1run ();

Return True;

}

Else {

TA2.AppendText ("" "" "" "" "" "" "" "" "" "" "" "" "" "" "

Return True;

}

(Program 1)

Send E-mail programs bu1run see programs 2:

Public void bu1run () {

Try {

Socket Socket1 = New Socket (). Gethost (), 25);

/ * Establish connection with the SMTP server * /

DataOutputStream DataOut2 = New DataOutputStream (Socket1.getOutputStream ());

DataInputStream DataIN3 = New DataInputStream (Socket1.GetInputStream ());

GetReply (DataIn3);

Dataout2.writebytes ("Helo Person.zj.cninfo.net/r/N");

/ * Started by SMTP protocol * /

GetReply (DataIn3);

DataOut2.writebytes ("Mail from:" TF1.GetText () "/ r / n");

GetReply (DataIn3);

DataOut2.writebytes ("RCPT TO:" TF2.Gettext () "/ r / n");

GetReply (DataIn3);

DataOut2.writebytes ("DATA / R / N");

GetReply (DataIn3);

DataOut2.writebytes ("from:" tf1.gettext () "/ r / n");

DataOut2.writebytes ("to:" tf2.gettext () "/ r / n");

Dataout2.writebytes ("Subject:" Tf3.Gettext () "/ n / n" Ta1.getText () "/ r / n. / r / n");

GetReply (DataIn3);

DataOut2.writebytes ("quit / r / n"); getReply (datain3);

Socket1.close ();

/ * Exit connection * /

Bu1.enable ();

Return;

}

Catch (java.ioException ioException0) / * Capture IO error * /

{

TA2.AppendText ("Error! / N");

Bu1.enable ();

Return;

}

}

(Program 2)

Receive Server Answer GetReply See Program 3:

Void getReply (DataInputStream DataIn1)

{

String string2;

String2 = "";

Try

{

String2 = datain1.readline ();

TA2.AppendText (String2 "/ N");

}

Catch (java.io ioexception oException0)

{

TA2.AppendText ("Error! / N");

Bu1.enable ();

Return;

}

}

(Program 3)

3. Instance operation

in

Http://person.zj.cninfo.net/~caveman has already put compiled SMTPTester.class and calling it SMTP.htm, Figure 1 is the applet at the time of running, then this letter has been Send to caveman@nese.net!

three. POP3 protocol reading E_mail's Java Applet

1. POP3 protocol and server

POP3 (Post Office Protocol Version 3) is a commonly used network protocol for collecting E_MAIL from the mailbox of the remote server, and its common commands are:

(1) Authorization State

User User Name

Pass User Password

Quit Exit

(2) Transaction State

STAT Mailbox status, that is, a few letters in the mailbox, a total of a total size (8 binding), etc.

List Displays the size of the per letter when the MSG parameter is displayed, and the length of the letter numbered MSG is displayed with the MSG parameter (8).

TOP Take the header (Head) and part of the letter (body), n = 0, N ≠ 0, N ≠ 0 The front N row of the letter. This command is an optional command, and some POP3 server software does not support it.

Retr reads a letter previously numbered MSG.

Dele Deleting a letter to an MSG, in fact, is only a tag, truly deleting an update status.

(3) Update State)

Quit Exited and deleted mail that DELE tagged.

There are also NOOP, LAST, RSET, RPOP and other commands.

Similar to the SMTP server, in addition to the POP3 server provided by ISP, some of the domestic storage of free personal homepage is also available (such as Netease Nease.Net). Users can also get a POP3 mailbox while getting free homepage, such as my mailbox for caveman@nease.net, put a copy of the applet you want to write and call it on the server, run it online, enter the correct user After the name and password, you can read E-Mail. 2. Main procedures in pop3tester.java

The Java applet has a total of three textfield components TF1, TF2, and TF3, which are used to enter user names, passwords, and numbers (0 when 0, only the letter of the mailbox status " Reading letter); also uses a textarea component TA2 to display the answer information of the POP3 server, letters content, and error information. Of course, a button (Button component bu1, the user enters the correct information) , Click on it with a mouse to read the specified letter.

The event capture handler is the same as the SMTPTester, read the E-mail program bu1Run see the program 4:

Public void bu1run ()

{

Try

{

Socket Socket1 = New Socket (GetCodeBase (). Gethost (), 110);

/ * Connect POP3 Server * /

DataOutputStream DataOut2 = New DataOutputStream (Socket1.getOutputStream ());

DataInputStream DataIN3 = New DataInputStream (Socket1.GetInputStream ());

GetReply (DataIn3);

Dataout2.writebytes ("User" Tf1.Gettext () "/ r / n");

GetReply (DataIn3);

DataOut2.writebytes ("pass" tf2.gettext () "/ r / n");

GetReply (DataIn3);

DataOut2.writebytes ("STAT / R / N");

/ * Mail box status * /

GetReply (DataIn3);

DataOut2.writebytes ("List / R / N");

/ * Letter length list * /

GetReply2 (Datain3);

TA2.AppendText ("/ n");

IF (tf3.gettext (). Equals ("0") == false)

{

DataOut2.writebytes ("retr" tf3.gettext () "/ r / n");

/ * Read Specify Letters * /

GetReply2 (Datain3);

TA2.AppendText ("/ n");

}

Dataout2.writebytes ("quit / r / n");

GetReply (DataIn3);

Socket1.close ();

Bu1.enable ();

Return;

}

Catch (java.io ioexception oException0)

{

TA2.AppendText ("Error! / N");

Bu1.enable ();

Return;

}

}

(Program 4)

Receive Server Answer GetReply and getReply2 See programs 5: Void getReply2 (DataInputStream DataIn1)

{

String string2;

String2 = "";

Try

{

FOR (String2 = DataIn1.readline ();

String2.equals (".") = false;

String2 = datain1.readline ())

/ * List and retri commands have multiple lines, but they all use a sentence "." As the end, according to this pickup information * /

{

TA2.AppendText (String2 "/ N");

}

}

Catch (java.io ioexception oException0)

{

TA2.AppendText ("Error! / N");

Bu1.enable ();

Return;

}

}

(Procedure 5)

Read the same procedures in the single sentence response, the same, because when dialogue with the POP3 server, there is a getReply2 program to handle this problem.

3. Instance operation

in

Http://www.netese.com/~caveman has already put compiled POP3TESTER.CLASS and calls it POP3.html, Figure 2 is the screen when the applet is running, just on Person.zj.cninfo.net The letter sent by SMTPTester has reached the mailbox of my Netease.

four. Program improvement

The above two Java Applets are also very imperfect, and there is still a lot of places that need to be improved on the support of the wrong capture. In addition, according to some of the basic methods mentioned above, simply change, other network protocols, such as FTP, News, etc.

转载请注明原文地址:https://www.9cbs.com/read-120552.html

New Post(0)