Do you want to create a JSP email system in JSP to send and receive emails? The following describes how to create an email sending and receiving tool in jsp. In this article, you will learn some key points of the JavaMail API and how to use it in JSP. This article also includes examples of using JavaMail in JSP. JavaMail is a powerful API in JSP applications.
Reading this article requires a preliminary understanding of JSP, JavaBeans, and JavaMail. Of course, you can read this article to learn about JavaMail. If you do not know anything about the above three items, but the servers you use support JSP and JavaMail, you can simply use them by copying/pasting them.
What is JavaMail?
JavaMail is an API released by Sun to process emails. It can easily perform some common mail Transmission.
Although JavaMail is one of Sun's APIs, it has not yet been added to java Development Kit in the standard Java Development Kit), which means you must download the JavaMail file before use. In addition, you also need Sun's JavaBeans Activation Framework JAF ). The execution of the JavaMail Activation Framework is very complex. In this case, JavaMail must depend on its support. In Windows 2000, you must specify the path of these files, which is similar to that in other operating systems.
Next I will explain the most difficult part of this Guide.
This guide consists of three parts: HTML tables, JavaMail, JavaMail, and JSP.
Part 1: HTML table
The first section provides an example of the most basic HTML-based email sending and receiving program. The second part describes how JavaMail works. The third part introduces how to add JavaMail to JSP and create a basic email sending and receiving program.
Division Components
The most important feature of JSP is that it can divide the entire web page into small components. The components used here include:
An HTML table used to send email 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 emails
- <HTML>
- <BODY>
- <FORM action="sendmail.jsp" method="post">
- <TABLE align="center">
- <TR>
- <TD width="50%">
- To:<BR><INPUT name="to" size="25">
- </TD>
- <TD width="50%">
- 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">
- Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>
- </TD>
- </TR>
- </TABLE>
- <INPUT type="submit" name="cb_submit" value=" Send ">
- <INPUT type="reset" name="cb_reset" value=" Clear ">
- </FORM>
- </BODY>
- </HTML>
The above process creates a file containing the basic email information, such as the receiving address, sending Address, subject, and content. Of course, you can decide the information contained in this file based on your own needs.
There are two requirements for the use of this HTML file: The first point is that the generated file must be sent to the program that will be introduced later. In this example, sendmail. jsp is used, but you must use the File URL in the system to replace it. Second, you must have space to allow users to send emails.
Part 2: Use of JavaMail documents
The documentation in the downloaded JavaMail API is very useful. You can find it in/docs/javadocs/index.html under JavaMail. The second part mainly analyzes the mail program components. You can read the document for more information.
JavaMail is required for the component to send mails, which makes operations on the JSP mail system easy to use.
Attribute object
JavaMail needs to create a file in the format of "mail. smtp. host" to send information.
- Properties props = new Properties ();
- props.put("mail.smtp.host", "smtp.jspinsider.com");
Dialog object
All JavaMail-based programs require at least one or all of the conversation objectives.
- Session sendMailSession;
- sendMailSession = Session.getInstance(props, null);
Transmission
The mail can only be sent or received in two statuses. JavaMail describes these two different States as transmission and storage. The transfer will send the mail, and the storage will receive the mail.
- Transport transport;
- transport = sendMailSession.getTransport("smtp");
Using JavaMail can save us a lot of time. JavaMail can replace all SMTP tasks.
Note: JavaMail does not fully support sending and receiving all emails. Currently, it only supports IMAP, SMTP, and POP3. In addition, you only need to wait for the new JavaMail version or develop the protocol yourself.
Information object
The information object will actually reflect the email you sent.
- Message newnewMessage = new MimeMessage(sendMailSession);
This is all four objects we need. The next step is to add the object to JSP.
Part 3: Integration of JavaMail and JSP
Create JSP
Next we will begin to combine them. The most important thing is to confirm the classification according to the instructions on the page. Remember to mark java. util. date on the email.
- <%@ page
- import= " javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*"
- %>
Next, create a confirmation message for sending the email. The confirmation information can be arbitrary. It is generally used "Your email has been sent out of 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 perform operations on the information below. This is as simple as setting the attributes of information objects. You can use the following program to perform this operation.
- 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 be sent now. It is very easy to implement through JavaMail.
- transport.send(newMessage);
Combine all components
Now all the components are complete. Now they are all placed in JSP. Pay attention to every error message and send it back to the user. The Code is as follows. You can copy them and use them directly:
- Sample JSP email Utility Using JavaMail
- <%@ page
- import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
- %>
- <html>
- <head>
- <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 newnewMessage = 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 learn about the convenience of JavaMail. The introduction of JSP mail system is here.
- JSP bean code optimization
- Detailed introduction to JSP environment configuration Scheme
- Use stored procedures in JSP JSTL
- Which of ASP. NET, JSP, and PHP is better?
- JSP-related software