JSP and JavaMail 4 (send HTML emails)

Source: Internet
Author: User
5. Send HTML emails
HTML format is hypertext format. your email can be written in HTML code. After being sent to the recipient, the recipient will receive hypertext, which is much better than plain text. the following is a program modified based on the previous example:
<% @ Page contentType = "text/html; charset = GB2312" %>
<% Request. setCharacterEncoding ("gb2312"); %>
<% @ Page import = "java. util. *, javax. mail. *" %>
<% @ Page import = "javax. mail. internet. *" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Sent successfully </title>
</Head>
<Body>
<%
Try {
String parameter = request. getParameter ("");
String ttitle = request. getParameter ("title ");
String tcontent = request. getParameter ("content ");
Properties props = new Properties ();
Props. put ("mail. smtp. host", "127.0.0.1 ");
Props. put ("mail. smtp. auth", "true ");
Session s = Session. getInstance (props );
S. setDebug (true );
MimeMessage message = new MimeMessage (s );
// Set the sender/recipient/topic/sending time for the message object
InternetAddress from = new InternetAddress ("xxf@cafe.com ");
Message. setFrom (from );
InternetAddress to = new InternetAddress (region );
Message. setRecipient (Message. RecipientType. TO, );
Message. setSubject (ttitle );
Message. setSentDate (new Date ());
// Set content for the message object
BodyPart mdp = new MimeBodyPart (); // creates a BodyPart object that stores the content of a letter.
Mdp. setContent (tcontent, "text/html; charset = gb2312"); // set the content and format/encoding method for the BodyPart object
Multipart mm = new MimeMultipart (); // create a new MimeMultipart object to store the BodyPart
// Image (in fact, multiple objects can be stored)
Mm. addBodyPart (mdp); // add the BodyPart to the MimeMultipart object (multiple bodyparts can be added)
Message. setContent (mm); // use mm as the content of the message object.
Message. saveChanges ();
Transport transport = s. getTransport ("smtp ");
Transport. connect ("127.0.0.1", "xxf", "coffee ");
Transport. sendMessage (message, message. getAllRecipients ());
Transport. close ();
%>
Related Article

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.