Document directory
- [Introduction]
- [Spring mail API]
[Introduction]
It is easy to use the Java mail API to send emails. However, recently, a mail API encapsulated by a colleague in the company has made me unable to accept it, so I planned to use the spring mail API to send emails, record this article by the way.
[Spring mail API]
Spring mail APIs are encapsulated in org. springframework. Mail and its sub-package org. springframework. Mail. javamail, and only the mail sending encapsulation is provided.
Simplemailmessage: A simple encapsulation of an email. It can only be used to indicate a plain text email or contain attachments.
Javamailsenderimpl: A Mail sender. It mainly provides the mail sending interface, transparent creation of Java mail mimemessage, and mail sending configuration (such as host/port/username/password ...).
Mimemailmessage and mimemessagehelper: encapsulate mimemessage. Spring also provides a callback interface mimemessagepreparator to prepare mime letters of javamail.
The Code Reprinted from: http://www.blogjava.net/tangzurui/archive/2008/12/08/244953.html
1. Send simple text emails
Package net. xftzr. mail; import Java. util. properties; import Org. springframework. mail. simplemailmessage; import Org. springframework. mail. javamail. javamailsenderimpl;/*** in this test, a simple email is sent directly by email ** @ author administrator **/public class singlemailsend {public static void main (string ARGs []) {javamailsenderimpl senderimpl = new javamailsenderimpl (); // set mail serversenderimpl. sethost ("smtp.163.com"); // Create mail message s Implemailmessage mailmessage = new simplemailmessage (); // set the recipient, the sender sends multiple emails with an array // string [] array = new string [] {"sun111@163.com ", "sun222@sohu.com"}; // mailmessage. setto (array); mailmessage. setto ("toEmail@sina.com"); mailmessage. setfrom ("userName@163.com"); mailmessage. setsubject ("test sending a simple text email! "); Mailmessage. settext (" test my simple mail sending mechanism !! "); Senderimpl. setusername ("username"); // set usernamesenderimpl based on your own situation. setpassword ("password"); // set passwordproperties prop = new properties (); prop. put ("mail. SMTP. auth "," true "); // set this parameter to true to allow the server to authenticate and verify that the user name and password are correct. put ("mail. SMTP. timeout "," 25000 "); senderimpl. setjavamailproperties (PROP); // send the mail senderimpl. send (mailmessage); system. out. println ("the email is sent successfully .. ");}}
2. Send simple HTML emails
Org. springframework. Mail. javamail. mimemessagehelper is one of the commonly used tools to process javamail emails. It can free you from complicated javax. Mail. internetapi classes.
Package net. xftzr. mail; import Java. util. properties; import javax. mail. internet. mimemessage; import Org. springframework. mail. javamail. javamailsenderimpl; import Org. springframework. mail. javamail. mimemessagehelper; /*** this class tests the HTML mail ** @ author sunny **/public class htmlmaildemo {/*** @ Param ARGs */public static void main (string [] ARGs) throws exception {javamailsenderimpl senderimpl = new javamailsenderi Mpl (); // set mail serversenderimpl. sethost ("smtp.163.com"); // create a mail message. Differences between sending a Simple Mail and HTML mail: mimemessage mailmessage = senderimpl. createmimemessage (); mimemessagehelper messagehelper = new mimemessagehelper (mailmessage); // you can specify the recipient and the sender messagehelper. setto ("Mailto@sina.com"); messagehelper. setfrom ("username@163.com"); messagehelper. setsubject ("test HTML mail! "); // True indicates to start an HTML-format email messagehelper. settext (" <HTML>
3. Send emails with nested Images
You can add attachments to an email or embed resources in a multipart email. Embedded resources may be the images or style sheets you want to use in a letter, but you do not want to use them as attachments.
Package net. xftzr. mail; import Java. io. file; import Java. util. properties; import javax. mail. internet. mimemessage; import Org. springframework. core. io. filesystemresource; import Org. springframework. mail. javamail. javamailsenderimpl; import Org. springframework. mail. javamail. mimemessagehelper;/*** nested images in this test email ** @ author sunny **/public class attachedimagemail {public static void main (string [] ARGs) throws E Xception {javamailsenderimpl senderimpl = new javamailsenderimpl (); // set mail serversenderimpl. sethost ("smtp.163.com"); // create a mail message. Differences between sending a Simple Mail and HTML mail: mimemessage mailmessage = senderimpl. createmimemessage (); // note that the Boolean here is equivalent to embedding an image only when it is true. When constructing mimemessagehelper, the given value is true, indicating that the image is enabled, // multipart mode: mimemessagehelper messagehelper = new mimemessagehelper (mailmessage, true); // you can specify the recipient and the sender messagehelper. setto ("tomail @ Sina. Com "); messagehelper. setfrom (" username@163.com "); messagehelper. setsubject (" test mail nested pictures !! "); // True indicates to start an HTML-format email messagehelper. settext (" <HTML>
Note: The embedded image , wherein CID: is a fixed writing method, and AAA is a contentid.
4. Send emails containing attachments
Package net. xftzr. mail; import Java. io. file; import Java. util. properties; import javax. mail. internet. mimemessage; import Org. springframework. core. io. filesystemresource; import Org. springframework. mail. javamail. javamailsenderimpl; import Org. springframework. mail. javamail. mimemessagehelper; public class attachedfilemail {/*** this class tests the example with attachments in the email ** @ Param ARGs */public static void main (string [] ARGs) throws Exception {javamailsenderimpl senderimpl = new javamailsenderimpl (); // set mail serversenderimpl. sethost ("smtp.163.com"); // create a mail message. Differences between sending a Simple Mail and HTML mail: mimemessage mailmessage = senderimpl. createmimemessage (); // note that the Boolean here is equivalent to embedding an image only when it is true. When constructing mimemessagehelper, the given value is true, indicating that the image is enabled, // when the multipart mode is true, attachments can be sent in HTML format mimemessagehelper messagehelper = new mimemessagehelper (mailmessage, true, "UTF-8"); // set the recipient and sender Messagehelper. setto ("toMail@sina.com"); messagehelper. setfrom ("username@163.com"); messagehelper. setsubject ("Upload attachments in test mail !! "); // True indicates to start an HTML-format email messagehelper. settext (" <HTML>