1. Use APACHE-commons-email to send mail is very simple, just need to load Three jar packages: commons-email-1.1.jar, mail. jar, activition. jar. Commons email aims to provide a API for sending email. It is built on top of the Java mail API, which it aims to simplify. Some of the mail classes that are provided are as follows:
Simpleemail-This class is used to send basic text based emails.
Multipartemail-This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
Htmlemail-This class is used to send HTML formatted emails. It has all of the capabilities as multipartemail allowing attachments to be easily added. It also supports embedded images.
Emailattachment-This is a simple container class to allow for easy handling of attachments. it is for use with instances of multipartemail and htmlemail. 2. the following two tested codes: // send simple text
ImportOrg. Apache. commons. Mail. emailexception;
ImportOrg. Apache. commons. Mail. htmlemail;
Public
ClassMailsender {/***
@
Test successful !!!*/
Public
Static
VoidMain (string [] ARGs) {// do not use simpleemail. The error message "htmlemail email =" appears.
NewHtmlemail ();
Try{// Here is the sender server name email. sethostname ("smtp.sohu.com"); // set email. setcharset ("GBK"); // email address of the recipient. addto ("pengchua@gmail.com"); // sender's email. setfrom ("ppzhguy@sohu.com", "ppzhguy"); // If you need authentication information, set authentication: User Name-password. The registration name and password email of the sender on the email server. setauthentication ("ppzhguy", "XXXX"); email. setsubject ("test email"); // email of the message to be sent. setmsg ("test email"); // send an email. send ();}
Catch(Emailexception e ){//
TodoAuto-generated Catch Block E. printstacktrace () ;}}// send the attachment:
ImportJavax. Mail. Internet. mimeutility;
ImportOrg. Apache. commons. Mail. emailattachment;
ImportOrg. Apache. commons. Mail. multipartemail;
Public
ClassAttachmailsender {/***
@ ParamARGs *
@ ThrowsException */
Public
Static
VoidMain (string [] ARGs)
ThrowsException {//
TodoAuto-generated method stub // create the attachment emailattachment attachment =
NewEmailattachment (); attachment. setpath ("D:/test .jpg"); // specify the absolute path attachment. setdisposition (emailattachment.
Attachment); Attachment. setdescription ("picture of test"); // attachment description // attachment. setname ("test"); // The attachment name // If the attachment is a Chinese name, it will be garbled, attachment. setname (mimeutility. encodetext ("test"); attachment. setname (mimeutility.
Encodetext("Test"); // create the email message multipartemail email =
NewMultipartemail (); email. sethostname ("smtp.sohu.com"); // set email. setcharset ("GBK"); // email address of the recipient. addto ("pengchua@gmail.com"); // sender's email. setfrom ("ppzhguy@sohu.com", "ppzhguy"); // If you need authentication information, set authentication: User Name-password. The registration name and password email of the sender on the email server. setauthentication ("ppzhguy", "XXXXX"); email. setsubject ("image"); email. setmsg ("this is the image you want! "); // Add the attachment email. Attach (Attachment); // send the email. Send ();}}