Javamail email Sending System

Source: Internet
Author: User
Use org. Apache. commons. Mail to send emails

The Commons project of Apache has an email subproject, which encapsulates the javamail API and makes it easy to use. Before you begin, make the following preparations:

1. javamail API

Add the mail package in javamail API to classpath, javamail API: http://java.sun.com/products/javamail/downloads/index.html

2. commons email

: Http://www.apache.org/dist/commons/email/

The project directory structure is as follows:

The following two simple examples are provided:

// Send an email using simpleemail in sendmail. Java

Package com. Apache. commons. Email. Demo;

Import org. Apache. commons. Mail. simpleemail;

Public class sendmail
{
Public static void main (string [] Arg) throws exception
{
// The use of simpleemail may produce garbled characters for Chinese content
Simpleemail email = new simpleemail ();

// SMTP server name
Email. sethostname ("smtp.163.com ");
// User name and password used to log on to the email server
Email. setauthentication ("peki", "123456 ");
// Recipient
Email. addto ("", "xiqin ");
// Sender
Email. setfrom ("", "Chen ");
// Title
Email. setsubject ("Test message ");
// Email content
Email. setmsg ("this is a simple test of commons-email <br> I am Chen ");
// Send
Email. Send ();

System. Out. println ("Send email successful! ");

}
}

The receiving result is as follows:

This is a simple test of commons-email <br> ????

Garbled characters are generated, and HTML content is not properly displayed.

// Mailsender. Java use htmlemail to send emails

Package com. Apache. commons. Email. Demo;

Import org. Apache. commons. Mail. emailexception;
Import org. Apache. commons. Mail. htmlemail;

Public class mailsender {

Public static void main (string [] ARGs ){
// Do not use simpleemail. garbled characters may occur.
Htmlemail email = new htmlemail ();
Try {
// Here is the name of the SMTP sending server:, 163 is as follows:
Email. sethostname ("smtp.163.com ");
// Set the character delimiter set
Email. setcharset ("GBK ");
// Recipient's email address
Email. addto ("");
// Sender's email address
Email. setfrom ("", "Chen ");
// If you need authentication information, set authentication: User Name-password. The registration name and password of the sender on the email server.
Email. setauthentication ("peki", "123456 ");
Email. setsubject ("the discussion will be held in the conference room at pm. Please attend ");
// The message to be sent. Because htmlemail is used, HTML tags can be used in the mail content.
Email. setmsg ("the discussion will be held in the conference room at pm. Please attend on time <br> Haha ~! ");
// Send
Email. Send ();

System. Out. println ("email sent successfully! ");
} Catch (emailexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("failed to send email! ");
}

}

}

The receiving result is as follows:

The meeting room will be discussed at pm. Please attend on time
Haha ~! No garbled characters are generated. HTML content is displayed normally.The above procedures are for reference. If you need to use them in development, you need to study them in depth.# _ Java

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.