The mail entity is encapsulated first, and the protocol uses SMTP:
import java.util.list;import javax.mail.internet.internetaddress;/** * Mailbox Entity class * @ author administrator * */public class emailinfo {// mailbox The address of the server that corresponds to SMTP is private String smtpHost;// e-Mail user name private string user;private string password;// Message Subject private string subject;// message content (you can insert HTML tags in a string) private string content;// Encoding of message content (default nullable) private string charset = "Text/html;charset=utf-8";// The address of the object that receives the e-mail address of the private list<internetaddress> receiveemailaddress;// cc message private List<internetaddress> ccemailaddress;public emailinfo () {super ();} Public emailinfo (string smtphost, string user, string password, string subject, string content, list<internetaddress> receiveemailaddress) { Super (); THIS.SMTPHOST&NBSP;=&NBSP;SMTPHOST;THIS.USER&NBSP;=&Nbsp;user;this.password = password;this.subject = subject;this.content = content; this.receiveemailaddress = receiveemailaddress;} Public string getsmtphost () {return smtphost;} Public void setsmtphost (String smtphost) {this.smtphost = smtphost;} Public string getuser () {return user;} Public void setuser (String user) {this.user = user;} 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 getcontent () {return content;} Public void setcontent (string content) {this.content = content;} Public list<internetaddress> getReceiveemailaddress () {return receiveemailaddress;} Public void setreceiveemailaddress (list<internetaddress> receiveemailaddress) { this.receiveemailaddress = receiveemailaddress;} Public string getcharset () {return charset;} Public void setcharset (String charset) {this.charset = charset;} Public list<internetaddress> getccemailaddress () {return ccemailaddress;} Public void setccemailaddress (list<internetaddress> ccemailaddress) { this.ccemailaddress = ccemailaddress;}}
The
Then encapsulates the tool method class:
import java.util.arraylist;import java.util.list;import java.util.properties;import javax.mail.authenticator;import javax.mail.message.recipienttype;import javax.mail.messagingexception;import javax.mail.passwordauthentication;import javax.mail.session; import javax.mail.transport;import javax.mail.internet.addressexception;import javax.mail.internet.internetaddress;import javax.mail.internet.mimemessage;import org.apache.log4j.logger;import com.safety.pub.common.common;import edu.emory.mathcs.backport.java.util.arrays;/** * Mail Operations public class * @author administrator * */public class EmailUtils {public static Logger log = Logger.getlogger (emailutils.class);/** * send mail * @param email * @throws messagingexception */public static void sendemail (EmailInfo email) throws messagingexcEption {final properties props = generateprop (email); Session mailsession = generatesession (props); Mimemessage message = new mimemessage (mailsession);internetaddress from = New internetaddress (Props.getproperty ("Mail.user")); Message.setfrom (from); List<internetaddress> list = email.getreceiveemailaddress (); if (! Common.isempty (list)) {internetaddress[] to = list.toarray (new internetaddress[]{}); Log.debug ("e-mail address:" + arrays.tostring (to)); Message.setrecipients (recipienttype.to, to); List<internetaddress> cclist = email.getccemailaddress (); if (! Common.isempty (CCList)) {internetaddress[] cc = cclist.toarray (new InternetAddress[]{ }); Log.debug ("The address of the CC message is:" + arrays.tostring (cc)); message.setrecipients (RECIPIENTTYPE.CC,&NBSP;CC);} Log.debug ("The subject of sending the message is:" + email.getsubject ()); Message.setsubject (EMAIl.getsubject ()); Log.debug ("The content body of the sending message is:" + email.getcontent ()); Message.setcontent (Email.getcontent (), email.getcharset ()); Transport.send (message);}} Private static session generatesession (Final properties props) {return Session.getinstance (Props, new authenticator () {@Overrideprotected passwordauthentication getpasswordauthentication () {// username, password string username = props.getproperty (" Mail.user "); String password = props.getproperty ("Mail.password");return new Passwordauthentication (Username, password);}});} Private static properties generateprop (Emailinfo email) {final Properties Prop = new properties ();// indicates that SMTP sends a message and requires authentication Prop.put ("Mail.smtp.auth", "true"); Prop.put ("Mail.smtp.host", email.getsmtphost ());p rop.put ("Mail.user", email.getuser ());p rop.put (" Mail.password ", email.getpassword ()); return prop;} Test to successfully send Public static void main (String[] args) throws messagingexception {string smtphost= "smtp.163.com"; string user= "[ Email protected] "; string password=" CHNASHXBJKBNZXQG " ; string subject= "Test 111"; string content= "Test Send Mail"; Internetaddress to = new internetaddress ("[email Protected] "); arraylist<internetaddress> receiveemailaddress=new arraylist<internetaddress> (); Receiveemailaddress.add (to); Emailinfo email =new emailinfo (smtphost,user,password,subject,content,receiveemailaddress); SendEmail (email);}}
This article is from "Rookie Learning" blog, please make sure to keep this source http://biyusheng.blog.51cto.com/2626140/1657388
Java Mail Tool class