Java Mail receipt of HTML mail program

Source: Internet
Author: User
Tags date documentation html form include mail requires all mail
Program in JSP using JavaMail (top) <br>
<br>
<br>
Content: <br>
<br>
<br>
What is JavaMail <br>
Part I: HTML tables <br>
Dividing components <br>
HTML source code to send email <br>
<br>
<br>
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. <br>
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. <br>
<br>
What is JavaMail <br>
<br>
JavaMail is a sun-released API for handling emails. It makes it easy to perform some common message transfers. <br>
<br>
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. <br>
<br>
The next thing to explain is the most difficult part of this guide. <br>
<br>
This guide includes three parts: HTML tables, a combination of JavaMail, JavaMail, and JSP. <br>
<br>
Part I: HTML tables <br>
<br>
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. <br>
<br>
Dividing components <br>
<br>
The most important feature of JSP is the ability to divide the entire Web page into small components. The components used here include: <br>
<br>
An HTML form used to send email messages to the JSP; <br>
<br>
A JSP page is used to process and send letters. <br>
<br>
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: <br>
<br>
HTML source code to send email <br>
<br>
<HTML><br>
<BODY><br>
<form action= "sendmail.jsp" method= "POST" ><br>
<table align= "center" ><br>
<TR><br>
&LT;TD width= "50%" ><br>
To:<br><input name= "to" size= "><br>"
</TD><br>
&LT;TD width= "50%" ><br>
From:<br><input name= "from" size= "><br>"
</TD><br>
</TR><br>
<TR><br>
&LT;TD colspan= "2" ><br>
Subject:<br><input name= "Subject" size= "M" ><br>
</TD><br>
</TR><br>
<TR><br>
&LT;TD colspan= "2" ><br>
<p>message:<br><textarea name= "text" rows=25 cols=85></textarea></p><br>
</TD><br>
</TR><br>
</TABLE><br>
<input type= "Submit" Name= "Cb_submit" value= "Send" ><br>
<input type= "reset" name= "Cb_reset" value= "clear" ><br>
</FORM><br>
</BODY><br>
</HTML><br>
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. <br>
<br>
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. <br>
<br>
The second part will analyze the characteristics of javamail and prepare the JSP program in the third part. So next we're going to learn javamail<br>
<br>
<br>
Use JavaMail (bottom) in JSP <br>
<br>
<br>
<br>
<br>
<br>
Content: <br>
<br>
<br>
Part II: About JavaMail <br>
Part III: Combination of JavaMail and JSP <br>
<br>
<br>
Part II: About JavaMail <br>
<br>
Use of documents <br>
<br>
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. <br>
<br>
Component to send mail requires the use of javamail, which makes the operation of the message simple and easy. <br>
<br>
Property Objects <br>
<br>
JavaMail need to create a file format of "Mail.smtp.host" to send information. <br>
<br>
Properties Props = new properties (); <br>
<br>
Props.put ("Mail.smtp.host", "smtp.jspinsider.com"); Dialog Objects <br>
<br>
All JavaMail programs require at least one or all of the conversation goals. <br>
<br>
Session sendmailsession; <br>
<br>
Sendmailsession = session.getinstance (props, null); <br>
<br>
Transmission <br>
<br>
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. <br>
<br>
Transport transport; <br>
<br>
Transport = Sendmailsession.gettransport ("SMTP"); <br>
<br>
Using JavaMail can save us a lot of time. JavaMail can replace all SMTP work. <br>
<br>
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. <br>
<br>
Information Objects <br>
<br>
The information object will truly reflect the message you send. <br>
<br>
Message Newmessage = new MimeMessage (sendmailsession); <br>
<br>
This is all the four objects we need. The next step is how to add the object to the JSP. <br>
<br>
Part III: Combination of JavaMail and JSP <br>
<br>
Create JSP <br>
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. <br>
<br>
<%@ page<br>
Import= "javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*" <br>
%><br>
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)." "<br>
<br>
How information is created and sent <br>
<br>
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. <br>
<br>
Newmessage.setfrom (New InternetAddress (Request.getparameter ("from")); <br>
<br>
Newmessage.setrecipient (Message.RecipientType.TO), New InternetAddress (Request.getparameter ("to")); <br>
<br>
Newmessage.setsubject (Request.getparameter ("subject")); <br>
<br>
Newmessage.setsentdate (New Date ()); <br>
<br>
Newmessage.settext (Request.getparameter ("text")); <br>
<br>
The message will now begin to be sent. It is very simple to implement it through JavaMail. <br>
<br>
Transport.send (Newmessage); <br>
<br>
Combine all the components together <br>
<br>
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, you can use:<br> by copying them directly
<br>
Sample JSP Email Utility Using JavaMail <br>
<%@ page<br>
Import= "javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" <br>
%><br>
<title>jsp meets JavaMail, what a sweet combo.</title><br>
</HEAD><br>
<BODY><br>
<%<br>
Try{<br>
Properties Props = new properties ();<br>
Session sendmailsession;<br>
Store store;<br>
Transport transport;<br>
Sendmailsession = session.getinstance (props, null);<br>
Props.put ("Mail.smtp.host", "smtp.jspinsider.com");<br>
Message Newmessage = new MimeMessage (sendmailsession);<br>
Newmessage.setfrom (New InternetAddress (Request.getparameter ("from"));<br>
Newmessage.setrecipient (Message.RecipientType.TO, New InternetAddress (Request.getparameter ("to"));<br>
Newmessage.setsubject (Request.getparameter ("subject"));<br>
Newmessage.setsentdate (New Date ());<br>
Newmessage.settext (Request.getparameter ("text"));<br>
Transport = Sendmailsession.gettransport ("SMTP");<br>
Transport.send (newmessage);<br>
%><br>
<p>your Mail has been sent.</p><br>
<%<br>
}<br>
catch (messagingexception m) <br>
{<br>
Out.println (M.tostring ());<br>
}<br>
%><br>
</BODY><br>
</HTML><br>
<br>


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.