JSP and JavaMail 4 (send HTML message)

Source: Internet
Author: User
Tags mail
JS 5. Send HTML-formatted messages

The so-called HTML format, is hypertext format. Your mail can be written in HTML code, sent to each other, the other side will receive the message will be hypertext, hypertext than plain text more beautiful. The following is a modified procedure based on previous examples:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
<%@ page import= "java.util.*,javax.mail.*"%>
<%@ page import= "javax.mail.internet.*"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Send Success </title>

<body>
<%
try{
String Tto=request.getparameter ("to");
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 sender/Recipient/subject/letter time to a Message object
InternetAddress from=new internetaddress ("xxf@cafe.com");
Message.setfrom (from);
InternetAddress to=new internetaddress (tto);
Message.setrecipient (message.recipienttype.to,to);
Message.setsubject (Ttitle);
Message.setsentdate (New Date ());


Set content for a Message object
BodyPart mdp=new MimeBodyPart ()//Create a new BodyPart object that holds the contents of the letter
Mdp.setcontent (tcontent, "text/html;charset=gb2312")//set content and Format/encoding for BodyPart objects
Multipart mm=new Mimemultipart ()//Create a new Mimemultipart object to store BodyPart
Like (in fact, can be stored in multiple)
Mm.addbodypart (MDP)//Add BodyPart to Mimemultipart object (multiple bodypart can be added)
Message.setcontent (mm);//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 ();
%>
<div align= "center" >
<p><font color= "#FF6600" > Sent successfully!</font></p>
<p><a href= "recmail.jsp" > Take a look at my mailbox </a><br>
<br>
<a href= "index.htm" > re-send a letter </a> </p>
</div>
<%
}catch (Messagingexception e) {
Out.println (E.tostring ());
}
%>
</body>

Note: The HTML file for composing the message is still the same as the one in front (please refer to JSP and Java Mail (c)), and there is no need to make any changes.

How about this program is very simple? If there is anything you don't understand, please leave a message below. Next time we will talk about how to send attachments.
adjourned


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.