JavaMail (1): Use JavaMail to send a simple email and javamail to send
JavaMail provides developers with programming interfaces related to email processing. It is an API released by Sun to process emails. It can easily perform some common mail Transmission. But it is not included in JDK, to use JavaMail first download javax. mail. jar: https://javaee.github.io/javamail/
Custom Verification:
/*** Custom verification ** @ author fly * @ 2017-05-09 **/public class MyAuthenticator extends Authenticator {private String userName; private String password; public MyAuthenticator () {} public MyAuthenticator (String userName, String password) {this. userName = userName; this. password = password ;}@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (userName, password );}}
Encapsulate Mail Information:
/*** Encapsulate the email message ** @ author fly * @ 2017-05-09 */public class MailSenderInfo {// IP address and port private String mailServerHost of the server sending the email; private String mailServerPort = "25"; // the address of the mail sender, private String fromAddress; // the address of the mail recipient, private String toAddress; // log on to the mail sending server with the userName and password private String userName; private String password; // whether authentication is required private boolean validate = false; // The mail subject private String subject; // Text of the email Private String content; // the file name of the email attachment: private String [] attachFileNames;/*** get the email session attribute */public Properties getProperties () {Properties p = new Properties (); p. put ("mail. smtp. host ", this. mailServerHost); p. put ("mail. smtp. port ", this. mailServerPort); p. put ("mail. smtp. auth ", validate? "True": "false"); return p;} public String getMailServerHost () {return mailServerHost;} public void setMailServerHost (String mailServerHost) {this. mailServerHost = mailServerHost;} public String getMailServerPort () {return mailServerPort;} public void setMailServerPort (String mailServerPort) {this. mailServerPort = mailServerPort;} public String getFromAddress () {return fromAddress;} public void setFromAddress (String fromAddress) {this. fromAddress = fromAddress;} public String getToAddress () {return toAddress;} public void setToAddress (String toAddress) {this. toAddress = toAddress;} 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 boolean isValidate () {return validate;} public void setValidate (boolean validate) {this. validate = validate;} public String getSubject () {return subject;} public void setSubject (String subject) {this. subject = subject;} public String getContent () {return content;} public void setContent (String content) {this. content = content;} public String [] getAttachFileNames () {return attachFileNames;} public void setAttachFileNames (String [] attachFileNames) {this. attachFileNames = attachFileNames ;}}
Encapsulate the email sending code
/*** Encapsulate the Email sending Code * @ author fly * @ 2017-05-09 **/public class Email {/*** send an Email in text format * @ param mailInfo * @ throws Exception */public static void sendTextMail (MailSenderInfo mailInfo) throws Exception {// determine whether identity authentication is required. MyAuthenticator authenticator = null; Properties pro = mailInfo. getProperties (); if (mailInfo. isValidate () {// If identity authentication is required, create a authenticator = new MyAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ();} // construct a mail sending session Session sendMailSession = Session based on the mail session attribute and password validators. getDefaultInstance (pro, authenticator); // create an email Message mailMessage = new MimeMessage (sendMailSession) based on the session; // create the mail sender Address from = new InternetAddress (mailInfo. getFromAddress (); // sets the sender of the email message mailMessage. setFrom (from); // create the recipient Address of the email and set it to Address to = new InternetAddress (mailInfo. getToAddress (); mailMessage. setRecipient (Message. recipientType. TO, to); // set the subject of the email message mailMessage. setSubject (mailInfo. getSubject (); // set the mail message sending time mailMessage. setSentDate (new Date (); // you can specify String mailContent = mailInfo. getContent (); mailMessage. setText (mailContent); // send the email Transport. send (mailMessage);}/*** send an email in HTML format * @ param mailInfo message to be sent * @ throws Exception */public static void sendHtmlMail (MailSenderInfo mailInfo) throws Exception {// determine whether identity authentication is required. MyAuthenticator authenticator = null; Properties pro = mailInfo. getProperties (); if (mailInfo. isValidate () {// If identity authentication is required, create a authenticator = new MyAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ();} // construct a mail sending session Session sendMailSession = Session based on the mail session attribute and password validators. getDefaultInstance (pro, authenticator); // create an email Message mailMessage = new MimeMessage (sendMailSession) based on the session; // create the mail sender Address from = new InternetAddress (mailInfo. getFromAddress (); // sets the sender of the email message mailMessage. setFrom (from); // create the recipient Address of the email and set it to Address to = new InternetAddress (mailInfo. getToAddress (); // Message. recipientType. the TO attribute indicates that the receiver type is TO mailMessage. setRecipient (Message. recipientType. TO, to); // set the subject of the email message mailMessage. setSubject (mailInfo. getSubject (); // set the mail message sending time mailMessage. setSentDate (new Date (); Multipart mainPart = new MimeMultipart (); // create a MimeBodyPart BodyPart HTML containing html content = new MimeBodyPart (); html. setContent (mailInfo. getContent (), "text/html; charset = UTF-8"); mainPart. addBodyPart (html); // set the MiniMultipart object to mailMessage. setContent (mainPart); // send the email Transport. send (mailMessage );}
Test procedure:
Public class JavaMailTest {public static void main (String [] args ){
// Set MailSenderInfo mailInfo = new MailSenderInfo (); mailInfo. setMailServerHost ("smtp.163.com"); mailInfo. setMailServerPort ("25"); mailInfo. setValidate (true); mailInfo. setUserName ("qfanliyan@163.com"); mailInfo. setPassword (""); // your mailbox password. If the client Authorization password is enabled for your mailbox, this is your client Authorization password mailInfo. setFromAddress ("qfanliyan@163.com"); mailInfo. setToAddress ("ifanliyan@qq.com"); mailInfo. setSubject ("this is a test email"); mailI Nfo. setContent ("Hello! This is a test Email "); try {Email. sendTextMail (mailInfo); // Email. sendHtmlMail (mailInfo);} catch (Exception e) {e. printStackTrace ();}}}
The message is successfully sent as follows: