JSP and Java Mail API

Source: Internet
Author: User

Summary: The development of Java Mail API is a good example of SUN's continuous efforts to provide a public API framework for Java developers. A public framework is promoted to oppose solutions restricted by suppliers, which fully predicts the establishment of an increasingly open development environment.
The structure of the Java Mail API proves that the workload of software development depends on the complexity of the application and the degree of control required by the developer. In other words, the Java Mail API is as simple as possible. At first glance, it may take a long time to learn the total number of classes in the Java Mail API and the relationship between classes. In fact, once you start using the API, you will find that this API is a simple tool to add robust email/communication support to the application.

What software is required to create a JavaMail environment?

First, install the JavaMail API. Currently, there are two common JavaMail API versions: 1.2 and 1.1.3. Although version 1.2 is the latest version, Version 1.1.3 includes version 1.2.1 of Java 2 Platform Enterprise Edition (Java 2 Platform, Enterprise Edition, J2EE.
· JavaMail 1.2 Installation
To use the JavaMail 1.2 API, download the JavaMail 1.2 file, open the javamail-_2.zip file, and add the mail. jar file to CLASSPATH. In addition to the core class, the SMTP, IMAP4, and POP3 vendors are provided along with the implementation of version 1.2.
· JavaMail 1.1.3 Installation
To use the JavaMail 1.1.3 API, download JavaMail 1.1.3, open the javamailpolic3.3.zip file, and add the mail. jar file to your CLASSPATH. In addition to the core class, the SMTP and IMAP4 vendors are provided along with Version 1.1.3.
If you use JavaMail 1.1.3 to access a POP server, download and install a POP3 vendor. Sun has an implementation independent of JavaMail. Download and unbind the pop31_00001.zip file, and add pop3.jar to your CLASSPATH.
The second step is the installation of the activans Activation Framework. All versions of JavaMail API require JavaBeans Activation Framework to support the input and corresponding processing of any data block. There seems to be few features, but currently many browsers and mail tools can find this basic MIME-type support. After the framework is downloaded, unbind the jaf1_0_1.zip file and add the activation. jar file to CLASSPATH.
For JavaMail 1.2 users, You should have added the mail. jar and activation. jar files to CLASSPATH.
For JavaMail 1.1.3 users, You should have added the mail. jar, pop3.jar, and activation. jar files to CLASSPATH. If you do not want to use POP3, you will not add pop3.jar to CLASSPATH.
If you do not want to change the CLASSPATH Environment variable, copy the jar file to the lib/ext directory under the Java Runtime Environment (Java Runtime Environment, JRE) directory. For example, the default directory of J2SE 1.3 release is in C: jdk1.3jrelibext of Windows.

What are the core classes of Java Mail APIs?

· Javax. mail. Session: The Session class defines a basic Mail session, which is the highest entry class of the Java mail API. All other classes take effect only through this session. The Session object uses the Java. util. Properties object to obtain information, such as the email server, user name, password, and other information shared throughout the application.
· Javax. mail. Message: Once the Session object is obtained, you can continue to create the Message to be sent. This is done by the Message class. Message is an abstract class and must be a subclass. In most cases, it is Javax. mail. internet. MimeMessage. MimeMessage is an email message that understands the MIME type and header, as defined in different RFC. Although non-ASCII characters can be decoded in some header fields, the Message header can only be limited to US-ASCII characters.
· Javax. mail. Address: Once you create a Session and Message and enter the content in the Message, you can use Address to determine the mail Address. Like Message, Address is also an abstract class. You are using the Javax. mail. internet. InternetAddress class.
· Javax. mail. Authenticator: like the Java.net class, JavaMail API can also use Authenticator to access protected resources through the user name and password. For the JavaMail API, these resources are the mail server. JavaMail Authenticator is in the Javax. mail package, and it is different from the Authenticator class with the same name in Java.net. The two do not share the same Authenticator because the JavaMail API is used in Java 1.1 and does not have the Java.net category.
To use Authenticator, first create a subclass of the abstract class and return the PasswordAuthentication instance from the getPasswordAuthentication () method. After creation, you must register the Authenticator with the session. Authenticator will be notified when authentication is required. In the pop-up window, you can read user names and passwords from the configuration file (although not encrypted) and return them as PasswordAuthentication objects to the caller.
· Javax. mail. Transport: The last part of message sending is the Transport class. This class sends messages (usually SMTP) in the language specified by the Protocol ). It is an abstract class and works in a similar way as Session. Only the static send () method can be called to use the default version of the class: Transport. send (message); or, the reader can also obtain a specific instance from the session for his own protocol, pass the user name and password (if not necessary), send the message, close the connection.
· Javax. mail. Store: The Store class implements read, write, monitoring, and search operations on specific mail protocols. You can use the Javax. mail. Store class to access the Javax. mail. Folder class.
· Javax. mail. Folder: the Folder class is used to organize mails hierarchically and provide the ability to access emails in Javax. mail. Message format.

How to use JavaScript/"target =" _ blank "> JSP to send an email?

Below is a simple example to illustrate how emails are sent in JavaScript/"target =" _ blank "> JSP. This example consists of two files. An HTML file is used to create a form of mail information (including the sender, recipient, and topic, and sends the form content to the JavaScript/"target =" _ blank "> JSP file. The other is the JavaScript/" target = "_ blank"> JSP page, which is responsible for sending emails.

HTML file
<HTML>
<BODY>
<FORM action = "sendmail. jsp" method = "post">
<TABLE align = "center">
<TR>
& Lt; TD width = "50%" & gt;
To: <BR> <INPUT name = "to" size = "25">
</TD>
& Lt; TD width = "50%" & gt;
From: <BR> <INPUT name = "from" size = "25">
</TD>
</TR>
<TR>
<TD colspan = "2">
Subject: <BR> <INPUT name = "subject" size = "50">
</TD>
</TR>
<TR>
<TD colspan = "2">
<P> Message: <BR>
<TEXTAREA name = "text" rows = 25 cols = 85> </TEXTAREA>
</P>
</TD>
</TR>
</TABLE>
<INPUT type = "submit" name = "cb_submit" value = "Send">
<INPUT type = "reset" name = "cb_reset" value = "Clear">
</FORM>
</BODY>
</HTML>

JavaScript/"target =" _ blank "> the JSP page is used to obtain the data submitted by the form and assign the data to the corresponding objects in the Java Mail API, finally, the email is sent.

Sendmail. jsp file
<% @ Page import = "Javax. mail. *, Javax. mail. internet. *, Javax. activation. *, Java. util. *" %>
<Html>
<Head>
<TITLE> JavaScript/"target =" _ blank "> 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>

How to send HTML-type emails

In the preceding example, we have implemented how to send text-formatted emails. How can we send HTML-formatted emails? Let's take a look at the example below.
This example consists of four files:
· Form.htm: form used to create Mail Information
· Send. jsp: used to obtain the information submitted by the form and call the mymail. mail. HTML. send () method to send the mail.
· StringDataSource. Java: Custom JavaBean, used to convert the Body part of the email into HTML format.
· HTML. Java: Custom JavaBean, used to send HTML-format emails. The mymail. mail. HTML. send () method mentioned in the send. jsp file is defined in this JavaBean.

Form.htm
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> JavaMail-send an HTML email </title>
</Head>
<Body>
<Table border = "0" cell

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.