Apache commons-email Send mail garbled problem __ garbled problem

Source: Internet
Author: User
Tags format message

Earlier we used Mail.jar and Activation.jar two packages to write the code to send emails, although not difficult, but troublesome. Now the Apache site has a jarkata/commons/email subproject, but also for us to achieve the function of sending email, in http://jakarta.apache.org/commons/email/bag Commons-email-1.0.jar down, the code you want to write is very small, and very clear. The size of this bag is only 23K, which is 9 classes, but it can save you a lot of things.

Commons-email is an Open-source API provided by Apache that encapsulates JavaMail, so add Javamail.jar to class path when you use it, mainly including Simpleemail,multipartemail, Htmlemail,emailattachment four classes.

Simpleemail: Send a simple email and can't add attachments
Multipartemail: Text message, you can add multiple attachments
htmlemail:html format mail with all "features" of the Multipartemail class
Emailattchment: Attachment class, you can add local resources, or you can specify resources on the network to automatically send the resources on the network to the download.

Send a basic Text format message:
==============
Simpleemail email = new Simpleemail (); SMTP host Email.sethostname ("mail.test.com"); Login mail server username and password email.setauthentication ("Test", "Testpassword"); Receiver Email.addto ("jdoe@somewhere.org", "John Doe"); Sender Email.setfrom ("me@apache.org", "Me"); Title Email.setsubject ("Test message"); The content of the message email.setmsg ("This was a simple test of commons-email"); Send Email.send ();
Send text format with attachment message:
==================
Attachments, you can define multiple attachment objects emailattachment attachment = new Emailattachment (); Attachment.setpath ("E://1.pdf"); Attachment.setdisposition (emailattachment.attachment); Attachment.setdescription ("Picture of John"); Multipartemail email = new Multipartemail (); SMTP host Email.sethostname ("mail.test.com"); Login mail server username and password email.setauthentication ("Test", "Testpassword"); Receiver Email.addto ("jdoe@somewhere.org", "John Doe"); Sender Email.setfrom ("me@apache.org", "Me"); Title Email.setsubject ("Test message"); The content of the message email.setmsg ("This was a simple test of commons-email"); Add Attachment Email.attach (attachment); Send Email.send ();
Send HTML format with attachment message:
=================
Attachments, you can define multiple attachment objects emailattachment attachment = new Emailattachment (); Attachment.setpath ("E://1.pdf"); Attachment.setdisposition (emailattachment.attachment); Attachment.setdescription ("Picture of John"); Htmlemail email = new Htmlemail (); SMTP host Email.sethostname ("mail.test.com"); Login mail server username and password email.setauthentication ("Test", "Testpassword"); Receiver Email.addto ("jdoe@somewhere.org", "John Doe"); Sender Email.setfrom ("me@apache.org", "Me"); Title Email.setsubject ("Test message"); The content of the message email.sethtmlmsg ("This was a simple test of commons-email"); Add Attachment Email.attach (attachment); Send
A complete sample of the program is provided below:


Import org.apache.commons.mail.*; public class SendEmail {public static void main (string[] arg) throws Exception {simpleemail email = new Simpleemail ( ); SMTP host Email.sethostname ("smtp.sina.com"); Login mail server username and password email.setauthentication ("Tomcat", "123456"); Receiver Email.addto ("tomcat@yahoo.com.cn", "Zieckey"); Sender Email.setfrom ("tocat@sina.com", "Me"); Title Email.setsubject ("Hello"); The content of the message email.setmsg ("This was a simple test of commons-email"); Send Email.send (); SYSTEM.OUT.PRINTLN ("Send email successful!"); } }

Commons-email provides Simpleemail, Multipartemail, Htmlemail, Emailattachment and other classes, only you need to write a few lines of normal thinking of the code can send various types of email, generally we use JavaMail send email will encounter Chinese garbled problem, mainly appear in the code in the English system execution, the processing method is the theme and content using GBK or UTF-8 character set.

There are examples of commons-email use in http://jakarta.apache.org/commons/email/userguide.html, if Directly with the first example in the English environment to send a message with Chinese theme or content will also appear garbled,

Here to the first example of a slightly modified, you can send the Email in Chinese does not appear garbled, as follows:

Package Com.unmi; Import Org.apache.commons.mail.SimpleEmail; public class MailTo {/** * @Author Unmi * @param args * @throws Exception/public static void Main (string[) args) throw s Exception {//send Simple mail simpleemail email = new Simpleemail (); Email.sethostname ("mail.2911.net");//need mail to send server authentication, username/password em Ail.setauthentication ("Broodwar", "xxxxxx"); Email.addto ("fantasia@sina.com", "Fantasia"); Email.setfrom ("Broodwar@2911.net", "Broodwar"); Sets the character set of the theme to UTF-8 email.setcharset ("UTF-8"); Email.setsubject ("Test email subject"); Email.buildmimemessage (); Set the character set of the content to UTF-8, buildmimemessage before you can set the content text Email.getmimemessage (). SetText ("Test message Content", "UTF-8"); Email.sendmimemessage (); } }


It's good to use this commons-email component in the future when you send mail in Java, if you want to send an HTML message or a message with an attachment to learn to http://jakarta.apache.org/commons/email/ Userguide.html in the example to do, appear in Chinese garbled, follow to the source code to find ways to move their brains.

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.