Using JavaMail to build a JSP Mail System

Source: Internet
Author: User
Tags file url

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

 
 
  1. <HTML>   
  2. <BODY>   
  3. <FORM action="sendmail.jsp" method="post">   
  4. <TABLE align="center">   
  5. <TR>   
  6. <TD width="50%">   
  7. To:<BR><INPUT name="to" size="25">   
  8. </TD>   
  9. <TD width="50%">   
  10. From:<BR><INPUT name="from" size="25">   
  11. </TD>   
  12. </TR>   
  13. <TR>   
  14. <TD colspan="2">   
  15. Subject:<BR><INPUT name="subject" size="50">   
  16. </TD>   
  17. </TR>   
  18. <TR>   
  19. <TD colspan="2">   
  20. Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>   
  21. </TD>   
  22. </TR>   
  23. </TABLE>   
  24. <INPUT type="submit" name="cb_submit" value=" Send ">   
  25. <INPUT type="reset" name="cb_reset" value=" Clear ">   
  26. </FORM>   
  27. </BODY>   
  28. </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.

 
 
  1. Properties props = new Properties ();   
  2. props.put("mail.smtp.host", "smtp.jspinsider.com");   

Dialog object

All JavaMail-based programs require at least one or all of the conversation objectives.

 
 
  1. Session sendMailSession;   
  2. 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.

 
 
  1. Transport transport;   
  2. 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.

 
 
  1. 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.

 
 
  1. <%@ page   
  2. import= " javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*"   
  3. %>   

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.

 
 
  1. newMessage.setFrom(new InternetAddress(request.getParameter("from")));   
  2. newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));   
  3. newMessage.setSubject(request.getParameter("subject"));   
  4. newMessage.setSentDate(new Date());   
  5. newMessage.setText(request.getParameter("text"));   

The message will be sent now. It is very easy to implement through JavaMail.

 
 
  1. 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:

 
 
  1. Sample JSP email Utility Using JavaMail   
  2. <%@ page   
  3. import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"   
  4. %>   
  5. <html>   
  6. <head>   
  7. <TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>   
  8. </HEAD>   
  9. <BODY>   
  10. <%   
  11. try{   
  12. Properties props = new Properties();   
  13. Session sendMailSession;   
  14. Store store;   
  15. Transport transport;   
  16. sendMailSession = Session.getInstance(props, null);   
  17. props.put("mail.smtp.host", "smtp.jspinsider.com");   
  18. Message newnewMessage = new MimeMessage(sendMailSession);   
  19. newMessage.setFrom(new InternetAddress(request.getParameter("from")));   
  20. newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));   
  21. newMessage.setSubject(request.getParameter("subject"));   
  22. newMessage.setSentDate(new Date());   
  23. newMessage.setText(request.getParameter("text"));   
  24. transport = sendMailSession.getTransport("smtp");   
  25. transport.send(newMessage);   
  26. %>   
  27. <P>Your mail has been sent.</P>   
  28. <%   
  29. }   
  30. catch(MessagingException m)   
  31. {   
  32. out.println(m.toString());   
  33. }   
  34. %>   
  35. </BODY>   
  36. </HTML>   

You will soon learn about the convenience of JavaMail. The introduction of JSP mail system is here.

  1. JSP bean code optimization
  2. Detailed introduction to JSP environment configuration Scheme
  3. Use stored procedures in JSP JSTL
  4. Which of ASP. NET, JSP, and PHP is better?
  5. JSP-related software

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.