Java uses Commons-email to send and encapsulate messages

Source: Internet
Author: User
Tags addall

In this example, we use Commons-email to send messages and encapsulate them, support HTML content and attachments, and Commons email is a mail client component under Apache's Commons sub-project, which is based on javamail and greatly simplifies the sending and receiving of messages.

The tool class supports multiple senders, multiple cc people, multiple bcc, multiple attachments, and more. Very powerful.

Pom.xml

<dependency>    <groupId>org.projectlombok</groupId>    <artifactid>lombok</ artifactid>    <version>1.16.10</version></dependency><!--Email--><dependency >    <groupId>org.apache.commons</groupId>    <artifactid>commons-email</artifactid >    <version>1.4</version></dependency>
View Code

Mailinfo.java
 PackageCom.example.demo.email;ImportLombok. Data;Importorg.apache.commons.mail.EmailAttachment;Importjava.util.List;/*** Created by knocking the code out of Kaka on 2018/3/29.*/@Data Public classMailinfo {//Recipient    PrivateList<string> toaddress =NULL; //cc person Address    PrivateList<string> ccaddress =NULL; //Secret Delivery Person    PrivateList<string> bccaddress =NULL; //Attachment Information    Privatelist<emailattachment> attachments =NULL; //Message Subject    PrivateString subject; //text content of the message    PrivateString content;  Public voidaddtoaddress (String toaddress) { This. Toaddress.add (toaddress); }     Public voidAddtoaddress (list<string>toaddress) {         This. Toaddress.addall (toaddress); }     Public voidAddccaddress (list<string>ccaddress) {        if(NULL! = ccaddress && ccaddress.size () > 0)             This. Ccaddress.addall (ccaddress); }}
View Code
Mailutil.java
 PackageCom.example.demo.email;Importorg.apache.commons.mail.EmailAttachment;Importorg.apache.commons.mail.EmailException;ImportOrg.apache.commons.mail.HtmlEmail;Importjava.util.List;/*** Created by knocking the code out of Kaka on 2018/3/29.*/ Public classMailutil {//Email    Private StaticString mailserverhost = "smtp.163.com"; Private StaticString mailsenderaddress = "[Email protected]"; Private StaticString Mailsendernick = "The card that knocks the code"; Private StaticString mailsenderusername = "[Email protected]"; Private StaticString Mailsenderpassword = "xxxxx"; /*** Send mail Method (HTML format, support attachment) * *@returnvoid*/     Public Static voidSendEmail (Mailinfo mailinfo) {Try{htmlemail Email=NewHtmlemail (); //configuration InformationEmail.sethostname (mailserverhost);            Email.setfrom (Mailsenderaddress,mailsendernick);            Email.setauthentication (Mailsenderusername,mailsenderpassword); Email.setcharset ("UTF-8");            Email.setsubject (Mailinfo.getsubject ());            Email.sethtmlmsg (Mailinfo.getcontent ()); //Add an attachmentlist<emailattachment> attachments =mailinfo.getattachments (); if(NULL! = Attachments && attachments.size () > 0) {                 for(inti = 0; I < attachments.size (); i++) {Email.attach (Attachments.get (i)); }            }            //RecipientList<string> toaddress =mailinfo.gettoaddress (); if(NULL! = toaddress && toaddress.size () > 0) {                 for(inti = 0; I < toaddress.size (); i++) {email.addto (Toaddress.get (i)); }            }            //cc PersonList<string> ccaddress =mailinfo.getccaddress (); if(NULL! = ccaddress && ccaddress.size () > 0) {                 for(inti = 0; I < ccaddress.size (); i++) {EMAIL.ADDCC (Ccaddress.get (i)); }            }            //Mail template secret send peopleList<string> bccaddress =mailinfo.getbccaddress (); if(NULL! = bccaddress && bccaddress.size () > 0) {                 for(inti = 0; I < bccaddress.size (); i++) {EMAIL.ADDBCC (Ccaddress.get (i));            }} email.send (); System.out.println ("Mail sent successfully!" "); } Catch(emailexception e) {e.printstacktrace (); }    }}
View Code

Test.java
 PackageCom.example.demo.email;Importorg.apache.commons.mail.EmailAttachment;Importjava.util.ArrayList;Importjava.util.List;/*** Created by knocking the code out of Kaka on 2018/3/29.*/ Public classTest {/**     * @returnvoid*/     Public Static voidMain (string[] args) {mailinfo mailinfo=NewMailinfo (); List<String> toList =NewArraylist<string>(); Tolist.add ("[Email protected]"); List<String> cclist =NewArraylist<string>(); Cclist.add ("[Email protected]"); List<String> bcclist =NewArraylist<string>(); Bcclist.add ("[Email protected]"); //Add an attachmentEmailattachment att =Newemailattachment (); Att.setpath ("E:\\liuzhonghua\\ Backup \\xx.txt"); Att.setname ("Remote address. txt"); List<EmailAttachment> Atts =NewArraylist<emailattachment>();        Atts.add (ATT);        Mailinfo.setattachments (Atts); Mailinfo.settoaddress (toList);//RecipientMailinfo.setccaddress (cclist);//cc PersonMailinfo.setbccaddress (bcclist);//Secret Delivery PersonMailinfo.setsubject ("Test Subject"); Mailinfo.setcontent ("Content: );    Mailutil.sendemail (Mailinfo); }}
View Code

OK, pro-Test available!!!!!!

Java uses Commons-email to send and encapsulate messages

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.