Send a viable template Ali cloud above e-mail

1. First Class Mail dto layer is

public  class Mail the implements the Serializable {
     / * * 
     * sequence number 
     * / 
    Private  static Final Long serialVersionUID = - 3562218214168975242L ;
     / * * 
     * Message Encoding 
     * / 
    public  static Final String ENCODEING = " UTF-. 8 " ;
     / * * 
     * server address 
     * / 
    Private String Host;
     / * * 
     * server port number 
     * / 
    Private String portNumber;
     / * * 
     * E-mail sender 
     * /
    Private String SENDER;
     / * * 
     * recipient's mailbox 
     * / 
    Private String Receiver;
     / * * 
     * Sender Nickname 
     * / 
    Private String name;
     / * * 
     * account 
     * / 
    Private String username;
     / * * 
     * Password 
     * / 
    Private String password;
     / * * 
     * theme 
     * / 
    Private String Subject;
     / * * 
     * information (support HTML) 
     * / 
    Private String the message; 
}
Remember generate getter and setter methods 

2 followed MailUtil Tools
org.apache.commons.mail.EmailException Import; 
Import org.apache.commons.mail.HtmlEmail; 

public  class MailUtil {
     public Boolean Send (Mail mail) {
         // send email subject 
        HtmlEmail email = new new HtmlEmail ();
         the try {
             / / here is the sending SMTP server name 
            email.setHostName (mail.getHost ());
             // port number if the port number is not empty, user-defined port number for the SMTP server sends 
            IF (! "" .equals (mail. getPortNumber ())) { 
                email.setSSLOnConnect ( to true ); 
                email.setSslSmtpPort (mail.getPortNumber ());
            } 
            // set the character set encoding 
            email.setCharset (Mail.ENCODEING);
             // recipient's mailbox 
            email.addTo (mail.getReceiver ());
             // sender mailbox 
            email.setFrom (mail.getSender () , mail.getName ());
             // if authentication information is needed, then set the authentication: user name - password. Senders are registered on the mail server name and password 
            email.setAuthentication (mail.getUsername (), mail.getPassword ());
             // message subject to be sent 
            email.setSubject (mail.getSubject ());
             / / information to be transmitted, since the HtmlEmail, HTML tags can be used in the message content 
            email.setMsg (mail.getMessage ());
             // send
             email.send ();
             return  to true ;
        } catch (EmailException e) {
            e.printStackTrace();
            return false;
        }
    }
This class package top two lines of import needs commons- email.jar jar package 
in addition to write this feature also requires mail.jar jar package 
I created a maven project dependencies directly to the website directly into these two jar package a 

3 call tools email
public  void the Test () { 
        Mail mail = new new Mail (); 
        mail.setHost ( " smtp.qq.com " );            // set up a mail server, if not QQ mailbox, their own look for related 
        mail.setPortNumber ( " 465 " );              // set the mail server port number, default 25 
        mail.setSender ( " sender's mailbox, here QQ " );              // sender 
        mail.setName ( " xx company " );                     // sender nickname 
        mail .setReceiver ( " the recipient's mailbox, you can easily write ");            // recipient 
        mail.setUsername ( " login ID " );            // login account and mailbox names are generally the same as 
        mail.setPassword ( " QQ mailbox Authorization Code " ;)   // When the QQ-mail login third-party client the password box enter "authorization code" to verify. Other specific password to view the mail server Description 

        // send messages 
        
        // Here you can write some mail content or computing functions 
        
        // defined message header 
        String title = " title " 
        mail.setSubject (title); 

        // define the message content 
        StringBuilder emailContent = new new the StringBuilder ();
         // less easily change 
        emailContent.append ( "SUMMARY a " ); 
        emailContent.append ( " ........... " ); 
        mail.setMessage (String.valueOf (emailContent)); 
        
        IF ( new new . MailUtil () Send (mail) ) { 
            . the System OUT .println ( " sent successfully " ); 
        } the else { 
            the System. OUT .println ( " transmission failed " ); 
        } 
    }
This way, mail transmission function is complete! 
Congratulations, congratulations

Guess you like

Origin www.cnblogs.com/mike-JP/p/11368499.html