Apache common email component Summary

Source: Internet
Author: User
Apache common email component summary. This is a good thing. It supports single-click Simple Mail, attachments, and HTML emails. The following is a summary:

Import java. Io. file;
Import java. Io. unsupportedencodingexception;
Import java.net. malformedurlexception;
Import java.net. url;
Import javax. Mail. Internet. mimeutility;
Import org. Apache. commons. Mail. emailattachment;
Import org. Apache. commons. Mail. emailexception;
Import org. Apache. commons. Mail. htmlemail;
Import org. Apache. commons. Mail. multipartemail;
Import org. Apache. commons. Mail. simpleemail;

Public class testemail {


Private string emailserver = "smtp.163.com ";
Private string username = "test ";
Private string Password = "123456 ";
Private string emailencoding = "GBK"; // email Encoding
/**
* Test sending a simple plain text email that does not contain attachments
* @ Throws emailexception
*/
Public void testsimpleemail () throws emailexception {
Simpleemail email = new simpleemail ();
Email. sethostname (emailserver); // server name
Email. setauthentication (username, password); // user name, password
Email. setcharset (emailencoding); // email encoding method

Email. addto ("[email protected]", "contact us for help"); // recipient
Email. setfrom ("[email protected]", "letter for question"); // sender
Email. setsubject ("Ask questions by letter"); // Title
Email. setmsg ("contact me for help"); // body

Email. Send (); // send
}

/**
* Test sending emails containing attachments
* @ Throws unsupportedencodingexception
* @ Throws emailexception
* @ Throws malformedurlexception
*/
Public void testmultipartemail () throws unsupportedencodingexception, emailexception, malformedurlexception {
// Local attachment
Emailattachment att1 = new emailattachment ();
Att1.setpath ("C:/test the mailbox"); // original attachment path
Att1.setdisposition (emailattachment. Attachment );
// Check whether garbled characters cannot be tested. If garbled characters exist, refer to the next solution.
Att1.setdescription ("attachemnt description gril Chinese"); // attachment description
// Prevent garbled attachments
Att1.setname (mimeutility. encodetext ("test mailbox"); // attachment name

// Network attachment
Emailattachment att2 = new emailattachment ();
Att2.seturl (new URL ("http://www.apache.org/images/asf_logo_wide.gif "));
Att2.setdisposition (emailattachment. Attachment );
Att2.setdescription ("attachemnt description logo Chinese ");
Att2.setname (mimeutility. encodetext ("logo Chinese .gif "));

Multipartemail email = new multipartemail ();
Email. sethostname (emailserver );
Email. setauthentication (username, password );
Email. setcharset (emailencoding );

Email. addto ("[email protected]", "contact us for help"); // recipient
Email. setfrom ("[email protected]", "letter for question"); // sender
Email. setsubject ("Ask questions by letter"); // Title
Email. setmsg ("contact us by email from Liao Yunxiao, Guangzhou"); // body
Email. Attach (att1); // send
Email. Attach (att2 );

Email. Send ();
}

/**
* Test and send an HTML-format email. The test shows that the image used does not have an attachment icon in the QQ email interface.
* @ Throws unsupportedencodingexception
* @ Throws emailexception
* @ Throws malformedurlexception
*/
Public void testhtmlemail () throws unsupportedencodingexception, emailexception, malformedurlexception {
Htmlemail email = new htmlemail ();
Email. sethostname (emailserver );
Email. setauthentication (username, password );
Email. setcharset (emailencoding );

Email. addto ("[email protected]", "contact us for help"); // recipient
Email. setfrom ("[email protected]", "letter for question"); // sender
Email. setsubject ("Ask questions by letter"); // Title

// Local image
File file = new file ("C:/test the mailbox ");
String cid1 = Email. Embed (file, "test mailbox ");
// Network image
URL url = new URL ("http://www.apache.org/images/asf_logo_wide.gif ");
String cid2 = Email. Embed (URL, "logo Chinese .gif ");

Email. sethtmlmsg ("Pretty gril-" + "" + "the Apache logo-" + "");
Email. settextmsg ("your email client does not support HTML messages ");

Email. Send ();
}

Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Testemail test = new testemail ();
Try {
Test. testsimpleemail ();
Test. testmultipartemail ();
Test. testhtmlemail ();
System. Out. println ("OK ");
} Catch (exception e ){
E. printstacktrace ();
}

}

Apache common email component Summary

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.