Use the Apache email component to send mail instances

Source: Internet
Author: User

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 ();}}

 

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.