5. Send an email in the HTML format
The so-called HTML format is a hypertext format. Your email can be written in HTML code. After sending it to the other party, the other party will be information will be a hypertext, and the hypertext is more than the pure text. The program is modified on the basis of the example:
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% Request.SetCharacterencoding ("GB2312");%>
<% @ Page Import = "java.util. *, javax.mail. *"%>
<% @ Page Import = "javax.mail.internet. *"%>
hEAD>
<%
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 the sender / recipient / topic / send time to the 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 the content to the message object
Bodypart MDP = new mimebodypart (); // Newly built a Bodypart object that stores letters content
Mdp.setContent (tcontent, "text / html; charset = GB2312"); // Set content and format / encoding method for Bodypart objects
Multipart mm = new mimemultipart (); // Newly built a MIMEMULTIPART object to store Bodypart pairs
// (in fact, multiple)
mm.addbodypart (MDP); // Add BODYPAR to MIMEMULTIPART objects (you can join multiple Bodypart)
Message.setContent (mm); // Put the 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 ();
%>
Send success! font> p>
div>
<%
} catch (messagingException e) {
Out.println (E.TOString ());
}
%>
body>
html>
Note: The HTML file written is still in front (please refer to JSP and Java Mail (3)), do not need to make any modifications.
How is this program? If there is anything you don't understand, please leave a message below. Next time we will talk about how to send an attachment.
(to be continued)