[Java] detailed explanation of source code Java sends an email

Source: Internet
Author: User

It seems that the weather in Xi'an is difficult to get better, and the continuous moderate pollution is suffocating. No nonsense. This morning I wrote a Java mail sending service class. Let's look at the source code:

Package Dec; import Java. util. properties; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. addressexception; import javax. mail. internet. internetaddress; import javax. mail. internet. mimemessage;/***** <p> title: Java mail test class/P> *** <p> Description: example business class </P> *** <p> copyright: copyright (c) 2012 </P> *** @ aut Hor DML @ 2012-12-17 * @ version 1.0 *//***************************** (you should have changed the usage of others) first, open POP3 SMTP In the QQ mailbox. First, make sure that your network is normal and not a proxy. * [Make Sure That SMTP is enabled in your QQ mailbox ]!!!! If not, in this case, set-> account-in the following-select [√] SMTP mail and save it to the server **************** **************************************** * ******/public class javamail {// set the server Private Static string key_smtp = "mail. SMTP. host "; Private Static string value_smtp =" smtp.qq.com "; // server verification Private Static string key_props =" mail. SMTP. auth "; Private Static Boolean value_props = true; // sender's username and password private string send_user =" ******** @ qq. Com "; private string send_uname =" ********* "; private string send_pwd =" ******** "; // create a session private mimemessage message; private session s;/** Initialization Method */Public javamail () {properties props = system. getproperties (); props. setproperty (key_smtp, value_smtp); props. put (key_props, value_props); s = session. getinstance (props);/* s. setdebug (true); debugging information after enabling */message = new mimemessage (s);}/*** send email ** @ Param head Name * email header file name * @ Param sendhtml * Email content * @ Param receiveuser * Recipient address */Public void dosendhtmlemail (string headname, string sendhtml, string receiveuser) {try {// sender internetaddress from = new internetaddress (send_user); message. setfrom (from); // recipient internetaddress to = new internetaddress (receiveuser); message. setrecipient (message. recipienttype. to, to); // mail title message. setsubject (headname); string content = Sendhtml. tostring (); // The Mail content. You can also make the plain text "text/plain" message. setcontent (content, "text/html; charset = GBK"); message. savechanges (); transport Transport = S. gettransport ("SMTP"); // SMTP authentication, that is, the user name and password transport you use to send emails. connect (value_smtp, send_uname, send_pwd); // send transport. sendmessage (message, message. getallrecipients (); transport. close (); system. out. println ("Send success! ");} Catch (addressexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (messagingexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {javamail Se = new javamail (); se. dosendhtmlemail ("mail header file name", "Mail content ","*******@***. com ");}}

There is only one jar package, mail. Jar

Note that before executing the program, make sure that SMTP is enabled in the mailbox. In this example, use QQ mailbox for testing and set the path.

Qq mail settings-> account-> POP3/IMAP/SMTP service selected Enable POP3/SMTP servicesP.s. there is a cloud in the old saying: "linyuanyu is better to get rid of the Internet, all resources occupied by anyone are obtained. Dml@2012.12.17

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.