Mail.jar, Commons-email-x.x.jar, Activation.jar, Log4j.jar four jars, put them in the project.
Http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html
This class implements a more complete message delivery feature, including sending in HTML, adding attachments, and cc people. Here is the specific code:
Write a Simple Mail object first
PackageCom.shadow.mvc.vo;Importjava.io.Serializable;/*** Mail Attribute entity * *@authorShadow **/@SuppressWarnings ("Serial") Public classMailImplementsSerializable { Public Static FinalString encodeing = "UTF-8"; PrivateString host;//Server Address PrivateString Sender;//Sender's Mailbox PrivateString receiver;//Recipient's Mailbox PrivateString name;//Sender Nickname PrivateString username;//Account Number PrivateString password;//Password PrivateString subject;//Theme PrivateString message;//information (HTML supported) PublicString gethost () {returnhost; } Public voidSethost (String host) { This. Host =host; } PublicString Getsender () {returnSender; } Public voidSetsender (String sender) { This. Sender =Sender; } PublicString Getreceiver () {returnreceiver; } Public voidSetreceiver (String receiver) { This. Receiver =receiver; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Getsubject () {returnsubject; } Public voidSetsubject (String subject) { This. Subject =subject; } PublicString getMessage () {returnmessage; } Public voidsetmessage (String message) { This. Message =message; }}
And then just write a tool class.
PackageCom.shadow.extras.util;Importorg.apache.commons.mail.EmailException;ImportOrg.apache.commons.mail.HtmlEmail;ImportOrg.apache.log4j.Logger;ImportCom.shadow.mvc.vo.Mail;/*** Mail Sending Tool implementation class * *@authorShadow * @create 2013/07/12*/ Public classMailutil {protected FinalLogger Logger =Logger.getlogger (GetClass ()); Public BooleanSend (mail mail) {//Send emailHtmlemail email =NewHtmlemail (); Try { //here is the name of the SMTP sending server: 163 as follows: "Smtp.163.com"Email.sethostname (Mail.gethost ()); //setting of the character encoding setEmail.setcharset (mail.encodeing); //Recipient's Mailboxemail.addto (Mail.getreceiver ()); //Sender's MailboxEmail.setfrom (Mail.getsender (), Mail.getname ()); //If authentication information is required, set up authentication: username-password. The registered name and password of the sender on the mail server, respectivelyemail.setauthentication (Mail.getusername (), Mail.getpassword ()); //subject of the message to be sentEmail.setsubject (Mail.getsubject ()); //to send the information, you can use the HTML tag in the message content due to the use of Htmlemailemail.setmsg (Mail.getmessage ()); //SendEmail.send (); if(logger.isdebugenabled ()) {Logger.debug (Mail.getsender () )+ "Send mail to" +mail.getreceiver ()); } return true; } Catch(emailexception e) {e.printstacktrace (); Logger.info (Mail.getsender ()+ "Send mail to" +Mail.getreceiver ()+ "Failed"); return false; } }}
Finally write a main method to test the
Public Static voidMain (string[] args) {Mail mail=NewMail (); Mail.sethost ("Smtp.163.com");//set up the mail server, if not 163, look for the relevantMail.setsender ("[Email protected]"); Mail.setreceiver ("[Email protected]");//Receiving PersonMail.setusername ("[email protected]");//login account, usually the same as the mailbox nameMail.setpassword ("xxxxx");//login password for sender's mailboxMail.setsubject ("Aaaaaaaaa"); Mail.setmessage ("BBBBBBBBBBBBBBBBB"); Newmailutil (). Send (mail); }
Simple Java Mail sending instance of mail