今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包。

据说这个包是对javamail进行了封装,简化了操作。 这里讲一下具体用法吧

一.首先你需要有邮箱账号和一个授权码。

需要进入到QQ邮箱或者是网易邮箱里面去获取。在邮箱的设置->账户里面,开启如下服务,就能得到一个授权码,这个授权码要好好保管。有了这两个东西就能够通过第三方客户端发送邮件了。

二.导入commons-email的maven依赖。

我用的是1.4,也可以去maven仓库网站(https://mvnrepository.com)上面找别的版本。

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>

三.然后就可以写发送邮件的代码了。

我在网上找了几个案例,如下。

1.发送简单文本邮件。这是最简单也是最常用的。

    /**
* @describe 发送内容为简单文本的邮件
* @throws EmailException
*/
public static void sendSimpleTextEmail() throws EmailException {
Email email = new SimpleEmail();
//设置主机名,QQ邮箱是"smtp.qq.com",网易邮箱是"smtp.163.com"
email.setHostName("smtp.163.com");
// 用户名和密码为邮箱的账号和授权码(不需要进行base64编码)
email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma"));
//设置SSL连接,这样写就对了
email.setSSLOnConnect(true);
//设置来源,就是发送方的邮箱地址
email.setFrom("myemailaddress@163.com");
//设置主题,可以不设置
email.setSubject("java发送邮件");
//设置信息,就是内容,这个必须要有
email.setMsg("这是测试邮件 ... :-)");
//接收人邮箱地址
email.addTo("receiveeraddress@qq.com");
email.send();
}

2.发送包含附件的邮件(附件为本地资源),这里用到了一个EmailAttachment对象,也就是附件的意思

    /**
* @describe 发送包含附件的邮件(附件为本地资源)
* @throws EmailException
*/
public static void sendEmailsWithAttachments() throws EmailException {
// 创建一个attachment(附件)对象
EmailAttachment attachment = new EmailAttachment();
//设置上传附件的地址
attachment.setPath("C:\\Users\\Administrator\\Pictures\\Saved Pictures\\conti.png");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
//这个描述可以随便写
attachment.setDescription("Picture of conti");
//这个名称要注意和文件格式一致,这将是接收人下载下来的文件名称
attachment.setName("conti.png"); //因为要上传附件,所以用MultiPartEmail()方法创建一个email对象,固定步骤都是一样的
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.163.com");
email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma"));
email.setSSLOnConnect(true);
email.addTo("receiveemail@qq.com", "Conti Zhang");
email.setFrom("myemailaddress@163.com", "Me");
email.setSubject("图片");
email.setMsg("这是发送给你的图片");
//将附件添加到邮件
email.attach(attachment); email.send();
}

3.发送包含附近的邮件(附件为在线资源),这个与上传本地附件稍有区别,注意一下就行

   /**
* @describe 发送包含附件的邮件(附件为在线资源)
* @throws EmailException
* @throws MalformedURLException
*/
public static void sendEmailsWithOnlineAttachments() throws EmailException, MalformedURLException {
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.gif"); MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.163.com");
email.setAuthenticator(new DefaultAuthenticator("myemailaddress@163.com", "myshouquanma"));
email.setSSLOnConnect(true);
email.addTo("receiveemail@qq.com", "Conti Zhang");
email.setFrom("myemailaddress@163.com", "Me");
email.setSubject("The logo");
email.setMsg("Here is Apache's logo");
email.attach(attachment);
email.send();
}

4.发送内容为HTML格式的邮件,有些邮件直接打开就是一个HTML页面。虽然不一定用到,可以了解一下

   /**
* @describe 发送内容为HTML格式的邮件
* @throws EmailException
* @throws MalformedURLException
*/
public static void sendHTMLFormattedEmail() throws EmailException, MalformedURLException {
// 这里需要使用HtmlEmail创建一个email对象
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");
email.setAuthenticator(new DefaultAuthenticator("myemailaddresss@163.com", "myshouquanma"));
email.addTo("receiveemail@qq.com", "Conti Zhang");
email.setFrom("myemailaddress@163.com", "Me");
email.setSubject("Test email with inline image"); // 嵌入图像并获取内容id,虽然案例这样写,但我感觉直接在html内容里面写图片网络地址也可以
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo"); // 设置html内容
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>"); // 设置替代内容,如果不支持html
email.setTextMsg("你的邮件客户端不支持html邮件");
email.send();
}

5.发送内容为HTML格式的邮件(嵌入图片更方便)

