Use javamail in JSP

Source: Internet
Author: User


Do you want to create an email sending and receiving tool in JSP? The following describes how to create an email sending and receiving tool in JSP. In this article Article You can understand some key points of the javamail API and how to use it in JSP. This article also includes examples of using javamail in JSP. Javamail is a powerful API in JSP applications.
Reading this article requires a preliminary understanding of JSP, JavaBeans, and javamail. Of course, you can read this article to learn about javamail. If you do not know anything about the above three items, but the servers you use support JSP and javamail, you can simply use them by copying/pasting them.
What is javamail?
Javamail is an API released by Sun to process emails. It can easily perform some common mail Transmission.
Although javamail is one of Sun's APIs, it has not yet been added to the standard Java Development Kit (Java Development Kit), which means you must download the javamail file before use. In addition, you also need sun's JavaBeans activation framework (JAF ). The execution of the javamail activation framework is very complex. In this case, javamail must depend on its support. In Windows 2000, you must specify the path of these files, which is similar to that in other operating systems.
Next I will explain the most difficult part of this Guide.
This guide consists of three parts: HTML tables, javamail, javamail, and JSP.
Part 1: HTML table
The first part provides a basic HTML-based email sending and receiving Program . The second part describes how javamail works. The third part introduces how to add javamail to JSP and create a basic email sending and receiving program.
Division Components
The most important feature of JSP is that it can divide the entire web page into small components. The components used here include:
● An HTML table used to send email information to 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 set the following html Code Copy to your computer:
HTML used to send emails Source code
<HTML>
<Body>
<Form action = "sendmail. jsp" method = "Post">
<Table align = "center">
<Tr>
<TD width = "50%"> to: <br> <input name = "to" size = "25"> </TD>
<TD width = "50%"> from: <br> <input name = "from" size = "25"> </TD>
</Tr>
<Tr>
<TD colspan = "2"> subject: <br> <input name = "subject" size = "50"> </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>
</Html>
The above process creates a file containing the basic email information (such as the receiving address, sending Address, subject, and content. Of course, you can decide the information contained in this file based on your own needs.
There are two requirements for the use of this HTML file: The first point is that the generated file must be sent to the program that will be introduced later. In this example, sendmail. jsp is used, but you must use the File URL in the system to replace it. Second, you must have space to allow users to send emails.
The second part analyzes the features of javamail to prepare for creating JSP programs in the third part. So next we will learn about javamail.
Part 2: About javamail
Document usage
The documentation in the downloaded javamail API is very useful. You can find it in/docs/javadocs/index.html under javamail. The second part mainly analyzes the mail program components. You can read the document for more information.
Javamail is required for the component to send emails, which makes operations on emails easy to use.
Attribute object
Javamail needs to create a file in the format of "mail. SMTP. Host" to send information.
Properties props = new properties ();
Props. Put ("mail. SMTP. Host", "smtp.jspinsider.com ");
Dialog object
All javamail-based programs require at least one or all of the conversation objectives.
Session sendmailsession;
Sendmailsession = session. getinstance (props, null );
Transmission
The mail can only be sent or received in two statuses. Javamail describes these two different States as transmission and storage. The transfer will send the mail, and the storage will receive the mail.
Transport transport;
Transport = sendmailsession. gettransport ("SMTP ");
Using javamail can save us a lot of time. Javamail can replace all SMTP tasks.
Note: javamail does not fully support sending and receiving all emails. Currently, it only supports IMAP, SMTP, and POP3. In addition, you only need to wait for the new javamail version or develop the protocol yourself.
Information object
The information object will actually reflect the email you sent.
Message newmessage = new mimemessage (sendmailsession );
This is all four objects we need. The next step is to add the object to JSP.
Part 3: Integration of javamail and JSP
Create JSP
Next we will begin to combine them. The most important thing is to confirm the classification according to the instructions on the page. Remember to mark java. util. date on the email.
<% @ Page
Import = "javax. Mail. *, javax. Mail. Internet. *, javax. Activation. *, java. util .*"
%>
Next, create a confirmation message for sending the email. The confirmation information can be arbitrary. It is 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 the second part. We will perform operations on the information below. This is as simple as setting the attributes of information objects. You can use the following program to perform this operation.
Newmessage. setfrom (New internetaddress (request. getparameter ("from ")));
Newmessage. setrecipient (message. recipienttype. To, new internetaddress (request. getparameter ("")));
Newmessage. setsubject (request. getparameter ("subject "));
Newmessage. setsentdate (new date ());
Newmessage. settext (request. getparameter ("text "));
The message will be sent now. It is very easy to implement through javamail.
Transport. Send (newmessage );
Combine all components
Now all the components are complete. Now they are all placed in JSP. Pay attention to every error message and send it back to the user. The Code is as follows. You can copy them and use 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. </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 ("")));
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>
</Html>
You will soon learn about 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.