Download Java and java Official Website

Source: Internet
Author: User

Download Java and java Official Website

In Java projects, emails are often sent back.

There are several kinds of mails sent in Java. Today we will introduce you to sending mails using HtmlEmail. Here we use Maven to set up

HtmlEmail can copy HTML

 

First, import the jar package.

1 <dependency>2     <groupId>org.apache.commons</groupId>3     <artifactId>commons-email</artifactId>4     <version>1.4</version>5 </dependency>

 

Then we will create a Mail-type JavaBean

1 public class Mail implements Serializable {2 3 private static final long serialVersionUID =-6390720891150157552L; 4 public static final String ENCODEING = "UTF-8"; 5 6 // server address 7 private String host; 8 // sender's mailbox 9 private String sender; 10 // sender's nickname 11 private String name; 12 // Account 13 private String username; 14 // password 15 private String password; 16 // recipient's email address 17 private String receiver; 18 // recipient's name 19 private String receiverName; 20 // recipient's email address (key) and name (value) 21 private Map <String, String> to; 22 // cc recipient's email address (key) and name (value) 23 private Map <String, String> cc; 24 // the email address (key) and name (value) 25 private Map <String, String> bcc; 26 // topic 27 private String subject; 28 // information (HTML supported) 29 private String message; 30 31 public String getHost () {32 return host; 33} 34 public void setHost (String host) {35 this. host = host; 36} 37 public String getSender () {38 return sender; 39} 40 public void setSender (String sender) {41 this. sender = sender; 42} 43 public String getReceiver () {44 return receiver; 45} 46 public void setReceiver (String receiver) {47 this. extends ER = extends er; 48} 49 public String getName () {50 return name; 51} 52 public void setName (String name) {53 this. name = name; 54} 55 public String getUsername () {56 return username; 57} 58 public void setUsername (String username) {59 this. username = username; 60} 61 public String getPassword () {62 return password; 63} 64 public void setPassword (String password) {65 this. password = password; 66} 67 public String getSubject () {68 return subject; 69} 70 public void setSubject (String subject) {71 this. subject = subject; 72} 73 public String getMessage () {74 return message; 75} 76 public void setMessage (String message) {77 this. message = message; 78} 79 public String getReceiverName () {80 return receiverName; 81} 82 public void setReceiverName (String receiverName) {83 this. receiverName = receiverName; 84} 85 public Map <String, String> getTo () {86 return to; 87} 88 public void setTo (Map <String, String>) {89 this. to = to; 90} 91 public Map <String, String> getCc () {92 return cc; 93} 94 public void setCc (Map <String, String> cc) {95 this. cc = cc; 96} 97 public Map <String, String> getBcc () {98 return bcc; 99} 100 public void setBcc (Map <String, String> bcc) {101 this. bcc = bcc; 102} 103}

 

Then create a tool for sending emails, MailUtil.

1 public class MailUtil {2 3 public Boolean send (Mail mail) {4 HtmlEmail email = new HtmlEmail (); 5 try {6 // here is the name of the SMTP sending server: 163 is as follows: "smtp.163.com" 7 email. setHostName (mail. getHost (); 8 // Character Set setting 9 email. setCharset (Mail. ENCODEING); 10 // the sender's email address 11 email. setFrom (mail. getSender (), mail. getName (); 12 // If authentication information is required, set authentication: User Name-password. The registration name and password of the sender on the email server 13 email respectively. setAuthentication (mail. getUsername (), mail. getPassword (); 14 15 // set Recipient Information 16 setTo (email, mail); 17 // set CC information 18 setCc (email, mail ); 19 // set the BCC information 20 setBcc (email, mail); 21 // The email Subject 22 email to be sent. setSubject (mail. getSubject (); 23 // the message to be sent. Because HtmlEmail is used, the HTML Tag 24 email can be used in the mail content. setHtmlMsg (mail. getMessage (); 25 // send 26 emails. send (); 27 if (Log. isDebugEnabled ()){ 28 Log.info (mail. getSender () + "send email to" + mail. getcycler (); 29} 30 return true; 31} catch (Exception e) {32 e. printStackTrace (); 33 Log.info (mail. getSender () + "send email to" + mail. getcycler () + "failed"); 34 return false; 35} 36} 37 38/** 39 * set the Recipient Information to 40*41 * @ param email 42 * @ param mail 43 * @ throws EmailException 44 */45 private void setTo (HtmlEmail email, mail mail) throws EmailExcep Tion {46 // The recipient is not empty. 47 if (StringUtils. isNotEmpty (mail. getcycler () {48 // The Recipient Name is not blank. 49 if (StringUtils. isNotEmpty (mail. getReceiverName () {50 email. addTo (mail. getcycler (), mail. getReceiverName (); 51} else {52 email. addTo (mail. getcycler (); 53} 54} 55 // The recipient Map is not empty. 56 if (mail. getTo ()! = Null) {57 for (Map. entry <String, String> entry: mail. getTo (). entrySet () {58 // The Recipient Name is not null 59 if (StringUtils. isNotEmpty (entry. getValue () {60 email. addTo (entry. getKey (), entry. getValue (); 61} else {62 email. addTo (entry. getKey ()); 63} 64} 65} 66} 67 68/** 69 * Set CC information 70*71 * @ param email 72 * @ param mail 73 * @ throws EmailException 74 */75 private void setCc (HtmlEmail email, mail m Ail) throws EmailException {76 // CC Map is not empty 77 if (mail. getCc ()! = Null) {78 for (Map. entry <String, String> entry: mail. getCc (). entrySet () {79 // The CC name is not empty. 80 if (StringUtils. isNotEmpty (entry. getValue () {81 email. addCc (entry. getKey (), entry. getValue (); 82} else {83 email. addCc (entry. getKey ()); 84} 85} 86} 87} 88 89/** 90 * Set BCC information 91*92 * @ param email 93 * @ param mail 94 * @ throws EmailException 95 */96 private void setBcc (HtmlEmail email, mail Mail) throws EmailException {97 // BCC Map is not empty 98 if (mail. getBcc ()! = Null) {99 for (Map. entry <String, String> entry: mail. getBcc (). entrySet () {100 // The BCC name is not empty. 101 if (StringUtils. isNotEmpty (entry. getValue () {102 email. addBcc (entry. getKey (), entry. getValue (); 103} else {104 email. addBcc (entry. getKey (); 105} 106} 107} 108} 109}

 

This is probably the last step.

Test, test, test

 

Compile a junit method to test

1 @ Test 2 public void sendMail () {3 Mail mail = new Mail (); 4 // set mail Server 5 Mail. setHost ("smtp.exmail.qq.com"); 6 // sender email address 7 mail. setSender ("chenhao@kezhanbang.cn"); 8 // sender name 9 mail. setName ("Java. pupils "); 10 // login account, usually the same as the mailbox name 11 mail. setUsername ("chenhao@kezhanbang.cn"); 12 // the sender's email login password 13 mail. setPassword ("xxxxxxxx"); 14 // recipient 15 mail. setcycler ("huntertochen@163.com"); 16 mail. setReceiverName (" I want a female ticket "); 17 mail. setSubject (" last test of the Long March "); 18 String html =" <! DOCTYPE html> "; 19 html + =" 

 

Log on to your mailbox.

Let's see if the subject content here is the same as the sender name and recipient name as the code. If so, congratulations!

At this point, the email is actually sent !!!

 

Here is the last small knowledge pointMailto

Mailto: This protocol allows you to create a hyperlink pointing to an email address, through which you can send emails over the Internet.

Click "send mail" to open the sending interface of your mail client. If you have a copy information, it will also bring you

 

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.