Using JavaMail in a JSP

Source: Internet
Author: User
Tags date documentation html form include mail requires all mail
Js


Do you want to create a mail delivery collection tool in your JSP? The following is an introduction to the JSP to create a mail delivery collection tool. In this article you can learn some of the key points of the JavaMail API and how to use it in a JSP. Examples of using JavaMail in JSPs are also included in this article. JavaMail is a fairly powerful API in JSP applications.

Reading this article requires a certain preliminary understanding of JSP, JavaBeans and JavaMail. Of course, there is knowledge about JavaMail You can read this article to get. If you know nothing about the above three items, but the server you are using supports JSP and JavaMail, you can use them only by copying/pasting.

What is JavaMail
JavaMail is a sun-released API for handling emails. It makes it easy to perform some common message transfers.

Although JavaMail is one of the Sun's APIs, it has not yet been added to the standard Java Development Toolkit (Java Development Kit), which means that you must download the JavaMail file separately before you use it. In addition, you will need to have Sun's JavaBeans activation Framework (JAF). The operation of the JavaBeans activation framework is complex, and it is simple to say that JavaMail's operation must depend on its support. The path to specify these files is used under Windows 2000 and is similar on other operating systems.

The next thing to explain is the most difficult part of this guide.

This guide includes three parts: HTML tables, a combination of JavaMail, JavaMail, and JSP.



Part I: HTML tables
The first part provides an example of the most basic HTML-based email delivery procedure. The second part tells about the working principle of javamail. The third part introduces the JavaMail to add the JSP, create a basic email send to receive the program.

Dividing components
The most important feature of JSP is the ability to divide the entire Web page into small components. The components used here include:

An HTML form used to send email messages to the JSP;

A JSP page is used to process and send letters.

The first step is to create an HTML table to send information to the JSP page. You can copy the following HTML code to your computer:

HTML source code used to send email
< html>

< body>

< FORM action= "sendmail.jsp" method= "POST" >

< TABLE align= "center" >

< tr>

< TD width= "50%" >

to:< br>< INPUT name= "to" size= ">"

</td>

< TD width= "50%" >

from:< br>< INPUT name= "from" size= ">"

</td>

</tr>

< tr>

< TD colspan= "2" >

subject:< br>< INPUT name= "Subject" size= "M" >

</td>

</tr>

< tr>

< TD colspan= "2" >

< p>message:< br>< TEXTAREA name= "text" rows=25 cols=85></textarea></p>

</td>

</tr>

</table>

< INPUT type= "submit" Name= "Cb_submit" value= "Send" >

< INPUT type= "reset" name= "Cb_reset" value= "clear" >

</form>

</body>


The above program will create a file that contains basic email information such as your Inbox address, send address, subject, and content. You can, of course, decide on your own needs to include that information.

The use of this HTML file has two requirements: The 1th is that the generated file must be sent to the program that will be introduced next. In this case it is sendmail.jsp, but when you use it, you must replace it with the URL of the file in the system, and the 2nd is that there must be room to allow the user to send email.

The second part will analyze the characteristics of javamail and prepare the JSP program in the third part. So next we will learn javamail.
Part II: About JavaMail
Use of documents
The documentation that is available in the downloaded JavaMail API is useful. You can find it in the/docs/javadocs/index.html under the JavaMail. The second part mainly analyzes the components of the mail program. You can read the documentation to get more information on this.

Component to send mail requires the use of javamail, which makes the operation of the message simple and easy.

Property Object
JavaMail need to create a file format of "Mail.smtp.host" to send information.

Properties Props = new properties ();

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

Dialog Object
All JavaMail programs require at least one or all of the conversation goals.

Session sendmailsession;

Sendmailsession = session.getinstance (props, null);

Transmission
The transmission of a message is only sent out or affected by two states. JavaMail describes these two different States as transport and storage. The transmission will send out the mail and the store will receive the mail.

Transport transport;

Transport = Sendmailsession.gettransport ("SMTP");

Using JavaMail can save us a lot of time. JavaMail can replace all SMTP work.

Note: JavaMail does not fully support all mail sending and receiving work. It currently supports only IMAP, SMTP, and POP3, and you can only wait for a new JavaMail version or develop your own protocol.

Information Object
The information object will truly reflect the message you send.

Message Newmessage = new MimeMessage (sendmailsession);

This is all the four objects we need. The next step is how to add the object to the JSP.

  

Part III: The combination of JavaMail and JSP
Create JSP
Here we will begin to combine them. The most important point is to confirm the classification according to the page instructions. Also remember to mark the java.util.date on the mail.

<%@ page

Import= "javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*"

%>

Second, create confirmation messages sent by the message. The confirmation information can be arbitrary and generally used "your email has been sent out (Your Mail has been sent)." "

How information is created and sent
We have discussed the creation of information objects in part two. We will operate on the information below. This is as simple as setting the properties of an information object. You can do this by using the following procedure.

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 message will now begin to be sent. It is very simple to implement it through JavaMail.

Transport.send (Newmessage);

Combine all the components together
Now all the components are complete. Now put them all in the JSP. Pay attention to each error message and feed it back to the user. The code is as follows, and you can use it by copying them directly:

Sample JSP Email Utility Using JavaMail

<%@ page

Import= "javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"

%>

< html>

< head>

< title>jsp meets JavaMail, what a sweet combo.<


< 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>


You will soon realize the convenience of JavaMail, JSP and JavaMail will be the hope of the future.




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.