Package CN. grgbanking. view. server. util; import Java. util. properties; import javax. activation. datahandler; import javax. activation. filedatasource; import javax. mail. address; import javax. mail. bodypart; import javax. mail. message; import javax. mail. multipart; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimebodypart; import javax. Mail. internet. mimemessage; import javax. mail. internet. mimemultipart; import CN. grgbanking. view. server. service. isendmail; import CN. grgbanking. view. server. util. osyslog; // implements the mail sending function. The system must provide the SMTP server address public class osendmail implements isendmail {private mimemessage mimemsg; // The mime mail object private session; // mail session object private properties props; // system property private string username = ""; // SMTP authentication username and password PR Ivate string Password = ""; private string defaultfrom; private multipart MP; // multipart object, mail content, title, attachment, and other content are added to it, and then the mimemessage object public osendmail () is generated () {setsmtphost (""); // if no email server is specified, obtain createsession ();} public osendmail (string SMTP) {setsmtphost (SMTP) from the getconfig class ); createsession ();} public void setdefaultfrom (string from) {defaultfrom = from;}/*** @ Param hostname * string */Public void set Smtphost (string hostname) {// system. out. println ("set system properties: mail. SMTP. host = "+ hostname); If (props = NULL) props = system. getproperties (); // obtain the system property object props. put ("mail. SMTP. host ", hostname); // set SMTP host}/*** @ return Boolean */Public Boolean createsession () {try {// system. out. println ("prepare to get the mail session object! "); Session = session. getdefaultinstance (props, null); // get the mail session object} catch (exception e) {system. err. println ("An error occurred while obtaining the email Session object! "); Osyslog. Error (E); Return false;} return true;} public Boolean createmimemessage () {// system. Out. println (" prepare to create a mime mail object! "); Try {mimemsg = new mimemessage (session); // create a mime mail object MP = new mimemultipart (); Return true;} catch (exception e) {system. err. println ("An error occurred while creating the mime mail object! "); Osyslog. error (E); Return false ;}}/*** @ Param need * Boolean */Public void setneedauth (Boolean need) {// system. out. println ("set SMTP authentication: mail. SMTP. auth = "+ need); If (props = NULL) props = system. getproperties (); If (need) {props. put ("mail. SMTP. auth "," true ");} else {props. put ("mail. SMTP. auth "," false ") ;}}/*** @ Param name * string * @ Param Pass * string */Public void setnamepass (St Ring name, string pass) {username = Name; Password = pass;}/*** @ Param mailsubject * string * @ return Boolean */Public Boolean setsubject (string mailsubject) {// system. out. println ("set email subject! "); Try {mimemsg. setsubject (mailsubject); Return true;} catch (exception e) {system. Err. println (" an error occurred when setting the mail subject! "); Osyslog. error (E); Return false ;}}/*** @ Param mailbody * string */Public Boolean setbody (string mailbody) {try {bodypart BP = new mimebodypart (); BP. setcontent ("<meta http-equiv = Content-Type content = text/html; charset = gb2312>" + mailbody, "text/html; charset = gb2312"); MP. addbodypart (BP); Return true;} catch (exception e) {system. err. println ("an error occurred when setting the mail body! "); Osyslog. error (E); Return false ;}}/*** @ Param name * string * @ Param Pass * string */Public Boolean addfileaffix (string filename) {try {bodypart BP = new mimebodypart (); filedatasource fileds = new filedatasource (filename); BP. setdatahandler (New datahandler (fileds); BP. setfilename (fileds. getname (); MP. addbodypart (BP); Return true;} catch (exception e) {system. err. println ("Add email attachment:" + F Ilename + "error! "); Osyslog. error (E); Return false ;}}/*** @ Param name * string * @ Param Pass * string */Public Boolean setfrom (string from) {If (from = NULL) {return false;} Try {mimemsg. setfrom (New internetaddress (from); // sets the sender's return true;} catch (exception e) {osyslog. error (E); Return false ;}}/*** @ Param name * string * @ Param Pass * string */Public Boolean setto (string) {If (to = NULL) Return false; try {mimemsg. setrecipients (message. recipienttype. to, internetaddress. parse (to); Return true;} catch (exception e) {osyslog. error (E); Return false ;}}/*** @ Param name * string * @ Param Pass * string */Public Boolean setcopyto (string copyto) {If (copyto = NULL) return false; try {mimemsg. setrecipients (message. recipienttype. CC, (Address []) internetaddress. parse (copyto); Re Turn true;} catch (exception e) {osyslog. error (E); Return false ;}}/*** @ Param name * string * @ Param Pass * string */Public Boolean sendout () {try {mimemsg. setcontent (MP); mimemsg. savechanges (); // system. out. println ("sending email .... "); Session mailsession = session. getinstance (props, null); transport Transport = mailsession. gettransport ("SMTP"); transport. connect (string) props. get ("mail. SMTP. Host "), username, password); transport. sendmessage (mimemsg, mimemsg. getrecipients (message. recipienttype. to); // transport. send (mimemsg); // system. out. println ("email sent successfully! "); Transport. Close (); Return true;} catch (exception e) {system. Err. println (" email sending failed! "); Osyslog. error (E); Return false ;}}/*** just do it as this */public static void main (string [] ARGs) {// verify that the email can be sent successfully. ljming 2007/08/29 string mailbody = "<meta http-equiv = Content-Type content = text/html; charset = gb2312> "+" <Div align = center> <a href = http://www.csdn.net> csdn </a> </div> "; osendmail sendmailer = new osendmail (); // sendmailer. send ("211.154.109.68", true, "ljming", "XXXXX", "subject", m Ailbody, null, "ljming@grgbanking.com", "ljming@grgbanking.com"); sendmailer. setsmtphost ("smtp.163.com"); sendmailer. setdefaultfrom ("fiafa5000@163.com"); sendmailer. setneedauth (true); sendmailer. setnamepass ("Account", "password"); sendmailer. send ("ljming to Ming", mailbody, "superv3@yeah.net");} public Boolean send (string smtpip, Boolean isneedauth, string user, string pass, string subject, string mailbody, strin G [] fillaffix, string mailto, string mailfrom) {setsmtphost (smtpip); setneedauth (isneedauth); If (isneedauth) setnamepass (user, pass); return send (subject, mailbody, fillaffix, mailto, mailfrom);} public Boolean send (string subject, string mailbody, string [] fillaffix, string mailto, string mailfrom) {If (! Createmimemessage () return false; If (! Setfrom (mailfrom) return false; If (! Setsubject (subject) return false; If (! Setbody (mailbody) return false; If (! Setto (mailto) return false; If (fillaffix! = NULL & fillaffix. length> 0) {for (INT I = 0; I <fillaffix. length; I ++) {If (addfileaffix (fillaffix [I]) return false ;}} if (! Sendout () return false; return true;} public Boolean send (string subject, string mailbody, string mailto) {return send (subject, mailbody, null, mailto, defaultfrom ); /* system. out. println ("mailsubject:" + subject); If (! Createmimemessage () return false; If (! Setfrom (defaultfrom) return false; If (! Setsubject (subject) return false; If (! Setbody (mailbody) return false; If (! Setto (mailto) return false; If (! Sendout () return false; return true ;*/}}