Java Htmlemail Simple implementation code for mail delivery _java

Source: Internet
Author: User

Sending mail is often encountered in Java projects

Java Send mail There are several, today first introduced to you with Htmlemail to send mail, I here is built with Maven

Htmlemail can be copied with HTML

First you need to import the jar package

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactid>commons-email </artifactId>
   <version>1.4</version>
 </dependency>

And then we're going to build a mail class that sends emails JavaBean

public class Mail implements serializable{private static final long serialversionuid = -6390720891150157552l; 

  public static final String encodeing = "UTF-8";
  Server address private String host; 
  Sender's mailbox private String sender; 
  Sender nickname private String name; 
  Account Private String username; 
  Password private String password;
  The recipient's mailbox private String receiver;
  The recipient's name is Private String receivername;
  The recipient's mailbox (key) and name (value) private map<string, string> to;
  CC person's mailbox (key) and name (value) private map<string, string> cc;
  Secret cc person's mailbox (key) and name (value) private map<string, string> bcc; 
  Theme private String subject;

  Information (HTML-enabled) private String message;
  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.receiver = receiver;
  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;
  Public String Getreceivername () {return receivername;
  } public void Setreceivername (String receivername) {this.receivername = Receivername;
  Public map<string, String> Getto () {return to; } public void Setto (map<string, STRING&GT
  To) {this.to = to;
  Public map<string, String> GETCC () {return cc;
  public void SETCC (map<string, string> cc) {this.cc = cc;
  Public map<string, String> getbcc () {return BCC;
  } public void Setbcc (map<string, string> bcc) {this.bcc = Bcc; }
}

And then create a tool class to send the message mailutil

public class Mailutil {public Boolean send (mail mail) {htmlemail email = new Htmlemail ();
      try {//Here is the name of the SMTP send server: 163 is as follows: "Smtp.163.com" Email.sethostname (Mail.gethost ());
      The setting of the character encoding set Email.setcharset (mail.encodeing); 
      Sender's Mailbox Email.setfrom (Mail.getsender (), Mail.getname ()); If you need authentication information, set authentication: username-password.

      Email.setauthentication (Mail.getusername (), Mail.getpassword ()) for the sender's registered name and password on the mail server respectively;
      Set recipient information Setto (email, mail);
      Set up cc person information SETCC (email, mail);
      Set up SETBCC information (email, mail);
      The subject of the message to be sent Email.setsubject (Mail.getsubject ());
      The information to be sent, because of the use of Htmlemail, you can use HTML tags in the content of the message email.sethtmlmsg (Mail.getmessage ());
      Send Email.send ();
      if (log.isdebugenabled ()) {Log.info (Mail.getsender () + "Send mail to" + mail.getreceiver ());
    return true;
      catch (Exception e) {e.printstacktrace (); Log.info (Mail.getsender () + "Send mail to "+ mail.getreceiver () +" failure ");
    return false; /** * Set Recipient information * * @param email * @param mail * @throws emailexception/private void Setto
      (Htmlemail email, mail mail) throws emailexception{//recipient is not empty if (Stringutils.isnotempty (Mail.getreceiver ())) { The recipient name is not an empty if (Stringutils.isnotempty (Mail.getreceivername ())) {Email.addto (Mail.getreceiver ()), MAIL.G
      Etreceivername ());
      else {email.addto (Mail.getreceiver ()); }//Recipient Map is not empty if (Mail.getto ()!= null) {for (map.entry<string, string> entry:mail.getTo () . EntrySet ()) {//Recipient name is not an empty if (Stringutils.isnotempty (Entry.getvalue ())) {Email.addto (entry.get
        Key (), Entry.getvalue ());
        else {email.addto (Entry.getkey ()); /** * Set CC person information * * @param email * @param mail * @throws emailexception/priv ate void SETCC (htmlemail email, Mail maIL) throws emailexception{//CC person MAP is not empty if (MAIL.GETCC ()!= null) {for (map.entry<string, string> E Ntry:mail.getCc (). EntrySet ()) {//CC person name is not empty if (Stringutils.isnotempty (Entry.getvalue ())) {em
        AIL.ADDCC (Entry.getkey (), Entry.getvalue ());
        else {EMAIL.ADDCC (Entry.getkey ()); /** * Set up the secret sender information * * @param email * @param mail * @throws emailexception/priv
      ate void setbcc (htmlemail email, mail mail) throws emailexception{//Secret sender Map is not empty if (MAIL.GETBCC ()!= null) { For (map.entry<string, string> entry:mail.getBcc (). EntrySet ()) {//The secret sender name is not empty if (stringutils.is
        Notempty (Entry.getvalue ())) {EMAIL.ADDBCC (Entry.getkey (), Entry.getvalue ());
        else {EMAIL.ADDBCC (Entry.getkey ()); }
      }
    }
  }
}

It's about the same here, Long March, only one last step.

Test, test, test

Here, write a JUnit method to test

@Test public void SendMail () {Mail mail = new mail ();
    Set up mail server Mail.sethost ("smtp.exmail.qq.com");
    Sender email Address mail.setsender ("chenhao@kezhanbang.cn");
    Sender name Mail.setname ("Java. Pupil");
    Login account, is generally the same as the mailbox name Bar Mail.setusername ("chenhao@kezhanbang.cn");
    Sender's Mailbox login password Mail.setpassword ("xxxxxxxx");
    Receiver Mail.setreceiver ("huntertochen@163.com");
    Mail.setreceivername ("I want female Ticket");
    Mail.setsubject ("Long March last Step Test"); String html = "<!
    DOCTYPE html> "; HTML + + " 

Just log in to the mailbox and see.

Let's take a look at the subject content, the sender name and the recipient name are the same as the code, if the same, congratulations you succeed

Here, actually send the mail is over!!!

Here's the last little bit of knowledge in the long-winded mailto

mailto: This protocol allows you to create a hyperlink to an e-mail address that allows you to send e-mail messages over the Internet

We click Send mail to open your email client a send interface, if you have a copy of the information, will also bring you

The above Java Htmlemail mail to send a simple implementation code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.