Use javamail (below) in JSP)

Source: Internet
Author: User

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.