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. maven Dependencies
<dependency>
<groupId>org.apache.commons</groupId>
<artifactid>commons-email </artifactId>
<version>1.4</version>
</dependency>
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 a message with an 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 ();
3. Add the files on the network to the attachment through any link
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 ();
4. Send a message in HTML format
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 ("
5. Send an HTML message with a picture
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 ();
complete Sample Code
Finally, attach a complete, operational code, as follows:
Package com.ricky.mail;
Import Org.apache.commons.mail.DefaultAuthenticator;
Import org.apache.commons.mail.EmailException;
Import Org.apache.commons.mail.HtmlEmail;
Import java.net.MalformedURLException;
Import Java.net.URL; /** * ${description} * * @author Ricky Fung * @create 2016-07-06 11:42 * * public class Simplemaildemo {public S
Tatic String hostName = "smtp.163.com";
public static int smtpport = 465;
public static String Auth_username = "yulore_bigdata_vip@163.com";
public static String Auth_password = "yulorei7##";
public static Boolean sslonconnect = true; public static void Main (string[] args) throws Malformedurlexception, emailexception {htmlemail email = new Htmle
Mail ();
Email.sethostname (HostName);
Email.setsmtpport (Smtpport);
Email.setauthenticator (New Defaultauthenticator (Auth_username, Auth_password));
Email.setsslonconnect (Sslonconnect);
Email.addto ("ricky_feng@163.com", "Ricky"); Email.setfrom ("yulore_bigdata_vip@163.com", "Bigdata");
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 ("reference materials
Http://commons.apache.org/proper/commons-email/userguide.html