Do you want to create a mail to send a collection tool in JSP? The following will be introduced to create a mail sending a collection tool in JSP. In this article you can learn about some of the JavaMail API and how to use it in JSP. This article also includes an example of using JavaMail in JSP. JavaMail is a quite powerful API in the JSP application.
Read this article needs to have a certain initial understanding of JSP, JavaBeans and Javamail. Of course, knowledge about JavaMail you can get through this article. If you don't know much about the above three, the server you use supports JSP and JavaMail, you can use them only by copying / paste.
What is JavaMail
JavaMail is an API for the SUN release to handle Email. It can easily perform some commonly used mail transmission.
Although JavaMail is one of Sun's API, it has not yet been added to the standard Java Development Kit, which means you need to download another JavaMail file before use. In addition, you also need Sun's JavaBeans Activation Framework (JAF). The operation of JavaBeans Activation Framework is very complicated. It is simple to say that JavaMail's operation must depend on its support. Use the path to specify these files in Windows 2000, similar to other operating systems.
The next thing to explain is the most difficult part of this guide.
This guide includes three parts: HTML form, combined with JavaMail, Javamail, and JSP.
Part 1: HTML form
The first part provides an example of the most basic HTML-based Email sending charger. The second part tells the working principle of JavaMail. The third part introduces JavaMail to join JSP, create a basic Email sending charger.
Partitioning components
The most important feature of JSP is to divide the entire web page into some small components. The components used here include:
● One HTML table used to send Email's 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 copy the following HTML code to your computer:
HTML source code used to send email
The above program will create a file that contains Email basic information (for example: receiving addresses, sending addresses, topics, and content). Of course, you can decide the file in accordance with your own needs.
This HTML file has two points required: The first point is that the generated file must be sent to the next program that will be introduced. In this example, it is sendmail.jsp, but when you use, this file must be used in the URL in the system instead of it; the second point is that there must be space to allow the user to send email.
The second part will analyze the characteristics of JavaMail, preparing for the JSP program in the third part. So let's learn JavaMail.
Part II: About JavaMail
Documentation
The documentation with the downloaded JavaMail API is useful. You can find it on /Docs/javadocs/index.html under JavaMail. The second part mainly analyzes the components of the mail program. You can get more information from reading documents.
Components Send Mail Need to use Javamail, which makes it easy to use the operation of the message.
Attribute object
JavaMail needs to create a file that is "mail.smtp.host" used to send information.
Properties PROPS = New Properties ();
Props.Put ("mail.smtp.host", "smtp.jspinsider.com"); dialogue object
All JavaMail-based programs require at least one or all of the dialogue targets. Session SendmailSession;
SendmailSession = session.getInstance (PROPS, NULL);
transmission
The transmission of the mail is only sent or subject to two states. JavaMail describes both different states as transmission and storage. Transfer will send a message and save the message.
Transport Transport;
Transport = sendmailsession.getTransport ("SMTP");
Use JavaMail to save us a lot of time. JavaMail can replace all SMTP work.
Note: JavaMail does not fully support all email to send charges. It currently only supports IMAP, SMTP, and POP3, in addition to this, you only wait for new JavaMail versions or its own development protocols.
Information object
The information object will reflect the email you sent true.
Message newMessage = New MimeMailSession;
This is all the four objects we need. The next step will be how to add objects to JSP.
Part III: Javamail and JSP combination
Create JSP
Below we will begin to combine them together. The most important point is to confirm that the classification is based on the page. Also remember to mark java.util.date on the email.
<% @ PAGE
Import = "javax.mail. *, javax.mail.internet. *, javax.activation. *, java.util. *"
%>
Second, create a confirmation information sent by the message. Confirm that the information can be arbitrary, generally commonly used "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 follow the information below. This is as simple as the properties of the setting information object. You can implement this operation by the following program.
NEWMESSAGE.SETFROM (New InternetDress (Request.GetParameter ("from")))))
NEWMESSAGE.SETRECIPIPIPIPient (Message.RecipientType.to, New InternetDress (Request.GetParameter ("to")));
NEWMESSAGE.SETSUBJECT (Request.GetParameter ("Subject"));
NEWMESSAGE.SETSENTDATE (New Date ());
NEWMESSAGE.SETTEXT (Request.GetParameter ("text"));
The information will now be sent. It is very simple to achieve through JavaMail.
Transport.send (NewMessage);
Combine all components together
All components are now complete. Now put them in JSP. Pay attention to each error message and feed back it to the user. The code is as follows, you can use them directly:
Sample JSP Email Utility Using JavaMail
<% @ PAGE
Import = "javax.mail. *, javax.mail.internet. *, javax.activation. *, java.util. *"
%>
<%
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 MimeMailSession;
NEWMESSAGE.SETFROM (New InternetDress (Request.GetParameter ("from")))))
NEWMESSAGE.SETRECIPIPIPIPient (Message.RecipientType.to, New InternetDress (Request.GetParameter ("to")));
NEWMESSAGE.SETSUBJECT (Request.GetParameter ("Subject"));
NEWMESSAGE.SETSENTDATE (New Date ());
NEWMESSAGE.SETTEXT (Request.GetParameter ("text"));
Transport = sendmailsession.getTransport ("SMTP");
Transport.send (NewMessage);
%>
Your Mail Has Been Sent.
<%
}
Catch (MessagingException M)
{
Out.println (m.toString ());
}
%>
You will quickly experience the convenience of JavaMail, JSP and JavaMail will be the future hope.