Using JavaMail in a JSP (bottom)

Source: Internet
Author: User
Tags date documentation mail all mail
JS 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.*"

%>



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

</HTML>

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

< finished full >


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.