JSP Insider 5: Use JavaMail---4 in JSP

Source: Internet
Author: User
Tags date mail
JS Part Three: Use JavaMail in JSP

Constructing a JSP
Now it's important to show you how to put these objects together and you need to import them with the page designator. Note: You also need to use the java.util.date, because the time stamp of the email to use the java.util.date.

<%@ page
Import= "javax.mail.*, javax.mail.internet.*,
=>javax.activation.*, java.util.* "
%>
The next step is to create a specific message that indicates that the message has been sent. You can set this information yourself, usually: "Your Mail has been sent." ”

How to create and send messages

The code that creates the message object already appears in the second part. Now we can process the message. Simply use the Get and set properties method in the object. Using the post information as a parameter, such as Request.getparameter (), is a useful way to get the parameters that the post sends. See listing B.
Listing B:

Newmessage.setfrom (New InternetAddress (Request.getparameter ("from"));
Newmessage.setrecipient (Message.RecipientType.TO, new
InternetAddress (Request.getparameter ("to"));
Newmessage.setsubject (Request.getparameter ("subject"));
Newmessage.setsentdate (New Date ());
Newmessage.settext (Request.getparameter ("text"));


The last thing to do is send a message, and the JavaMail tool makes it simple:

Transport.send (Newmessage);
Put ' em together.

We've just introduced a separate component, and now we can put them together and put them in a JSP script. Note that you want to add a script statement that catches the error and displays it to the user. JSP script See listing C. To use the script, just copy the script to your computer.
Listing C:

<%@ page
Import= "javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<title>jsp meets JavaMail, what a sweet combo.</title>
</HEAD>
<BODY>
<%

try{
Properties Props = new properties ();
Session sendmailsession;
Store store;
Transport transport;


Sendmailsession = session.getinstance (props, null);

Props.put ("Mail.smtp.host", "smtp.jspinsider.com");

Message Newmessage = new MimeMessage (sendmailsession);
Newmessage.setfrom (New InternetAddress (Request.getparameter ("from"));
Newmessage.setrecipient (Message.RecipientType.TO), New InternetAddress (Request.getparameter ("to"));
Newmessage.setsubject (Request.getparameter ("subject"));
Newmessage.setsentdate (New Date ());
Newmessage.settext (Request.getparameter ("text"));

Transport = Sendmailsession.gettransport ("SMTP");
Transport.send (Newmessage);
%>
<p>your Mail has been sent.</p>
<%
}
catch (messagingexception m)
{
Out.println (M.tostring ());
}
%>
</BODY>


It's all right! If you intend to understand the step-by-step action of JavaMail, use ' Sendmailsession.setdebug ' (true).

Now you've got a primer on JSP and JavaMail usage. For more information, see Sun ' s JavaMail links.

How to do better?

The most important thing is that the tool can be run. But we can improve it. The following tutorial will introduce:

Why don't you put the form in the JSP?
Client-side validation
How to use beans instead of messy, reusable code.
The function and design are separated by JSP.


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.