Send mail using commons email

Source: Internet
Author: User

Commons Email is a component of the Apache Commons library that has been packaged in Java mail to provide a simplified API for developers to use. It relies on javax.mail.

First download the Commons-email-1.4.jar and Javax.mail.jar, and set the build path.

The following is a simple tool class that can send text messages to a specified address.

 PackageCom.hzfi.flume;ImportOrg.apache.commons.mail.DefaultAuthenticator;ImportOrg.apache.commons.mail.Email;Importorg.apache.commons.mail.EmailException;ImportOrg.apache.commons.mail.SimpleEmail;/** * @authorliny * Mail sending tool class*/ Public classMailutil {Private StaticString _user_name_ = "[Email protected]"; Private StaticString _password_ = "Linyang210"; Private StaticString _smtp_address_ = "smtp.exmail.qq.com"; Private Final Static int_smtp_port_ssl_ = 465; Private StaticString from = "[Email protected]";  Public Static voidSendMail (String toaddress, string subject, string content) {Email email=NewSimpleemail ();        Email.sethostname (_smtp_address_);        Email.setsmtpport (_smtp_port_ssl_); Email.setauthenticator (NewDefaultauthenticator (_user_name_, _password_)); //Email.setsslonconnect (true);Email.setssl (true);//commons-mail-1.1 supported methods, using Setsslonconnect (true) in 1.4 instead        Try{email.setfrom (from);            Email.setsubject (subject);            Email.setmsg (content);            Email.addto (toaddress);        Email.send (); } Catch(emailexception e) {e.printstacktrace (); }    }         Public Static voidMain (string[] args) {//TestString subject = "Test Message"; String content= "test-test test ... Test content ... This is from the Mailutil.java. "; String to= "[Email protected]";    Mailutil.sendmail (to, subject, content); }}

Send mail using commons email

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.