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
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 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 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. *" %>
Your mail Has Been SENT. p> <%} catch (messagingException m) {outputln (m.toString ());}%> body> h TML>
You will quickly experience the convenience of JavaMail, JSP and JavaMail will be the future hope.