Commons Email使用
Apache Commons Email
Apache的一个开源项目,是基于另一个开源项目Java Mail上进行封装的,使用起来更加简单方便:
http://commons.apache.org/proper/commons-email/index.html
首先下载jar包:commons-email-1.5.jar
activation.jar mail.jar
1.简单文本邮件发送
package com.fpc.Test; import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail; public class CommonsEmail {
public static void sendEmail() {
SimpleEmail email = new SimpleEmail();
// email.setTLS(true);
//email.setSSL(true);
email.setDebug(true);
email.setHostName("smtp.163.com");
email.setAuthenticator(new DefaultAuthenticator("15755502569@163.com","aa892475"));
try {
email.setFrom("15755502569@163.com");
email.addTo("18500408772@163.com");
email.addCc("1448433741@qq.com");
email.setCharset("GB2312");
email.setSubject("2017/11/29");
email.setMsg("看到邮件速度到会议室来开会!");
email.send();
System.out.println("邮件发送成功");
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static void main(String[] args) {
CommonsEmail.sendEmail();
}
}
注:
email.setHostName("smtp.163.com"); 协议主机
- 使用不同的服务商邮箱,这里的HostName需要改一下,同时安全校验也是不同的 setTLS,setSSL
- email.setDebug(true);开启debug模式,可以打印一些信息。
2.带附件的邮件发送
MultiPartEmail EmailAttachment
package com.fpc.Test; import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail; public class CommonsEmail {
public static void sendEmail() {
MultiPartEmail email = new MultiPartEmail();
// email.setTLS(true);
//email.setSSL(true);
email.setDebug(true);
email.setHostName("smtp.163.com");
email.setAuthenticator(new DefaultAuthenticator("15755502569@163.com","aa892475"));
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("C:\\Users\\Administrator\\Desktop\\test.xml");
attachment.setDescription(EmailAttachment.ATTACHMENT);
attachment.setDescription("test xml file");
attachment.setName("test xml");
try {
email.setFrom("15755502569@163.com");
email.addTo("18500408772@163.com");
email.addCc("1448433741@qq.com");
email.setCharset("GB2312");
email.setSubject("2017/11/29");
email.setMsg("看到邮件速度到会议室来开会!");
// email.attach(attachment);
email.attach(attachment);//添加附件
email.send();
System.out.println("邮件发送成功");
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static void main(String[] args) {
CommonsEmail.sendEmail();
}
}
Commons Email使用的更多相关文章
- 使用Apache Commons Email 发生邮件
Apache Commons Email的Maven依赖 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-e ...
- 使用Commons Email发送邮件
Commons Email是apache commons库中的一个组件,对java mail做了一些个封装,提供能为简化的API供开发者使用.它依赖于javax.mail . 首先下载commons- ...
- 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 ...
- Sending e-mail
E-mail functionality uses the Apache Commons Email library under the hood. You can use theplay.libs. ...
- 编写更少量的代码:使用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 提供了对于 ...
随机推荐
- STM32F1_常见外设资源汇总
前言 STM32F1系列芯片算是在STM32中最早的一系列,在实际生活中应用的比较广泛.因此,汇总一下STM32F1系列芯片常见片内资源,每一篇文章把重点提出来讲解,并提供软件源代码工程. 汇总常见资 ...
- LVM简介
3. 创建VG.. 7 4. 创建LV.. 9 5.LV格式化及挂载... 10 一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauel ...
- javascript 实现java中的Map
javascript实现java中的Map,代码是在国外的一个网站上看到的(http://stackoverflow.com/questions/368280/javascript-hashmap-e ...
- [Java] 高效快速导入EXCEL数据
需求1.高效率的以excel表格的方式导入多条数据.2.以身份证号为唯一标识,如果身份证号已存在,则该条数据不导入. 分析刚开始的时候是传统的做法,解析excel数据,获取单个对象,判断身份证是否已存 ...
- doAfterBody()方法是在( )接口中定义的。
A.Tag B.IterationTag C.BodyTag D.TagSupport 解答:B
- PHPMailer发送邮箱(ThinkPHP实战篇)
1.下载phpmailer文件库 2.引用文件,此处将代码放到 :函数库中,function.php function sendConsultantMessage($sendData){ Vendor ...
- jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表
对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码. 需求:url:链接 par:ID sel:下拉列表选择器 function BuildS ...
- WebStorm Cordova 环境搭建
一.软件安装 1.nodejs 2.npm 3.Cordova 如果慢,可以修改镜像,再进行安装. npm config set registry http://registry.cnpmjs.org ...
- JqGrid 获取所有数据
jqGrid使用本地数据时,当jqGrid配置的rowNum小于本地总数据量(records属性记录总数据,可以通过records获取到本地总数据量),调用getRowData方法获取到的只是显示的部 ...
- json学习之JSONArray的应用(转载)
从json数组中得到相应java数组,如果要获取java数组中的元素,只需要遍历该数组. 1 /** 2 * 从json数组中得到相应java数组 3 * JSONArray下的toArray()方法 ...