- 浏览: 166594 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (54)
- JAVA_SE (18)
- JAVA_EE (11)
- Spring Security (0)
- Spring Ldap (0)
- Spring Quartz (0)
- Spring AOP (0)
- Spring IOC (1)
- EhCache (0)
- OSCache (0)
- Struts2 I18N (0)
- Flex (1)
- Android (1)
- PHP (1)
- HTML (3)
- DIV+CSS (1)
- JavaScript (7)
- Groovy (0)
- jQuery (3)
- ExtJS (0)
- DOJO (0)
- Ajax (1)
- Oracle (1)
- Microsoft SQL Server (1)
- MySQL (1)
- XML (0)
- Ibatis (0)
- WebLogic (0)
- JBoss (1)
- Tomcat (6)
- GlassFish (0)
- Jetty (1)
- JSF (0)
- JSTL+EL (1)
- SSH_Integration (0)
- Maven (1)
- Active MQ (0)
- Ldap (2)
- MyVD (1)
- JNI (0)
- JAVA 7 (0)
- 程序人生 (2)
- WebService (3)
- 小知识点综合 (2)
- 开发工具 (3)
- nodejs (1)
- javamail (2)
- Ant (1)
- linux (4)
最新评论
-
stranger2008:
这个代码有问题public static int check( ...
java检测端口号是否配占用 -
pengyan5945:
gold__sun 写道做安装包?将tomcat整个打成exe ...
Tomcat找不到service.bat文件 -
gold__sun:
做安装包?将tomcat整个打成exe么?求教如何做
Tomcat找不到service.bat文件 -
pengyan5945:
chenzengzhe 写道不是吧,你在安装的时候可以选择语言 ...
CentOS 6.2 中文 -
chenzengzhe:
不是吧,你在安装的时候可以选择语言呀.
CentOS 6.2 中文
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.
发表评论
-
jsp 简单下载
2013-01-24 15:32 1383<%@ page language="jav ... -
tomcat之JNDI数据源配置
2012-07-31 11:15 1520一、docbase包含方式部署项目 D:\apache-to ... -
eclipse中tomcat配置(待完善)
2012-07-30 21:03 1234tomcat版本:apache-tomcat-6.0.29 ... -
常见web服务器错误
2012-05-17 11:46 4076参考地址:http://www.w3.org/Protocol ... -
java mail jar冲突
2012-05-16 16:24 6077开发环境:jdk1.6.0_25 MyEclipse- ... -
常用邮件服务器【更新中】
2012-05-16 15:08 1533技巧:如果你不知道, ... -
spring IOC经典理解
2012-04-19 17:30 1025不多解释,直接上图片! -
Java获取操作系统信息
2012-04-10 15:09 1477今天在看jdk的demo时候发现java获取系统操作系统的一些 ... -
AD查询1000条限制和解决方案
2012-03-30 14:44 2798公司的一个项目要从AD上取数据,为了测试性能,批量在AD ... -
java检测端口号是否配占用
2012-03-27 14:21 8135java检测端口号是否被占用的工具类: p ... -
java集合(交集,并集,差集)
2011-12-15 09:44 20944说明:这里没有求差集的代码,有了交集和并集,差集=并集-交 ... -
SVN常见问题处理
2011-11-18 15:55 12911.SVN 提交时报错:Path is not a worki ... -
double与int类型自动转换
2011-11-08 17:20 1580package com.abc.test; pub ... -
Map排序(按key/按value)
2011-11-08 17:07 7837package com.abc.test; imp ... -
构造方法在继承关系时的执行过程
2011-11-05 23:43 1256一句话:在执行子类的构造方法是先执行父类的构造方法! ... -
for循环,你深刻理解了吗?
2011-11-04 00:22 1327前几天,有一个面试机会,去看了看,遇到一个认为不错的面试题! ... -
HTML文件中使用Java程序
2011-09-27 15:00 1560HTML文件中使用Java程序:简而言之,在HTML文件中引入 ... -
Myeclipse安装svn插件(link方式)
2011-06-19 16:42 4059Myeclipse安装svn插件(link方式) 优 ... -
JAVA多线程通信
2011-06-16 12:33 986JAVA多线程通信 package com.frank ... -
Java序列化与反序列化(Serializable)
2011-06-16 00:09 4000Java序列化与反序列化(Serializable) ...
相关推荐
"commons-email-1.3.jar_mail.jar_activation.jar"这个压缩包包含了两个关键的Java邮件处理库,它们是Apache Commons Email和JavaMail API,以及JavaBeans Activation Framework(JAF)。接下来,我们将深入探讨这些...
这个"commons-dbutils-1.3-bin"压缩包包含了DBUtils 1.3版本的所有二进制文件,供开发者在项目中直接引用。 DBUtils的核心理念是提供一个简单且轻量级的工具集,用于处理基础的数据库访问任务,比如执行SQL查询、...
fileupload-1.3.1-bin commons-fileupload-1.3-bin commons-io-2.4-bin commons-jexl-1.1 commons-lang-2.6-bin commons-lang3-3.4-bin commons-launcher-1.1 commons-logging-1.2-bin ccommons-math-2.2 commons-...
commons整理的常用的jar包,希望对你们有用,org.apache.commons 的经典jar 包 commons-beanutils-1.8.0-bin、 commons-betwixt-0.8、 commons-cli-1.1、 commons-codec-1.3、 commons-collections-3.2.1-bin、...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
同时,通过Apache Commons Email库,你可以方便地添加附件、使用HTML模板等高级功能。 总结来说,"mail.jar", "activation.jar" 和 "commons-email-1.3.jar"是Java编程中发送邮件所必需的关键库,它们提供了完整的...
包括commons-beanutils-1.8.0-bin、commons-betwixt-0.8、commons-cli-1.1、commons-codec-1.3、commons-collections-3.2.1-bin、commons-digester-1.8、commons-discovery-0.4、commons-email-1.1-bin、commons-...
org.apache.commons相关的所以jar包,包括commons-beanutils-1.8.0-bin.zip;commons-betwixt-0.8.zip;commons-cli-1.1.zip;commons-codec-1.3.zip;commons-collections-3.2.1-bin.zip;commons-digester-1.8.zip...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
org.apache.commons的jar包 包含:commons-beanutils-1.8.0-bin.zip commons-betwixt-0.8.zip commons-cli-1.1.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-digester-1.8.zip commons-...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
chain-1.2-bin.zip commons-chain-1.2-src.zip commons-cli-1.1-src.zip commons-cli-1.1.zip commons-codec-1.3-src.zip commons-codec-1.3.zip commons-collections-3.2.1-bin.zip commons-...
在“commons-email-1.3-bin”这个版本中,包含了编译好的库文件和必要的资源,供开发者在他们的Java应用程序中直接使用。这个压缩包通常包含JAR文件(如commons-email-1.3.jar),这是运行和开发基于commons-email的...
org.apache.commons 的经典jar 包 commons-beanutils-1.8.0-bin、 commons-betwixt-0.8、 commons-cli-1.1、 commons-codec-1.3、 commons-collections-3.2.1-bin、 commons-digester-1.8、 commons-...
DBUtils是Apache Commons项目的一部分,它是一个开源的Java库,专为简化数据库操作而设计。在Java编程中,DBUtils提供了许多实用的功能,使得开发者在处理JDBC(Java Database Connectivity)时可以更加高效和便捷。...
包括 commons-beanutils-core-1.8.0.jar commons-collections-3.2.1....commons-email-1.2.jar commons-io-1.3.2.jar commons-lang-2.5.jar commons-logging-1.1.1.jar commons-net-1.4.1.jar commons-pool-1.5.4.jar