Send email using commons-emai

Source: Internet
Author: User
Jakarta commons-Email 1.0 is released.

Commons-Email provides a set of simpler email operation APIs.

Commons-email is built based on the javamail API. The core structure only contains a few classes.

The following is a brief introduction to some of the classes:

Simpleemail-This class is used to send basic text based emails.

Multipartemail-This class is used to send multipart messages. This allows a text message with attachments either inline or attached.

Htmlemail-This class is used to send HTML formatted emails. It has all of the capabilities as multipartemail allowing attachments to be easily added. It also supports embedded images.

Emailattachment-this is a simple container class to allow for easy handling of attachments. it is for use with instances of multipartemail and htmlemail. commons-email is an open-source API provided by Apache and is an encapsulation of javamail. Therefore, javamail must be used. jar is added to the class path, including simpleemail, multipartemail, htmlemail, and emailattachment.
 
Simpleemail: send a simple email and do not add attachments.
Multipartemail: Text mail. You can add multiple attachments.
Htmlemail: HTML-formatted mail with all "functions" of the multipartemail class"
Emailattchment: attachment class. You can add local resources or specify network resources. Resources on the network are automatically downloaded and sent upon sending.
 
Send emails in basic text format:
====================
Simpleemail email = new simpleemail ();
// SMTP host
Email. sethostname ("mail.test.com ");
// User name and password used to log on to the email server
Email. setauthentication ("test", "testpassword ");
// Recipient
Email. addto ("jdoe@somewhere.org", "John Doe ");
// Sender
Email. setfrom ("me@apache.org", "me ");
// Title
Email. setsubject ("Test message ");
// Email content
Email. setmsg ("this is a simple test of commons-email ");
// Send
Email. Send (); text format, with attachment Email:
============================
// Attachment. Multiple attachment objects can be defined.
Emailattachment attachment = new emailattachment ();
Attachment. setpath ("E: // 1.20 ");
Attachment. setdisposition (emailattachment. Attachment );
Attachment. setdescription ("picture of John ");
//
Multipartemail email = new multipartemail ();
// SMTP host
Email. sethostname ("mail.test.com ");
// User name and password used to log on to the email server
Email. setauthentication ("test", "testpassword ");
// Recipient
Email. addto ("jdoe@somewhere.org", "John Doe ");
// Sender
Email. setfrom ("me@apache.org", "me ");
// Title
Email. setsubject ("Test message ");
// Email content
Email. setmsg ("this is a simple test of commons-email ");
// Add an attachment
Email. Attach (Attachment );
// Send
Email. Send ();
 
Send an email with an attachment in HTML format:
========================
// Attachment. Multiple attachment objects can be defined.
Emailattachment attachment = new emailattachment ();
Attachment. setpath ("E: // 1.20 ");
Attachment. setdisposition (emailattachment. Attachment );
Attachment. setdescription ("picture of John ");
//
Htmlemail email = new htmlemail ();
// SMTP host
Email. sethostname ("mail.test.com ");
// User name and password used to log on to the email server
Email. setauthentication ("test", "testpassword ");
// Recipient
Email. addto ("jdoe@somewhere.org", "John Doe ");
// Sender
Email. setfrom ("me@apache.org", "me ");
// Title
Email. setsubject ("Test message ");
// Email content
Email. sethtmlmsg ("<B> This is a simple test of commons-email </B> ");
// Add an attachment
Email. Attach (Attachment );
// Send
Email. Send ();

 

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.