Java mail email sending instance

Source: Internet
Author: User

The project uses one requirement. After completing a specific task, you need to automatically send an email to the customer. It is relatively simple to read javamail... don't worry, you know, then start to write a simple entry, first Baidu find

Mail. jar, commons-email-X.X.jar, activation. Jar these three jar, put into the project

Write a Simple Mail object first

Package COM. shadow. MVC. VO; import Java. io. serializable;/*** mail attribute entity ** @ author shadow **/@ suppresswarnings ("serial") public class mail implements serializable {public static final string encodeing = "UTF-8 "; private string host; // server address private string sender; // sender's email address private string Explorer; // recipient's email address private string name; // sender's nickname private string username; // account private string password; // password private string subject; // topic private string message; // information (HTML supported) Public String gethost () {return host ;} public void sethost (string host) {This. host = host;} Public String getsender () {return sender;} public void setsender (string sender) {This. sender = sender;} Public String getreceiver () {return receiver;} public void setreceiver (string receiver) {This. extends ER = extends er;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} Public String getsubject () {return subject;} public void setsubject (string subject) {This. subject = subject;} Public String getmessage () {return message;} public void setmessage (string message) {This. message = message ;}}

Then write a tool class.

Package COM. shadow. extras. util; import Org. apache. commons. mail. emailexception; import Org. apache. commons. mail. htmlemail; import Org. apache. log4j. logger; import COM. shadow. MVC. VO. mail;/*** mail sending tool implementation class ** @ author shadow * @ create 2013/07/12 */public class mailutil {protected final logger = logger. getlogger (getclass (); Public Boolean send (mail) {// send emailhtmlemail email = new htmlemail (); try {// The Name Of The SMTP sending server is as follows: 163: "smtp.163.com" email. sethostname (mail. gethost (); // set email for the character set. setcharset (mail. encodeing); // email address of the recipient. addto (mail. getcycler (); // sender's email. setfrom (mail. getsender (), mail. getname (); // If you need authentication information, set authentication: User Name-password. The registration name and password email of the sender on the email server. setauthentication (mail. getUserName (), mail. getPassword (); // The subject email to be sent. setsubject (mail. getsubject (); // the message to be sent. Because htmlemail is used, you can use the HTML Tag email in the mail content. setmsg (mail. getmessage (); // send an email. send (); If (logger. isdebugenabled () {logger. debug (mail. getsender () + "send email to" + mail. getcycler ();} return true;} catch (emailexception e) {e. printstacktrace (); logger.info (mail. getsender () + "send email to" + mail. getcycler () + "failed"); Return false ;}}}

Finally, write a main method for testing.

Public static void main (string [] ARGs) {mail = new mail (); mail. sethost ("smtp.163.com"); // set up the email server. If you do not need 163 email, find the relevant mail. setsender ("shadowsick@163.com"); mail. setcycler ("shadowsick@163.com"); // recipient mail. setusername ("shadowsick@163.com"); // login account, is generally the same as the mailbox name mail. setpassword ("XXXXX"); // mail the sender's email logon password. setsubject ("aaaaaaaaa"); mail. setmessage ("bbbbbbbbbbbbbbbbbbb"); New mailutil (). send (Mail );}

Finally, let's look at the effect.

Related Article

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.