Apache Commons Email Usage Experience

Source: Internet
Author: User

The most recent development in the project needs to use the Send mail function, when the background timing task after processing to notify the caller. Java Mail API is more cumbersome to use, so here is the Apache Commons email, official website address: http://commons.apache.org/proper/commons-email/,Commons Email The API is more concise and efficient, learning is also very fast. Write an article to share with you, the problem can leave a message.

1. Send a simple text message

email email = new Simpleemail ();
Email.sethostname ("smtp.googlemail.com");
Email.setsmtpport (465);
Email.setauthenticator (New Defaultauthenticator ("username", "password"));
Email.setsslonconnect (true);
Email.setfrom ("user@gmail.com");
Email.setsubject ("Testmail");
Email.setmsg ("This is a test mail ...:-)");
Email.addto ("foo@bar.com");
Email.send ();

2, send the message with the attachment

Create the attachment
  emailattachment attachment = new Emailattachment ();
  Attachment.setpath ("mypictures/john.jpg");
  Attachment.setdisposition (emailattachment.attachment);
  Attachment.setdescription ("Picture of John");
  Attachment.setname ("John");

  Create the email message
  multipartemail email = new Multipartemail ();
  Email.sethostname ("mail.myserver.com");
  Email.addto ("jdoe@somewhere.org", "John Doe");
  Email.setfrom ("me@apache.org", "Me");
  Email.setsubject ("the Picture");
  Email.setmsg ("Here's the picture for You Wanted");

  Add the Attachment
  Email.attach (attachment);

  Send the email
  email.send ();

You can also add files on your network to an attachment by using any of the links, such as:

Create the attachment
  emailattachment attachment = new Emailattachment ();
  Attachment.seturl (New URL ("Http://www.apache.org/images/asf_logo_wide.gif"));
  Attachment.setdisposition (emailattachment.attachment);
  Attachment.setdescription ("Apache logo");
  Attachment.setname ("Apache logo");

  Create the email message
  multipartemail email = new Multipartemail ();
  Email.sethostname ("mail.myserver.com");
  Email.addto ("jdoe@somewhere.org", "John Doe");
  Email.setfrom ("me@apache.org", "Me");
  Email.setsubject ("the logo");
  Email.setmsg ("Here is Apache ' s logo");
  
  Add the Attachment
  Email.attach (attachment);

  Send the email
  email.send ();

3, send the HTML format of the message

Create the email message
  htmlemail email = new Htmlemail ();
  Email.sethostname ("mail.myserver.com");
  Email.addto ("jdoe@somewhere.org", "John Doe");
  Email.setfrom ("me@apache.org", "Me");
  Email.setsubject ("Test email with inline image");
  
  Embed the image and get the content id
  URL url = new URL ("Http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed (URL, "Apache logo");
  
  Set the HTML message
  email.sethtmlmsg ("


4, send the picture with the HTML format mail

Load your HTML email template
  String htmlemailtemplate = ....

  Define you the base URL to resolve relative resource locations
  url url = new URL ("http://www.apache.org");

  Create the email message
  htmlemail email = new Imagehtmlemail ();
  Email.setdatasourceresolver (new Datasourceresolverimpl (URL));
  Email.sethostname ("mail.myserver.com");
  Email.addto ("jdoe@somewhere.org", "John Doe");
  Email.setfrom ("me@apache.org", "Me");
  Email.setsubject ("Test email with inline image");
  
  Set the HTML message
  email.sethtmlmsg (htmlemailtemplate);

  Set the alternative message
  email.settextmsg ("Your e-mail client does not support HTML messages");

  Send the email
  email.send ();



In addition, found in the use of email.addto can only add one contact at a time, if you want to send to multiple people, need to use for loop nesting to implement, the following is a simple example:

public static void Main (string[] args) {

        String maillist = "abc@163.com;tt@qq.com";

        string[] List = Maillist.split (";");
        for (int i=0;list!=null && i<list.length;i++) {   //nested call

            SendEmail (List[i]);
        }

    public static void SendEmail (String target) {

        try{
            Email email = new Simpleemail ();
            Email.sethostname ("smtp.163.com");
            Email.setsmtpport (465);
            Email.setauthenticator (New Defaultauthenticator ("abc@163.com", "abc"));
            Email.setsslonconnect (true);
            Email.setfrom ("abc@163.com");
            Email.addto (target);

            Email.setsubject ("Test Mail");
            Email.setmsg ("This is a Test mail");
            Email.send ();

        } catch (Exception e) {
            e.printstacktrace ();
        }
    }



Detailed tutorials can refer to the official website userguide, Link: http://commons.apache.org/proper/commons-email/userguide.html




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.