You are on page 1of 12

Internet Programming with Java Course

1.8 : mail
e-mail S!"#"$"%
Mail Services
Users consider electronic mail the most important network service because they use it for interpersonal
communications. Some applications are newer and fancier. Other applications consume more network
bandwidth. Others are more important for the continued operation of the network. But email is the application
people use to communicate with each other. It isn't very fancy, but it's vital.
!"#I" provides a reliable, fle$ible email system built on a few basic protocols. hese are% Simple Mail
Transfer Protocol &SM"', Post Office Protocol &"O"', and Multipurpose Internet Mail Extensions &MIM('.
here are other !"#I" mail protocols. Interactive Mail Access Protocol, defined in )*! ++,-, is an interestin.
protocol desi.ned to supplant "O". It provides remote te$t searches and messa.e parsin. features not found in
"O". /e will touch only briefly on IM0". It and other protocols have some very interestin. features, but they
are not yet widely implemented.
Our covera.e concentrates on the three protocols you are most likely to use buildin. your network% SM", "O",
and MIM(. /e start with SM", the foundation of all !"#I" email systems.
Simple Mail ransfer "rotocol
SM" is the !"#I" mail delivery protocol. It moves mail across the Internet and across your local network.
SM" is defined in )*! 12+, A Simple Mail Transfer Protocol. It runs over the reliable, connection3oriented
service provided by Transmission Control Protocol &!"', and it uses well3known port number 24. 5,6 able
7.+ lists some of the simple, human3readable commands used by SM".
5,6 Most standard !"#I" applications are assi.ned a well3known port in the Assigned Numbers RC, so that
remote systems know how to connect the service.
Table 3.1: SMTP Commands
Command Syntax Function
Hello HELO <sendin!"ost# $denti%y sendin SMTP
F&om M'$L F(OM:<%&om!add&ess# Sende& add&ess
(eci)ient (CPT TO:<to!add&ess# (eci)ient add&ess
*ata *'T' +ein a messae
(eset (SET 'bo&t a messae
,e&i%y ,(F- <st&in# ,e&i%y a use&name
Ex)and E.P/ <st&in# Ex)and a mailin list
Hel) HELP 0st&in1 (e2uest online "el)
3uit 34$T End t"e SMTP session
SM" is such a simple protocol you can literally do it yourself. &el'e& to port 24 on a remote host and type mail
in from the command line usin. the SM" commands. his techni8ue is sometimes used to test a remote
system's SM" server, but we use it here to illustrate how mail is delivered between systems. he e$ample
below shows mail manually input from 9aniel on peanut!nuts!com to yler on almond!nuts!com.
5 telnet almond.nuts.com 25
T&yin 167.18.17.1 ...
Connected to almond.nuts.com.
Esca)e c"a&acte& is 9:19.
77; almond Sendmail <.1=1.<1 &eady at Tue> 7? Ma& ?< 16:71:78 EST
helo peanut.nuts.com
7@; almond Hello )eanut.nuts.com> )leased to meet you
mail from:<daniel@peanut.nuts.com>
7@; <danielA)eanut.nuts.com#... Sende& oB
rcpt to:<tler@almond.nuts.com>
7@; <tyle&Aalmond.nuts.com#... (eci)ient oB
data
3@< Ente& mail> end Cit" D.D on a line by itsel%
!i "ler#
.
7@; Mail acce)ted
$uit
771 almond deliEe&in mail
Connection closed by %o&ein "ost.
he user input is shown in bold type. 0ll of the other lines are output from the system. his e$ample shows
how simple it is. 0 !" connection is opened. he sendin. system identifies itself. he rom address and the
To address are provided. he messa.e transmission be.ins with the ()!) command and ends with a line that
contains only a period &.'. he session terminates with a :UI command. ;ery simple, and very few commands
are used.
here are other commands &S*+(, S$,, S),, and !-.+' defined in )*! 12+ that are optional and not
widely implemented. (ven some of the commands that are implemented are not commonly used. he
commands /*,", 0.12, and *3"+ are desi.ned more for interactive use than for the normal machine3to3
machine interaction used by SM". he followin. e$cerpt from a SM" session shows how these odd
commands work.
!%&P
71<!Commands:
71<! HELO M'$L (CPT *'T' (SET
71<! /OOP 34$T HELP ,(F- E.P/
71<!Fo& mo&e in%o use DHELP <to)ic#D.
71<!Fo& local in%o&mation contact )ostmaste& at t"is site.
71< End o% HELP in%o
!%&P '(%"
71<!(SET
71<! (esets t"e system.
71< End o% HELP in%o
)'*+ <,ane>
7@; <FaneAb&aGil.nuts.com#
)'*+ <mac>
7@; Hat"y McCa%%e&ty <<mac##
%-P. <admin>
7@;!<sa&aA)ecan.nuts.com#
7@; *aEid C&ai <<daEid##
7@;!<tyle&Anuts.com#
he /*," command prints out a summary of the commands implemented on the system. he /*," .S*!
command specifically re8uests information about the .S*! command. *rankly, this help system isn't very
helpful<
he 0.12 and *3"+ commands are more useful, but are often disabled for security reasons because they
provide user account information that mi.ht be e$ploited by network intruders. he *3"+ 4a5mi'6 command
asks for a listin. of the email addresses in the mailin. list admin, and that is what the system provides. he
0.12 command asks for information about an individual instead of a mailin. list. In the case of the 0.12
4ma76 command, mac is a local user account and the user's account information is returned. In the case of
0.12 48a'e6, "ane is an alias in the #etc#aliases file. he value returned is the email address for "ane found in
that file. he three commands in this e$ample are interestin., but rarely used. SM" depends on the other
commands to .et the real work done.
SM" provides direct end3to3end mail delivery. his is unusual. Most mail systems use store and for$ard
protocols like UU!" and =.>?? that move mail toward its destination one hop at a time, storin. the complete
messa.e at each hop and then forwardin. it on to the ne$t system. he messa.e proceeds in this manner until
final delivery is made. *i.ure 7.7 illustrates both store and forward and direct delivery mail systems. he
UU!" address clearly shows the path that the mail takes to its destination, while the SM" mail address
implies direct delivery. he address doesn't have anythin. to do with whether or not a system is store and
forward or direct delivery. It @ust happens that UU!" provides an address that helps to illustrate this point.
1i9:;e %.%: ail 5eli<e;= >=>&em>
9irect delivery allows SM" to deliver mail without relyin. on intermediate hosts. If the delivery fails, the
local system knows it ri.ht away. It can inform the user that sent the mail or 8ueue the mail for later delivery
without reliance on remote systems. he disadvanta.e of direct delivery is that it re8uires both systems to be
fully capable of handlin. mail. Some systems cannot handle mail, particularly small systems such as "!s or
mobile systems such as laptops. hese systems are usually shut down at the end of the day and are fre8uently
offline. Mail directed from a remote host fails with a Acannot connectA error when the local system is turned off
or offline. o handle these cases, features in the 9BS system are used to route the messa.e to a mail server in
lieu of direct delivery. he mail is then moved from the server to the client system when the client is back
online. he protocol most !"#I" networks use for this task is "O".
($ample C sendin. e3mail usin. SM" protocol
=II
I SMTPSession ! Class %o& sendin e!mails usin SMTP )&otocol.
I=
im)o&t FaEa.io.IJ
im)o&t FaEa.net.IJ
im)o&t FaEa.util.IJ
)ublic class SMTPSession
K
=II 1@ sec. socBet &ead timeout I=
)ublic static %inal int SOCHETL(E'*LT$MEO4T M 1@I1;;;J
)&iEate St&in "ostJ
)&iEate int )o&tJ
)&iEate St&in &eci)ientJ
)&iEate St&in sende&J
)&iEate St&in subFectJ
)&iEate St&in messaeJ
)&otected SocBet smt)SocBetJ
)&otected +u%%e&ed(eade& inJ
)&otected Out)utSt&eamN&ite& outJ
=II
I C&eates neC SMTP session by iEen SMTP "ost and )o&t> &eci)ient9s email
I add&ess> sende&9s email add&ess> email subFect and email messae text.
I=
)ublic SMTPSessionOSt&in "ost> int )o&t> St&in &eci)ient>
St&in sende&> St&in subFect> St&in messaeP
K
t"is."ost M "ostJ
t"is.)o&t M )o&tJ
t"is.&eci)ient M &eci)ientJ
t"is.messae M messaeJ
t"is.sende& M sende&J
t"is.subFect M subFectJ
Q
=II
I C&eates neC SMTP session by iEen SMTP "ost> &eci)ient9s email add&ess>
I sende&9s email add&ess> email subFect and email messae text. 'ssumes
I SMTP )o&t is 7@ Ode%ault %o& SMTP se&EiceP.
I=
)ublic SMTPSessionOSt&in "ost> St&in &eci)ient>
St&in sende&> St&in subFect> St&in messaeP
K
t"isO"ost> 7@> &eci)ient> sende&> subFect> messaePJ
Q
=II
I Closes doCn t"e connection to SMTP se&Ee& Oi% o)enP.
I S"ould be called i% an exce)tion is &aised du&in t"e SMTP session.
I=
)ublic Eoid closeOP
K
t&y K
in.closeOPJ
out.closeOPJ
smt)SocBet.closeOPJ
Q catc" OExce)tion exP K
== $no&e t"e exce)tion. P&obably t"e socBet is not o)en.
Q
Q
=II
I Connects to t"e SMTP se&Ee& and ets in)ut and out)ut st&eams Oin> outP.
I=
)&otected Eoid connectOP
t"&oCs $OExce)tion
K
smt)SocBet M neC SocBetO"ost> )o&tPJ
smt)SocBet.setSoTimeoutOSOCHETL(E'*LT$MEO4TPJ
in M neC +u%%e&ed(eade&OneC $n)utSt&eam(eade&Osmt)SocBet.et$n)utSt&eamOPPPJ
out M neC Out)utSt&eamN&ite&Osmt)SocBet.etOut)utSt&eamOPPJ
Q
=II
I Sends iEen command and Caits %o& a &es)onse %&om se&Ee&.
I A&etu&n &es)onse &eceiEed %&om t"e se&Ee&.
I=
)&otected St&in sendCommandOSt&in commandSt&inP
t"&oCs $OExce)tion
K
out.C&iteOcommandSt&in R DSnDPJ
out.%lus"OPJ
St&in &es)onse M et(es)onseOPJ
&etu&n &es)onseJ
Q
=II
I Sends iEen commandSt&in to t"e se&Ee&> ets its &e)ly and c"ecBs i%
I it sta&ts Cit" ex)ected(es)onseSta&t. $% not> t"&oCs $OExce)tion Cit"
I se&Ee&9s &e)ly OC"ic" is unex)ectedP.
I=
)&otected Eoid doCommandOSt&in commandSt&in> c"a& ex)ected(es)onseSta&tP
t"&oCs $OExce)tion
K
St&in &es)onse M sendCommandOcommandSt&inPJ
c"ecBSe&Ee&(es)onseO&es)onse> ex)ected(es)onseSta&tPJ
Q
=II
I C"ecBs i% iEen se&Ee& &e)ly sta&ts Cit" ex)ected(es)onseSta&t.
I $% not> t"&oCs $OExce)tion Cit" t"is &e)ly Obecause it is unex)ectedP.
I=
)&otected Eoid c"ecBSe&Ee&(es)onseOSt&in &es)onse> c"a& ex)ected(es)onseSta&tP
t"&oCs $OExce)tion
K
i% O&es)onse.c"a&'tO;P TM ex)ected(es)onseSta&tP
t"&oC neC $OExce)tionO&es)onsePJ
Q
=II
I Uets a &es)onse bacB %&om t"e se&Ee&. Handles multi!line &es)onses
I Oacco&din to SMTP )&otocolP and &etu&ns t"em as multi!line st&in.
I Eac" line o% t"e se&Ee&9s &e)ly consists o% 3!diit numbe& %olloCed
I by some text. $% t"e&e is a 9!9 immediately a%te& t"e numbe&> t"e SMTP
I &es)onse continues on t"e next line. Ot"e&Cise it %inis"ed at t"is line.
I=
)&otected St&in et(es)onseOP
t"&oCs $OExce)tion
K
St&in &es)onse M DDJ
St&in lineJ
do K
line M in.&eadLineOPJ
i% OOline MM nullP VV Oline.lent"OP < 3PP K
== SMTP &es)onse lines s"ould at t"e Ee&y least "aEe a 3!diit numbe&
t"&oC neC $OExce)tionOD+ad &es)onse %&om se&Ee&.DPJ
Q
&es)onse RM line R DSnDJ
Q C"ile OOline.lent"OP # 3P WW Oline.c"a&'tO3P MM 9!9PPJ
&etu&n &es)onseJ
Q
=II
I P&e)a&es and &etu&ns e!mail messae "eade&s.
I=
)&otected St&in etMessaeHeade&sOP
K
== Most "eade& a&e less t"an 1;7< c"a&acte&s lon
St&in "eade&s M DDJ
"eade&s M "eade&s R D*ate: D R neC *ateOP.toSt&inOP R DSnDJ
"eade&s M "eade&s R DSende&: D R sende& R DSnDJ
"eade&s M "eade&s R DF&om: D R sende& R DSnDJ
"eade&s M "eade&s R DTo: D R &eci)ient R DSnDJ
"eade&s M "eade&s R DSubFect: D R subFect R DSnDJ
&etu&n "eade&s R DSnSnDJ
Q
=II
I Sends a messae usin t"e SMTP )&otocol.
I=
)ublic Eoid sendMessaeOP
t"&oCs $OExce)tion
K
connectOPJ
== '%te& connectin> t"e SMTP se&Ee& Cill send a &es)onse st&in.
== MaBe su&e it sta&ts Cit" a 979 O&e)onses in t"e 7;;9s a&e )ositiEeP.
St&in &es)onse M et(es)onseOPJ
c"ecBSe&Ee&(es)onseO&es)onse>979PJ
== $nt&oduce ou&selEes to t"e SMTP se&Ee& Cit" a )olite DHELO local"ostnameD
doCommandODHELO D R smt)SocBet.etLocal'dd&essOP.toSt&inOP> 979PJ
== Tell t"e se&Ee& C"o t"is messae is %&om
doCommandODM'$L F(OM: <D R sende& R D#D> 979PJ
== /oC tell t"e se&Ee& C"o Ce Cant to send a messae to
doCommandOD(CPT TO: <D R &eci)ient R D#D> 979PJ
== OBay> noC send t"e mail messae. Ne ex)ect a &es)onse beinnin
== Cit" 939 indicatin t"at t"e se&Ee& is &eady %o& data.
doCommandOD*'T'D> 939PJ
== Send t"e messae "eade&s
out.C&iteOetMessaeHeade&sOPPJ
+u%%e&ed(eade& ms+ody(eade& M neC +u%%e&ed(eade&OneC St&in(eade&OmessaePPJ
== Send eac" line o% t"e messae
St&in lineJ
C"ile OOlineMms+ody(eade&.&eadLineOPP TM nullP K
== $% t"e line beins Cit" a D.D> )ut an ext&a D.D in %&ont o% it.
i% Oline.sta&tsNit"OD.DPP
out.C&iteO9.9PJ
out.C&iteOline R DSnDPJ
Q
== ' D.D on a line by itsel% ends a messae.
doCommandOD.D> 979PJ
== Messae is sent. Close t"e connection to t"e se&Ee&
closeOPJ
Q
Q
=II
I SMTPSession exam)le. Sends emails usin SMTP )&otocol.
I OcP 7;;7 by SEetlin /aBoE
I=
im)o&t SMTPSessionJ
)ublic class SMTPClient
K
)ublic static Eoid mainOSt&in01 a&sP
K
SMTPSession smt) M neC SMTPSessionO
Dlocal"ostD>
D&eci)ientAmycom)any.comD>
Dsende&Amycom)any.comD>
DSome subFectD>
D... Messae text ...DPJ
t&y K
System.out.)&intlnODSendin e!mail...DPJ
smt).sendMessaeOPJ
System.out.)&intlnODE!mail sent.DPJ
Q catc" OExce)tion eP K
smt).closeOPJ
System.out.)&intlnODCan not send e!mailTDPJ
e.)&intStacBT&aceOPJ
Q
Q
Q
"ost Office "rotocol &"O"'
here are two versions of "O" in widespread use% "O"2 and "O"7. "O"2 is defined in )*! D7, and "O"7 is
defined in )*! +,24. "O"2 uses port +?D and "O"7 uses port ++?. hese are incompatible protocols that use
different commands, but they perform the same basic functions. he "O" protocols verify the user's lo.in name
and password, and move the user's mail from the server to the user's local mail reader.
0 sample "O"2 session clearly illustrates how a "O" protocol works. "O"2 is a simple re8uest#response
protocol, and @ust as with SM", you can type "O"2 commands directly into its well3known port &+?D' and
observe their effect. Eere's an e$ample with the user input shown in bold type%
5 telnet almond.nuts.com /01
T&yin 167.18.17.1 ...
Connected to almond.nuts.com.
Esca)e c"a&acte& is 9:19.
R POP7 almond POP7 Se&Ee& at Ned 3;!Ma&!?< 3:<XPM!EST
!%&2 hunt 3ats3att
Y3 ...OF&om %olde& 9/ENM'$L9P
'%45
M<?8
'%"'
{The full text of message 1}
4C65
M?7?
'%"'
{The full text of message 2}
4C65
M87<
'%"'
{The full text of message 3}
4C65
M;
78I"
ROH POP7 Se&Ee& exitin O; /ENM'$L messaes le%tP
Connection closed by %o&ein "ost.
he /*,$ command provides the username and password for the account of the mailbo$ that is bein.
retrieved. &his is the same username and password used to lo. into the mail server.' In response to the /*,$
command the server sends a count of the number of messa.es in the mailbo$, three &F7' in our e$ample. he
.*)( command be.ins readin. the mail. .*!. retrieves the full te$t of the current messa.e. )?@(
acknowled.es receipt of the messa.e and deletes it from the server. 0fter each acknowled.ment the server
sends a count of the number of bytes in the new messa.e. If the byte count is Gero &H?' it indicates that there are
no more messa.es to be retrieved and the client ends the session with the A-B! command. Simple< able 7.2
lists the full set of "O"2 commands.
Table 3.7: POP7 Commands
Command Syntax Function
Hello HELO use& )assCo&d $denti%y use& account
Folde& FOL* mail!%olde& Select mail %olde&
(ead (E'* 0n1 (ead mail> o)tionally sta&t Cit" messae n
(et&ieEe (ET( (et&ieEe messae
SaEe 'CHS 'cBnoClede and saEe
*elete 'CH* 'cBnoClede and delete
Failed /'CH /eatiEe acBnoCledement
3uit 34$T End t"e POP7 session
he commands for "O"7 are completely different from the commands used for "O"2. able 7.7 shows the set
of "O"7 commands defined in )*! +,24.
Table 3.3: POP3 Commands
Command Function
4SE( use&name T"e use&9s account name
P'SS )assCo&d T"e use&9s )assCo&d
ST'T *is)lay t"e numbe& o% un&ead messaes=bytes
(ET( n (et&ieEe messae numbe& n
*ELE n *elete messae numbe& n
L'ST *is)lay t"e numbe& o% t"e last messae accessed
L$ST 0n1 *is)lay t"e siGe o% messae n o& o% all messaes
(SET 4ndelete all messaesJ &eset messae numbe& to 1
TOP n l P&int t"e "eade&s and l lines o% messae n
/OOP *o not"in
34$T End t"e POP3 session
9espite the fact that these commands are different from those used by "O"2, they can be used to perform
similar functions. In the "O"2 e$ample we lo..ed into the server and read and deleted three mail messa.es.
Eere's a similar session usin. "O"7%
5 telnet almond //0
T&yin 167.18.17.1 ...
Connected to almond.nuts.com.
Esca)e c"a&acte& is 9:19.
ROH almond POP3 Se&Ee& P&ocess 3.3O1P at Mon 1@!May!?@ <:<XPM!E*T
user hunt
ROH 4se& name O"untP oB. PassCo&d> )lease.
pass 3atts93att9
ROH 3 messaes in %olde& /ENM'$L O,3.3 (eE +;<P
stat
ROH 3 <@?
retr /
ROH 1<8 octets
The full text of message 1
dele /
ROH messae Y 1 deleted
retr 2
ROH 1@@ octets
The full text of message 2
dele 2
ROH messae Y 7 deleted
retr :
ROH 1@X octets
The full text of message 3
dele :
ROH messae Y 3 deleted
$uit
ROH POP3 almond Se&Ee& exitin O; /ENM'$L messaes le%tP
Connection closed by %o&ein "ost.
Baturally you don't really type these commands in yourself, but e$periencin. hands3on interaction with SM"
and "O" .ives you a clearer understandin. of what these pro.rams do and why they are needed.
($ample C receivin. e3mail usin. "O"7 protocol
=II
I POP3Session ! Class %o& c"ecBin e!mail Eia POP3 )&otocol.
I=
im)o&t FaEa.io.IJ
im)o&t FaEa.net.IJ
im)o&t FaEa.util.IJ
)ublic class POP3Session extends ObFect
K
=II 1@ sec. socBet &ead timeout I=
)ublic static %inal int SOCHETL(E'*LT$MEO4T M 1@I1;;;J
)&otected SocBet )o)3SocBetJ
)&otected +u%%e&ed(eade& inJ
)&otected P&intN&ite& outJ
)&iEate St&in "ostJ
)&iEate int )o&tJ
)&iEate St&in use&/ameJ
)&iEate St&in )assCo&dJ
=II
I C&eates neC POP3 session by iEen POP3 "ost> use&name and )assCo&d.
I 'ssumes POP3 )o&t is 11; Ode%ault %o& POP3 se&EiceP.
I=
)ublic POP3SessionOSt&in "ost> St&in use&/ame> St&in )assCo&dP
K
t"isO"ost> 11;> use&/ame> )assCo&dPJ
Q
=II
I C&eates neC POP3 session by iEen POP3 "ost and )o&t> use&name and )assCo&d.
I=
)ublic POP3SessionOSt&in "ost> int )o&t> St&in use&/ame> St&in )assCo&dP
K
t"is."ost M "ostJ
t"is.)o&t M )o&tJ
t"is.use&/ame M use&/ameJ
t"is.)assCo&d M )assCo&dJ
Q
=II
I T"&oCs exce)tion i% iEen se&Ee& &es)onse i% neatiEe. 'cco&din to POP3
I )&otocol> )ositiEe &es)onses sta&t Cit" a 9R9 and neatiEe sta&t Cit" 9!9.
I=
)&otected Eoid c"ecBFo&E&&o&OSt&in &es)onseP
t"&oCs $OExce)tion
K
i% O&es)onse.c"a&'tO;P TM 9R9P
t"&oC neC $OExce)tionO&es)onsePJ
Q
=II
I A&etu&n t"e cu&&ent numbe& o% messaes usin t"e POP3 ST'T command.
I=
)ublic int etMessaeCountOP
t"&oCs $OExce)tion
K
== Send ST'T command
St&in &es)onse M doCommandODST'TDPJ
== T"e %o&mat o% t"e &es)onse is ROH msLcount siGeLinLbytes
== Ne taBe t"e subst&in %&om o%%set < Ot"e sta&t o% t"e msLcountP and
== o u) to t"e %i&st s)ace> t"en conEe&t t"at st&in to a numbe&.
t&y K
St&in countSt& M &es)onse.subst&inO<> &es)onse.indexO%O9 9> <PPJ
int count M OneC $ntee&OcountSt&PP.int,alueOPJ
&etu&n countJ
Q catc" OExce)tion eP K
t"&oC neC $OExce)tionOD$nEalid &es)onse ! D R &es)onsePJ
Q
Q
=II
I Uet "eade&s &etu&ns a list o% messae numbe&s alon Cit" some siGin
I in%o&mation> and )ossibly ot"e& in%o&mation de)endin on t"e se&Ee&.
I=
)ublic St&in01 etHeade&sOP
t"&oCs $OExce)tion
K
doCommandODL$STDPJ
&etu&n etMultiline(es)onseOPJ
Q
=II
I Uets "eade& &etu&ns t"e messae numbe& and messae siGe %o& a )a&ticula&
I messae numbe&. $t may also contain ot"e& in%o&mation.
I=
)ublic St&in etHeade&OSt&in messae$dP
t"&oCs $OExce)tion
K
St&in &es)onse M doCommandODL$ST D R messae$dPJ
&etu&n &es)onseJ
Q
=II
I (et&ieEes t"e enti&e text o% a messae usin t"e POP3 (ET( command.
I=
)ublic St&in etMessaeOSt&in messae$dP
t"&oCs $OExce)tion
K
doCommandOD(ET( D R messae$dPJ
St&in01 messaeLines M etMultiline(es)onseOPJ
St&in+u%%e& messae M neC St&in+u%%e&OPJ
%o& Oint iM;J i<messaeLines.lent"J iRRP K
messae.a))endOmessaeLines0i1PJ
messae.a))endODSnDPJ
Q
&etu&n neC St&inOmessaePJ
Q
=II
I (et&ieEes t"e %i&st <linecount# lines o% a messae usin t"e POP3 TOP
I command. /ote: t"is command may not be aEailable on all se&Ee&s. $%
I it isn9t aEailable> you9ll et an exce)tion.
I=
)ublic St&in01 etMessaeHeadOSt&in messae$d> int lineCountP
t"&oCs $OExce)tion
K
doCommandODTOP D R messae$d R D D R lineCountPJ
&etu&n etMultiline(es)onseOPJ
Q
=II
I *eletes a )a&ticula& messae Cit" *ELE command.
I=
)ublic Eoid deleteMessaeOSt&in messae$dP
t"&oCs $OExce)tion
K
doCommandOD*ELE D R messae$dPJ
Q
=II
I $nitiates a &ace%ul exit by sendin 34$T command.
I=
)ublic Eoid 2uitOP
t"&oCs $OExce)tion
K
doCommandOD34$TDPJ
Q
=II
I Connects to t"e POP3 se&Ee& and los on it
I Cit" t"e 4SE( and P'SS commands.
I=
)ublic Eoid connect'nd'ut"enticateOP
t"&oCs $OExce)tion
K
== MaBe t"e connection
)o)3SocBet M neC SocBetO"ost> )o&tPJ
)o)3SocBet.setSoTimeoutOSOCHETL(E'*LT$MEO4TPJ
in M neC +u%%e&ed(eade&OneC $n)utSt&eam(eade&O)o)3SocBet.et$n)utSt&eamOPPPJ
out M neC P&intN&ite&OneC Out)utSt&eamN&ite&O)o)3SocBet.etOut)utSt&eamOPPPJ
== (eceiEe t"e Celcome messae
St&in &es)onse M in.&eadLineOPJ
c"ecBFo&E&&o&O&es)onsePJ
== Send a 4SE( command to aut"enticate
doCommandOD4SE( D R use&/amePJ
== Send a P'SS command to %inis" aut"entication
doCommandODP'SS D R )assCo&dPJ
Q
=II
I Closes doCn t"e connection to POP3 se&Ee& Oi% o)enP.
I S"ould be called i% an exce)tion is &aised du&in t"e POP3 session.
I=
)ublic Eoid closeOP
K
t&y K
in.closeOPJ
out.closeOPJ
)o)3SocBet.closeOPJ
Q catc" OExce)tion exP K
== $no&e t"e exce)tion. P&obably t"e socBet is not o)en.
Q
Q
=II
I Sends a POP3 command and &et&ieEes t"e &es)onse. $% t"e &es)onse is
I neatiEe Obeins Cit" 9!9P> t"&oCs an $OExce)tion Cit" &eceiEed &es)onse.
I=
)&otected St&in doCommandOSt&in commandP
t"&oCs $OExce)tion
K
out.)&intlnOcommandPJ
out.%lus"OPJ
St&in &es)onse M in.&eadLineOPJ
c"ecBFo&E&&o&O&es)onsePJ
&etu&n &es)onseJ
Q
=II
I (et&ieEes a multi!line POP3 &es)onse. $% a line contains D.D by itsel%>
I it is t"e end o% t"e &es)onse. $% a line sta&ts Cit" a D.D> it s"ould
I &eally "aEe tCo D.D9s. Ne st&i) o%% t"e leadin D.D. $% a line does not
I sta&t Cit" D.D> t"e&e s"ould be at least one line mo&e.
I=
)&otected St&in01 etMultiline(es)onseOP
t"&oCs $OExce)tion
K
'&&ayList lines M neC '&&ayListOPJ
C"ile Ot&ueP K
St&in line M in.&eadLineOPJ
i% Oline MM nullP K
== Se&Ee& closed connection
t"&oC neC $OExce)tionODSe&Ee& unaCa&es closed t"e connection.DPJ
Q
i% Oline.e2ualsOD.DPP K
== /o mo&e lines in t"e se&Ee& &es)onse
b&eaBJ
Q
i% OOline.lent"OP # ;P WW Oline.c"a&'tO;P MM 9.9PP K
== T"e line sta&ts Cit" a D.D ! st&i) it o%%.
line M line.subst&inO1PJ
Q
== 'dd &ead line to t"e list o% lines
lines.addOlinePJ
Q
St&in &es)onse01 M neC St&in0lines.siGeOP1J
lines.to'&&ayO&es)onsePJ
&etu&n &es)onseJ
Q
Q
=II
I POP3Session exam)le. (eceiEes email usin POP3 )&otocol.
I=
im)o&t POP3SessionJ
im)o&t FaEa.util.St&inToBeniGe&J
)ublic class POP3Client
K
)ublic static Eoid mainOSt&in01 a&sP
K
POP3Session )o)3 M neC POP3SessionOD)o).mycom)any.comD> Duse&nameD> D)assCo&dDPJ
t&y K
System.out.)&intlnODConnectin to POP3 se&Ee&...DPJ
)o)3.connect'nd'ut"enticateOPJ
System.out.)&intlnODConnected to POP3 se&Ee&.DPJ
int messaeCount M )o)3.etMessaeCountOPJ
System.out.)&intlnODSnNaitin massaes on POP3 se&Ee& : D R messaeCountPJ
St&in01 messaes M )o)3.etHeade&sOPJ
%o& Oint iM;J i<messaes.lent"J iRRP K
St&inToBeniGe& messaeToBens M neC St&inToBeniGe&Omessaes0i1PJ
St&in messae$d M messaeToBens.nextToBenOPJ
St&in messaeSiGe M messaeToBens.nextToBenOPJ
St&in messae+ody M )o)3.etMessaeOmessae$dPJ
System.out.)&intlnO
DSn!!!!!!!!!!!!!!!!!!!! messsae D R messae$d R
D> siGeMD R messaeSiGe R D !!!!!!!!!!!!!!!!!!!!DPJ
System.out.)&intOmessae+odyPJ
System.out.)&intlnOD!!!!!!!!!!!!!!!!!!!! end o% messae D R
messae$d R D !!!!!!!!!!!!!!!!!!!!DPJ
Q
Q catc" OExce)tion eP K
)o)3.closeOPJ
System.out.)&intlnODCan not &eceiEe e!mailTDPJ
e.)&intStacBT&aceOPJ
Q
Q
Q

You might also like