Recently used to want to add a function to the project, that is, when some modules in the project exception, after catching the exception not only write log throw exception! Also need to send a message to the owner of the module, convenient operation and maintenance of positioning problems;
Maven Managed Configurations:
<!--Mail--><dependency> <groupId>javax.mail</groupId> <artifactId>mail< /artifactid> <version>1.4.7</version></dependency>
Paste the relevant code of the demo;
Import Java.util.date;import Java.util.properties;import Javax.activation.datahandler;import Javax.activation.filedatasource;import Javax.mail.message.recipienttype;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 javax.mail.internet.MimeUtility ;/** * Create and send a complex message that contains text, pictures, and attachments. PS: The following QQ account password is not true! * JavaMail version: 1.6.0 * JDK version: JDK 1.7 or higher (required) */public class SendmailUtil2 {//sender's mailbox and password (replace with own mailbox and password) public static Stri ng myemailaccount = "[email protected]";p ublic static String Myemailpassword = "Zybwulgnnxlebjcell";// This password is not the QQ password or email password, to verify. Self-Baidu//Sender Mailbox SMTP server address, must be accurate, different mail server address, the general format is: smtp.xxx.com//QQ mailbox SMTP server address is: smtp.qq.compublic static String Myem Ailsmtphost = "smtp.qq.com";//Recipient mailbox (replaced with a valid mailbox you know) public static String Receivemailaccount = "[email protected]"; public static void Main (string[] args) tHrows Exception {//1. Creating parameter configurations for connecting mail server parameters configuration Properties props = new properties (); Parameter Configuration props.setproperty ("Mail.transport.protocol", "SMTP"); The protocol used (JavaMail specification required) Props.setproperty ("Mail.smtp.host", myemailsmtphost); The SMTP server address of the sender's mailbox Props.setproperty ("Mail.smtp.auth", "true"); Need to request authentication//Enable SSL connection, and more detailed sending steps please see an article: JavaMail-based Java mail send: Simple Mail send//qq mailbox port has two, can Baidu. Final String smtpport = "465";p rops.setproperty ("Mail.smtp.port", Smtpport);p Rops.setproperty (" Mail.smtp.socketFactory.class "," Javax.net.ssl.SSLSocketFactory ");p Rops.setproperty (" Mail.smtp.socketFactory.fallback "," false ");p Rops.setproperty (" Mail.smtp.socketFactory.port ", smtpport);//2. Create a Session object from the configuration to interact with the mail server session session = Session.getinstance (props);//set to debug mode to view detailed send Logsession.setdebug (true) ;//3. Create a message MimeMessage message = Createmimemessage (Session, Myemailaccount, receivemailaccount);//can also be maintained to local view// Message.writeto (File_out_put_stream);//4. Gets the message transfer object according to Session transport TRansport = Session.gettransport ();//5. Use your email account and password to connect to the mail server//The mailbox certified here must match the sender's mailbox in message, otherwise error transport.connect (Myemailaccount, Myemailpassword);//6. To send a message to all of the receiving addresses, message.getallrecipients () gets all the recipients that were added when the message object was created, cc person, bcc transport.sendmessage (message, Message.getallrecipients ());//7. Close connection Transport.close ();} /** Create a mail message (text + picture + attachment) */public static MimeMessage Createmimemessage (Session session, String SendMail, String Receivemail) throws Exception {//1. Create a Mail object mimemessage message = new MimeMessage (session);//2. From: Sender Message.setfrom (new internetaddress (SendMail, "My test Email _ sender nickname", "UTF-8"));//3. To: The recipient (can add multiple recipients, CC, BCC) message.addrecipient (recipienttype.to, New internetaddress (Receivemail, "My test message _ recipient nickname", " UTF-8 "));//4. Subject: Message subject Message.setsubject ("Test message subject (text + picture + attachment)", "UTF-8");/* * The following is the creation of the message content: *///5. Create picture "Node" mimebodypart image = new MimeBodyPart ();D atahandler dh = new DataHandler (New Filedatasource ("d:\\ reference \ \ ad picture \ \ 54af95cfn67aa0c97.jpg ")); Read local file Image.setdatahandler (DH); Add picture data to "node" Image.setcontentid ("Image_fairy_tail"); Set a unique number for node (the ID will be referenced in the text "node")//6. Create text "Node" MimeBodyPart text = new MimeBodyPart ();//The way to add a picture here is to include the entire picture in the message content, or you can actually add the network picture as an HTTP link text.setcontent ("This is A picture <br/> "," text/html;charset=utf-8 ");//7. (text + picture) sets the relationship between the text and the picture "node" (combining text and picture "node" into a mixed "node") Mimemultipart mm_text_image = new Mimemultipart (); mm_text_image.addbody part (text); Mm_text_image.addbodypart (image); Mm_text_image.setsubtype ("related"); Association relationship//8. The mixed "node" of the text + picture is encapsulated into a normal "node"//Content that is eventually added to the message is composed of multiple bodypart Multipart, so what we need is bodypart,//above mm_text_image not BodyPart, all the mm_text_image to be encapsulated into a bodypartmimebodypart text_image = new MimeBodyPart (); Text_image.setcontent (Mm_text_ image);//9. Create the attachment "Node" mimebodypart attachment = new MimeBodyPart ();D atahandler DH2 = new DataHandler (New Filedatasource ("c:\\users\\ Zhang\\desktop\\111.docx ")); Read local file Attachment.setdatahandler (DH2); Add the attachment data to the node Attachment.setfilename (Mimeutility.encodetext (Dh2.getname ())); Set the file name of the attachment (requires encoding)//10. Set (text + picture) and attachment relationship (synthesize a large mixed "node"/Multipart) Mimemultipart mm = new Mimemultipart (); Mm.addbodypart (text_image); Mm.addbodypart (attachment); If you have multiple attachments, you can create multiple add-Mm.setsubtype ("mixed") more than once. Mixed relationship//11. Set the relationship for the entire message (Add the final mixed "node" as the message to the Message object) message.setcontent (mm);//12. Set the Send Time Message.setsentdate (new Date ());//13. Save all settings above Message.savechanges (); return message;}}
JavaMail operation QQ Mailbox send email Demo