这里用到了DataSourceFileResolver对象,和DataSourceUrlResolver对象,前者可以解析本地文件路径,后者可以解析网络路径

具体用法如下

    /**
* @describe 发送内容为HTML格式的邮件(嵌入图片更方便)
* @throws MalformedURLException
* @throws EmailException
*/
public static void sendHTMLFormattedEmailWithEmbeddedImages() throws MalformedURLException, EmailException {
//html邮件模板
String htmlEmailTemplate = "<img src=\"http://www.conti.com/images/1.jpg\">";
DataSourceResolver[] dataSourceResolvers =new DataSourceResolver[]{new DataSourceFileResolver(),new DataSourceUrlResolver(new URL("http://"))};
email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
email.setHostName("smtp.qq.com");
email.setAuthenticator(new DefaultAuthenticator("myemailaddress@qq.com", "myshouquanma"));
email.addTo("receiveemail@qq.com", "Conti Zhang");
email.setFrom("myemailaddress@qq.com", "Me");
email.setSubject("Test email with inline image");
email.setHtmlMsg(htmlEmailTemplate);
email.setTextMsg("你的邮件客户端不支持html邮件"); email.send();
}

此种方式可能会报错,会被邮箱认为是有害邮件不接收而导致发送失败。解决方法就是,网易邮箱不行就换QQ邮箱,我就是这样做的

好了,就这么多,欢迎讨论!

使用Apache commons email发送邮件的更多相关文章

  1. 使用Apache Commons Email 发生邮件

    Apache Commons Email的Maven依赖 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-e ...

  2. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  3. 使用Commons Email发送邮件

    Commons Email是apache commons库中的一个组件,对java mail做了一些个封装,提供能为简化的API供开发者使用.它依赖于javax.mail . 首先下载commons- ...

  4. Apache Commons Email 使用网易企业邮箱发送邮件

    最近使用HtmlEmail 发送邮件,使用网易企业邮箱,发送邮件,死活发不出去!原以为是网易企业邮箱,不支持发送邮箱,后面经过研究发现,是apache htmlEmail 的协议导致,apache E ...

  5. Apache Commons 工具类介绍及简单使用

    转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下 ...

  6. Apache Commons 工具类简单使用

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  7. Apache Commons 工具集介绍

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  8. Apache Commons 工具类介绍及简单使用(转载)

    原文链接 http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动 ...

  9. Commons Email使用

    Apache Commons Email Apache的一个开源项目,是基于另一个开源项目Java Mail上进行封装的,使用起来更加简单方便: http://commons.apache.org/p ...

随机推荐

  1. 永久激活idea

    申明:本教程 IntelliJ IDEA 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除. idea版本为老版本2018版本,下载地址如下,激活方法和插件在压缩包中 ...

  2. linux高级管理第十四章--kvm虚拟化

    案例 安装kvm所需软件 验证 注:虚拟机要开启虚拟引擎 开启服务 环境准备 安装相关软件包 启动 创建网桥 重启,reboot 安装虚拟机 完成.

  3. [安卓基础] 006.打开另一个Activity

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. [wordpress使用]002_主题

    使用WordPress作为博客内容管理系统有一个很大的好处是,WordPress拥有大量的优秀的免费模板.你所需要的是下载安装,和稍作修改.下面接着开始WordPress教程:WordPress主题 ...

  5. pandas如何逐行需改DataFrame

    逐行修改DataFrame而不会报SettingwithCopyWarning警告的方法: df.iloc[行数,df.columns.get_loc(列名)]=new_value 参考:https: ...

  6. Rocket - config - Parameters

    https://mp.weixin.qq.com/s/uLEr9gAFaMDIXa8S9xJVTw   介绍配置类Parameters及其伴生对象的实现.   参考链接: https://docs.q ...

  7. Spring Cloud Ribbon 客户端负载均衡

    Ribbon客户端组件提供一系列完善的配置选项,比如连接超时.重试.重试算法等,内置可插拔.可定制的负载均衡组件.下面是用到的一些负载均衡策略: 简单轮询负载均衡 加权轮询负载均衡 区域感知轮询负载均 ...

  8. java实现 历届试题 蓝桥杯 打印十字图

    历届试题 打印十字图 题目描述 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示(可参见p1.jpg) 对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数. 为了能 ...

  9. (Java实现) 洛谷 P1012 拼数

    题目描述 设有nn个正整数(n≤20)(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3n=3时,3个整数13,312,343联接成的最大整数为:3433121334331213 ...

  10. Java实现 LeetCode 437 路径总和 III(三)

    437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点 ...