Java Send message instance code with HTML tag content _java

Source: Internet
Author: User
Tags stringbuffer

The

looks like this:

Package test;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
Import javax.mail.Session;
Import javax.mail.MessagingException;

Import Javax.mail.Transport; public class Sendhtmlmail {public static void SendMessage (String smtphost, string from, String to,string subject, Strin G MessageText) throws Messagingexception, java.io.UnsupportedEncodingException {//step 1:configure The Mail
    Session System.out.println ("Configuring Mail Sessions for:" + smtphost);
    Java.util.Properties props = new Java.util.Properties (); Props.setproperty ("Mail.smtp.auth", "true");//Specify whether SMTP authentication Props.setproperty ("Mail.smtp.host", smtphost) is required;
    Specifies the SMTP server props.put ("Mail.transport.protocol", "SMTP");
    Session mailsession = session.getdefaultinstance (props); Mailsession.setdebug (FALSE)//Whether debug information is displayed on the console//step 2:construct the message System.out.println ("Constructing Message-from= "+From + "to=" + to);
    InternetAddress fromaddress = new internetaddress (from);

    InternetAddress toaddress = new internetaddress (to);
    MimeMessage testMessage = new MimeMessage (mailsession);
    Testmessage.setfrom (fromaddress);
    Testmessage.addrecipient (Javax.mail.Message.RecipientType.TO, toaddress);
    Testmessage.setsentdate (New Java.util.Date ());

    Testmessage.setsubject (Mimeutility.encodetext (Subject, "gb2312", "B"));
    Testmessage.setcontent (MessageText, "text/html;charset=gb2312");

    SYSTEM.OUT.PRINTLN ("message constructed");
    Step 3:now Send the message transport transport = Mailsession.gettransport ("SMTP");
    Transport.connect (SMTPHost, "Riteng_mes", "ri-teng1234");
    Transport.sendmessage (TestMessage, testmessage.getallrecipients ());

    Transport.close ();
  SYSTEM.OUT.PRINTLN ("message sent!");
    public static void Main (string[] args) {String smtphost = "10.131.119.36";
    String from = "Riteng_Mes@casetekcorp.com"; String to = "Qiang1_Zhang@intra.casetekcorp.com"; String subject = "HTML mail test";
    Subject JavaMail Automatic transcoding stringbuffer themessage = new StringBuffer ();
    Themessage.append (" 

The above is a small series of Java to send you with HTML tag content of the mail instance code all content, I hope that we support cloud-Habitat Community ~

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.