Java Mail Learning Finishing

Source: Internet
Author: User

1. Load Jar Pack Mail.jar, Commons-email-X.x.jar, Activation.jar<dependency> <groupId>commons-email</groupId> <artifactid>commons-email</            artifactid> <version>1.1</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> & lt;version>1.1</version> </dependency> <dependency> <groupid>org.slf4j </groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version > </dependency> 2. Write a Mail object PackageCom.study.bean; Importjava.io.Serializable; /*** * Mail attribute entity **/@SuppressWarnings ("Serial")             Public classMail { 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; }            }                                        3. Write a tool class PackageCom.study.service.impl; Importorg.apache.commons.mail.EmailException; ImportOrg.apache.commons.mail.HtmlEmail; ImportOrg.apache.log4j.Logger; ImportCom.study.bean.Mail; /*** Created by QD-ZHENGWP on 16/07/19. * * Mail Sending Tool implementation class **/             Public classMailutill {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; }                }            }                            4. Test Public Static voidTest () {Mail mail=NewMail (); Mail.sethost ("Smtp.163.com");//setting up a mail serverMail.setsender ("[email protected]");//SenderMail.setreceiver ("[email protected]");//Receiving PersonMail.setusername ("[email protected]");//login account, usually the same as the mailbox nameMail.setpassword ("haokaili1124");//login password for sender's mailboxMail.setsubject ("Hello, everyone!")); Mail.setmessage ("I'm a little bird."); NewMailutill (). Send (mail); }    

Java Mail Learning Finishing

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.