apache commons-email1.3使用
apache commons-email1.3下载地址:
?
https://repository.apache.org/content/repositories/orgapachecommons-095/org/apache/commons/commons-email/1.3/
?
实例代码:
?
参考地址:http://commons.apache.org/email/userguide.html
?
Our first example will create a basic email message to "John Doe" and send it through your Google Mail (GMail) account.
Email=newSimpleEmail();.("smtp.googlemail.com");.(465);.(newDefaultAuthenticator("username","password"));.(true);.("user@gmail.com");.("TestMail");.("This is a test mail ... :-)");.("foo@bar.com");.();Sending emails with attachments
To add attachments to an email, you will need to use the MultiPartEmail class. This class works just like SimpleEmail except that it adds several overloaded attach() methods to add attachments to the email. You can add an unlimited number of attachments either inline or attached. The attachments will be MIME encoded.
The simplest way to add the attachments is by using the EmailAttachment class to reference your attachments.
In the following example, we will create an attachment for a picture. We will then attach the picture to the email and send it.
import....*;...// Create the attachmentEmailAttachment=newEmailAttachment();.("mypictures/john.jpg");.(EmailAttachment.);.("Picture of John");.("John");// Create the email messageMultiPartEmail=newMultiPartEmail();.("mail.myserver.com");.("jdoe@somewhere.org","John Doe");.("me@apache.org","Me");.("The picture");.("Here is the picture you wanted");// add the attachment.();// send the email.();import....*;...// Create the attachmentEmailAttachment=newEmailAttachment();.(new("http://www.apache.org/images/asf_logo_wide.gif"));.(EmailAttachment.);.("Apache logo");.("Apache logo");// Create the email messageMultiPartEmail=newMultiPartEmail();.("mail.myserver.com");.("jdoe@somewhere.org","John Doe");.("me@apache.org","Me");.("The logo");.("Here is Apache's logo");// add the attachment.();// send the email.();Sending HTML formatted email
Sending HTML formatted email is accomplished by using the HtmlEmail class. This class works exactly like the MultiPartEmail class with additional methods to set the html content, alternative text content if the recipient does not support HTML email, and add inline images.
In this example, we will send an email message with formatted HTML content with an inline image.
import....HtmlEmail;...// Create the email messageHtmlEmail=newHtmlEmail();.("mail.myserver.com");.("jdoe@somewhere.org","John Doe");.("me@apache.org","Me");.("Test email with inline image");// embed the image and get the content id=new("http://www.apache.org/images/asf_logo_wide.gif");String=.(,"Apache logo");// set the html message.("<html>The apache logo - <img src="cid:"++""></html>");// set the alternative message.("Your email client does not support HTML messages");// send the email.();Sending HTML formatted email with embedded images
The previous example showed how to create a HTML email with embedded images but you need to know all images upfront which is inconvenient when using a HTML email template. The ImageHtmlEmail helps you solving this problem by converting all external images to inline images.
import....HtmlEmail;...// load your HTML email templateString=....// define you base URL to resolve relative resource locations=new("http://www.apache.org");// create the email messageHtmlEmail=newImageHtmlEmail();.(newDataSourceResolverImpl());.("mail.myserver.com");.("jdoe@somewhere.org","John Doe");.("me@apache.org","Me");.("Test email with inline image");// set the html message.();// set the alternative message.("Your email client does not support HTML messages");// send the email.();