Mailsenderinfo.java
Package Com.nihaorz.mail.util;import Java.util.properties;public class Mailsenderinfo {//IP and port private for the server sending the message String Mailserverhost;private String mailserverport = "25";//the address of the sender of the message private String fromaddress;//The address of the mail receiver private String toaddress;//the user name and password of the login mail sending server private string Username;private string password;//whether the authentication private Boolean is required validate = false;//Message Subject private string subject;//text content of the message private string content;//message attachment file name Private string[] attachfilenames;/** */ /** * Get mail Session Properties */public Properties GetProperties () {Properties P = new Properties ();p. put ("Mail.smtp.host", This.mailserv Erhost);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 Boolean isvalidate () {return validate;} public void Setvalidate (Boolean validate) {this.validate = validate;} Public string[] Getattachfilenames () {return attachfilenames;} public void Setattachfilenames (string[] fileNames) {this.attachfilenames = FileNames;} Public String getfromaddress () {return fromaddress;} public void setfromaddress (String fromaddress) {this.fromaddress = fromaddress;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} 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 Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public String getcontent () {return content;} public void SetContent (String textcontent) {this.content = textcontent;}}
Myauthenticator.java
Package Com.nihaorz.mail.util;import Javax.mail.authenticator;import Javax.mail.passwordauthentication;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;} Protected Passwordauthentication getpasswordauthentication () {return new passwordauthentication (userName, password);}}
Simplemailsender.java
Import Java.util.date;import java.util.properties;import Javax.mail.address;import Javax.mail.bodypart;import Javax.mail.message;import Javax.mail.messagingexception;import Javax.mail.multipart;import javax.mail.Session; Import Javax.mail.transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.MimeBodyPart; Import Javax.mail.internet.mimemessage;import Javax.mail.internet.mimemultipart;import Com.nihaorz.mail.util.mailsenderinfo;import Com.nihaorz.mail.util.myauthenticator;public class SimpleMailSender { public static void Main (string[] args) {Mailsenderinfo mailinfo = new Mailsenderinfo (); Mailinfo.setmailserverhost (" Mailinfo.setmailserverport ("smtp.126.com"); Mailinfo.setvalidate (true);//Mailbox Account Mailinfo.setusername ("Nihaorz *//email password Mailinfo.setpassword ("******")///email password//sender address Mailinfo.setfromaddress ("[email protected]"); Mailinfo.settoaddress ("[email protected]"); Mailinfo.setsubject ("This is a test e-mail"); Mailinfo.setcontent ("<a href = ' https://www.baidu.com ' >hTtps://www.baidu.com</a> "); Simplemailsender SMS = new Simplemailsender (); try {System.out.println (Sms.sendhtmlmail (Mailinfo));//Send HTML format} catch ( Exception e) {e.printstacktrace ();}} public boolean sendtextmail (Mailsenderinfo mailinfo) {//Determines if authentication is required myauthenticator authenticator = null; Properties Pro = Mailinfo.getproperties (); if (Mailinfo.isvalidate ()) {//If authentication is required, create a password validator authenticator = new Myauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ());} Constructs a Send mail sessionsession sendmailsession = session.getinstance (Pro, authenticator) based on mail session properties and password verifier; try {// Create a mail message based on session MailMessage = new MimeMessage (sendmailsession);//create mail Sender address from = new InternetAddress ( Mailinfo.getfromaddress ());//Set the sender of the mail message Mailmessage.setfrom (from);//Create the recipient address of the message and set it to the message in the message to = new InternetAddress (Mailinfo.gettoaddress ()); Mailmessage.setrecipient (Message.RecipientType.TO, to);// Set the subject Mailmessage.setsubject (Mailinfo.getsubject ()) of the mail message,//Set the time mailmessage.setsentdate for the mail message to be sent (the new date ();//Set the main contents of the mail message string mailcontent = Mailinfo.getcontent (); Mailmessage.settext (mailcontent);//Send mail Transport.send ( MailMessage); return true;} catch (Messagingexception ex) {ex.printstacktrace ();} return false;} public static Boolean Sendhtmlmail (Mailsenderinfo mailinfo) {//Determines if authentication is required myauthenticator authenticator = null; Properties Pro = Mailinfo.getproperties ();//If authentication is required, create a password validator if (Mailinfo.isvalidate ()) {authenticator = new Myauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ());} Constructs a Send mail sessionsession sendmailsession = session.getinstance (Pro, authenticator) based on mail session properties and password verifier; try {// Create a mail message based on session MailMessage = new MimeMessage (sendmailsession);//create mail Sender address from = new InternetAddress ( Mailinfo.getfromaddress ());//Set the sender of the mail message Mailmessage.setfrom (from);//Create the recipient address of the message and set it to the message in the message to = new InternetAddress (Mailinfo.gettoaddress ());//The Message.RecipientType.TO property indicates that the recipient is of type tomailmessage.setrecipient ( Message.RecipientType.TO, to);//Set the subject of the mail message Mailmessage.setsubject (mAilinfo.getsubject ());//sets the time Mailmessage.setsentdate (new Date ()) to send the mail message, or the Minimultipart class is a container class, An object containing the MimeBodyPart type multipart Mainpart = new Mimemultipart ();//Create a mimebodypartbodypart HTML with HTML content = new MimeBodyPart ();//Set HTML content html.setcontent (Mailinfo.getcontent (), "text/html; Charset=utf-8 "); Mainpart.addbodypart (HTML);//Set Minimultipart object to message content mailmessage.setcontent (Mainpart);// Send mail Transport.send (mailmessage); return true;} catch (Messagingexception ex) {ex.printstacktrace ();} return false;}}
Java Send mail code