Java-based email sending

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: [java] 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 sending test class/p> *** <p> Description: example business class </p> ** <P> Copyright: Copyright (c) 2012 </p> ***** @ author dml @ 2012-12-17 * @ version 1.0 *//******************** **************************************** * ***** use QQ mail (for other similar use, you should also change it) 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 headName * 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 www.2cto.com transport. sendMessage (message, message. getAllRecipients (); transport. close (); System. out. Println ("send success! ");} Catch (AddressException e) {// TODO Auto-generated catch block e. 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 ") ;}} only one jar package is required, mail. before executing the program, make sure that SMTP is enabled in the mailbox. In this example, we use QQ mail for testing, set the path QQ Mail Settings-> Account-> POP3/IMAP/SMTP Service to select Enable POP3/SMTP services p.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.