使用Apache Commons Email 发生邮件
Apache Commons Email的Maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
使用示例:
package app.hihn.me.mail.demo; import java.net.MalformedURLException;
import java.net.URL; import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.ImageHtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.resolver.DataSourceUrlResolver; /**
*
* <pre>
* <h1>Debugging</h1>
* The JavaMail API supports a debugging option that will can be very
* useful if you run into problems. You can activate debugging on any of the
* mail classes by calling setDebug(true). The debugging output will be written
* to System.out.
*
* Sometimes you want to experiment with various security setting or features of
* commons-email. A good starting point is the test class EmailLiveTest and
* EmailConfiguration which are used for testing commons-email with real SMTP
* servers.
*
* </pre>
*
* @describe Apache Commons Email 使用示例
* @author liwen
*
*/
public class SendEmailDemo {
/**
* <pre>
*
* Our first example will create a basic email message to "John Doe" and
* send it through your Google Mail (GMail) account.
*
* 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.
*
* </pre>
*
* @describe 发送内容为简单文本的邮件
* @throws EmailException
*/
public static void sendSimpleTextEmail() throws EmailException {
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
// 用户名和密码为邮箱的账号和密码(不需要进行base64编码)
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();
} /**
* <pre>
*
* 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.
*
* </pre>
*
* @describe 发送包含附件的邮件(附件为本地资源)
* @throws EmailException
*/
public static void sendEmailsWithAttachments() throws EmailException {
// 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();
} /**
* <pre>
*
* 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.
*
* </pre>
*
* @describe 发送包含附件的邮件(附件为在线资源)
* @throws EmailException
* @throws MalformedURLException
*/
public static void sendEmailsWithOnlineAttachments() throws EmailException, MalformedURLException {
// 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();
} /**
* <pre>
*
* 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.
*
* 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.
*
* <pre>
*
* @describe 发送内容为HTML格式的邮件
* @throws EmailException
* @throws MalformedURLException
*/
public static void sendHTMLFormattedEmail() throws EmailException, MalformedURLException {
// 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();
} /**
* <pre>
*
* 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.
*
* First we create a HTML email template referencing some images. All
* referenced images are automatically transformed to inline images by the
* specified DataSourceResolver.
*
* </pre>
*
* @describe 发送内容为HTML格式的邮件(嵌入图片更方便)
* @throws MalformedURLException
* @throws EmailException
*/
public static void sendHTMLFormattedEmailWithEmbeddedImages() throws MalformedURLException, EmailException {
// load your HTML email template
String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ...."; // define you base URL to resolve relative resource locations
URL url = new URL("http://www.apache.org"); // create the email message
ImageHtmlEmail email = new ImageHtmlEmail();
email.setDataSourceResolver(new DataSourceUrlResolver(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();
} public static void main(String[] args) throws EmailException {
Email email = new SimpleEmail();
email.setHostName("smtp.163.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("liwenzlw@163.com", "xxxxxxxxxxxxx"));
email.setSSLOnConnect(true);
email.setFrom("liwenzlw@163.com", "利文");
email.setSubject("异常信息");
email.setMsg("This is a test mail ... :-)");
email.addTo("1309928657@qq.com");
email.send();
}
}
使用Apache Commons Email 发生邮件的更多相关文章
- Apache commons email 使用过程中遇到的问题
apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...
- 使用Apache commons email发送邮件
今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包. 据说这个包是对javamail进行了封装,简化了操作. 这里讲一下具体用法吧 一.首先你需要有邮箱账号和一 ...
- Apache Commons Email 使用网易企业邮箱发送邮件
最近使用HtmlEmail 发送邮件,使用网易企业邮箱,发送邮件,死活发不出去!原以为是网易企业邮箱,不支持发送邮箱,后面经过研究发现,是apache htmlEmail 的协议导致,apache E ...
- Commons Email使用
Apache Commons Email Apache的一个开源项目,是基于另一个开源项目Java Mail上进行封装的,使用起来更加简单方便: http://commons.apache.org/p ...
- Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
关键词:IntelliJ IDEA.Tomcat.commons-io-2.5.jar.java.lang.ClassNotFoundException: org.apache.commons.io. ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- Apache Commons 工具类介绍及简单使用
转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...
- Apache Commons 工具类简单使用
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...
- 使用Commons Email发送邮件
Commons Email是apache commons库中的一个组件,对java mail做了一些个封装,提供能为简化的API供开发者使用.它依赖于javax.mail . 首先下载commons- ...
随机推荐
- PHP中public、protected、private权限修饰符
PHP中有三种访问修饰符 默认是public public(公共的.默认) protected(受保护的) private(私有的) 访问权限 public protected private 类内 ...
- RAID及热备盘详解
RAID,为Redundant Arrays of Independent Disks的简称,中文为廉价冗余磁盘阵列. 一.出现的原因(RAID的优点): 它的用途主要是面向服务器,但现在的个人电脑由 ...
- PIC24 通过USB在线升级 -- USB HID bootloader
了解bootloader的实现,请加QQ: 1273623966 (验证填bootloader):欢迎咨询或定制bootloader; 我的博客主页www.cnblogs.com/geekygeek ...
- RobotFramework自动化测试框架-移动手机自动化测试Click Element关键字的使用
Click Element关键字用来模拟点击APP界面上的一个元素,该关键字接收一个参数[ locator ] ,这里的locator指的是界面元素的定位方式. 示例1:使用Click Element ...
- [转载]Reids配置文件详解
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- Python自学笔记-字符串编码(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 1.理解变 ...
- ubuntu的应用程序哪里找
一般来说11.04以前的桌面是gnome,点左上角的那个小圆圈,会出来一个下拉菜单,里面就有应用程序. 11.10以后的桌面换成unity 在dash中寻找应用程序很不方便,知道名字的还好,不知道名字 ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- 我的第一个python web开发框架(8)——项目结构与RESTful接口风格说明
PS:再次说明一下,原本不想写的太啰嗦的,可之前那个系列发布后发现,好多朋友都想马上拿到代码立即能上手开发自己的项目,对代码结构.基础常识.分类目录与文件功能结构.常用函数......等等什么都不懂, ...
- Android模拟器检测常用方法
在Android开发过程中,防作弊一直是老生常谈的问题,而模拟器的检测往往是防作弊中的重要一环,接下来有关于模拟器的检测方法,和大家进行一个简单的分享. 1.传统的检测方法. 传统的检测方法主要是对模 ...