apache commons-email1.3使用
apache commons-email1.3下载地址:
实例代码:
参考地址:http://commons.apache.org/email/userguide.html
A simple text email
Our first example will create a basic email message to "John Doe" and send it through your Google Mail (GMail) account.
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();
The call to setHostName("mail.myserver.com") sets the address of the outgoing SMTP server that will be used to send the message. If this is not set, the system property "mail.host" will be used.
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 org.apache.commons.mail.*;
... // 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 is the picture you wanted"); // add the attachment
email.attach(attachment); // send the email
email.send();
You can also use EmailAttachment to reference any valid URL for files that you do not have locally. When the message is sent, the file will be downloaded and attached to the message automatically.
The next example shows how we could have sent the apache logo to John instead.
import org.apache.commons.mail.*;
... // 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();
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 org.apache.commons.mail.HtmlEmail;
... // 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("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // set the alternative message
email.setTextMsg("Your email client does not support HTML messages"); // send the email
email.send();
First, notice that the call to embed() returns a String. This String is a randomly generated identifier that must be used to reference the image in the image tag.
Next, there was no call to setMsg() in this example. The method is still available in HtmlEmail but it should not be used if you will be using inline images. Instead, the setHtmlMsg() and setTextMsg() methods were used.
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 org.apache.commons.mail.HtmlEmail;
... // load your HTML email template
String htmlEmailTemplate = .... // define you 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 email client does not support HTML messages"); // send the email
email.send();
First we create a HTML email template referencing some images. All referenced images are automatically transformed to inline images starting from the current working directory.
apache commons-email1.3使用的更多相关文章
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...
- apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)
转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...
- apache.commons.compress 压缩,解压
最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...
- CloudSim4.0报错NoClassDefFoundError,Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.distribution.UniformRealDistribution
今天下载了CloudSim 4.0的代码,运行其中自带的示例程序,结果有一部分运行错误: 原因是找不到org.apache.commons.math3.distribution.UniformReal ...
- Java--对象池化技术 org.apache.commons.pool2.ObjectPool
org.apache.commons.pool2.ObjectPool提供了对象池,开发的小伙伴们可以直接使用来构建一个对象池 使用该对象池具有两个简单的步骤: 1.创建对象工厂,org.apache ...
- 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)
首先dbcp相关的jar包和MySQL的驱动包导入到项目中. dbcp.properties配置文件如下,并放到项目根目录下. driverClassName=com.mysql.jdbc.Drive ...
- Apache Commons CLI官方文档翻译 —— 快速构建命令行启动模式
昨天通过几个小程序以及Hangout源码学习了CLI的基本使用,今天就来尝试翻译一下CLI的官方使用手册. 下面将会通过几个部分简单的介绍CLI在应用中的使用场景. 昨天已经联系过几个基本的命令行参数 ...
- apache commons math 示例代码
apache commons Math是一组偏向科学计算为主的函数,主要是针对线性代数,数学分析,概率和统计等方面. 我虽然是数学专业毕业,当年也是抱着<数学分析>啃,但是好久不用,这些概 ...
- Apache Commons Lang
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/package- ...
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
随机推荐
- mysql 升级方法
Performing an In-place Upgrade This section describes how to perform an in-place upgrade. Review Bef ...
- C/C++基础总结
1 static(静态)变量有什么作用 3个体明显的作用:1)在函数体内,静态变量具有“记忆”功能,即一个被声明为静态变量在一个函数被调用的过程中其值维持不变2)在模块内,它的作用域范围是有限制的,即 ...
- 重新组织 vs 重新生成索引
索引是数据库引擎中针对表(有时候也针对视图)建立的特别数据结构,用来帮助查找和整理数据.索引的重要性体现在能够使数据库引擎快速返回查询 结果.当对索引所在的基础数据表进行修改时(包括插入.删除和更新等 ...
- Golang学习 - io/ioutil 包
------------------------------------------------------------ // Discard 是一个 io.Writer 接口,调用它的 Write ...
- 安装PIL库时提示python未注册错误(自定义python安装路径)
import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...
- Sql 之 sql中的强制类型转换
1. convert(数据类型, 字段名) convert(datetime, startDate) 2. cast(字段名 as 数据类型) ,))
- C. Anya and Smartphone
C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Android 高级UI设计笔记10:瀑布流控件PinterestLikeAdapterView的使用
1. 首先我们看看瀑布流的效果,如下: 2. 今天要介绍的瀑布流控件是:PinterestLikeAdapterView 项目地址:https://github.com/GDG-Korea/Pinte ...
- 【HTML5 2】《html5 开发精要与实例讲解》 step1 -- 导读
一.教程重点:以 综合性案例 为导向,辅之以 精要知识点 二.内容概况: 第1部分:通过 大小型案例 对 各重要知识点 进行详细讲解 第2部分:jWebSocket.RGraph.WebGL 三个重要 ...
- mysql_DML_update
update 表名 set 字段=XX where....;(记得加条件不安全改了) 多个字段: update 表名 set 字段1=XX,字段2= where....;(记得加条件不安全 ...