Use JavaMail to write the email sending program

Source: Internet
Author: User

I have participated in the development of various Web applications and often encounter the need to embed mail functions into Web applications. For example, if it is an EC application, the customer will receive an order confirmation email after placing the order, and the commodity provider will receive an email to process the new order, in this way, the product provider does not have to regularly access its own website or database to process the order, and the customer does not have to write down the content entered when placing the order. Or, when new products are listed, you can send product introductions and various promotions to old customers who want to receive the latest information by email.

In general, the collaboration between the Pull browser (which can automatically collect information) and the Push Mail System (which actively provides information) can bring a better user experience to users, website operators can also provide very favorable solutions.

In this article, we will introduce the Java class library that can easily implement this mail system --JavaMail. By using JavaMail, you do not need to consider the implementation of SMTP-based communication. You only need to set relevant parameters to send emails.

Here, we only try to use fixed text information. Of course, you can edit necessary parameters and obtain data from the database to implement the dynamic mail sending function.

The following code is used to learn more.

1. Install the JavaMail class library

To use the JavaMail class library, in addition to JavaMail itself, JAF (Java Activation Framework) is also required. JAF is not required for Versions later than Java 6. You can obtain these information from the following address.

Http://www.oracle.com/technetwork/java/javamail/index-138643.html

Decompress the package and add the class library under lib to your Classpath.

2. Java program

                        Properties objPrp=     objPrp.put("mail.smtp.host","smtp.xxxxx.ne.jp");      objPrp.put("mail.host","smtp.xxxxx.ne.jp");           Session session=Session.getDefaultInstance(objPrp,          MimeMessage objMsg=                   objMsg.setRecipients(Message.RecipientType.TO,"xxxxxx@hotmail.com"              InternetAddress objFrm= InternetAddress("xxxxxx@gmail.com","E.Kou"                objMsg.setSubject("Mail Test","utf-8"                      objMsg.setText("Hello","utf-8"                    }       }     }

In this example, a Mail named Mail Test is sent from gmail to hotmail.

3. Notes

① Pay attention to the encoding when sending a Chinese email

UTF-8 is used here.

② Both mail. host and mail. smtp. host must be specified.

Because Java recognizes mail. smtp. host first, you can only specify mail. smtp. host. However, Message-ID is generated using mail. host. Therefore, if mail. host is not specified, Message-ID generation may be affected.

 

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.