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 提供了对于 ...
随机推荐
- Tomcat7调试运行环境搭建与源代码分析入门
1. 需要准备好下面这些工具 JDK 1.6+ Maven 2或3 TortoiseSVN 1.7+ (从1.7开始”.svn”目录集中放在一处了,不再每个目录下都放一份) Eclipse 3.5+ ...
- ipmitool sdr type Temperature sdr 从传感器获取某一类数据
1.1.1 常用监控命令总结.ipmitool sdr type Temperature 硬件监控 yum install OpenIPMI ipmitool ipmitool sdr type T ...
- struts2 OGNL ValueStack概念理解 # % $ 的区别
http://blog.csdn.net/tjujacob/article/details/8117659 —————————————————————————————————————————————— ...
- 谷歌字体(Google Font)初探 [翻译自Google官方文档]
这个指南解释了如何使用Google Font的API,把网络字体添加到自己的页面上.你不需要任何的编码,你所要做的只是添加一个特定的CSS到HTML页面上,然后把字体关联到这个CSS样式. 一个快速的 ...
- android onTouchEvent
触摸屏幕时,没搞懂每个事件的启动顺序.本文记录onTouchEvent发生时,每个事件启动的顺序. 测试代码 @Override public boolean onTouchEvent(MotionE ...
- 【UVa】Headmaster's Headache(状压dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【BZOJ】1647: [Usaco2007 Open]Fliptile 翻格子游戏(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1647 自己太弱...看题解.. 竟然是枚举第一行的放法,,,因为一定要全部变0,所以将前一行1的在这 ...
- jQuery的end()方法使用详解
end()方法的定义和用法: end()方法能够回到最近的一个"破坏性"操作之前,即将匹配的元素列表变为前一次的状态.如果没有破坏性操作将返回一个空集.破坏性操作的概念:指任何改变 ...
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题
一.ASP.NET MVC中使用OutputCache实现服务器端页面级缓存 在ASP.NET MVC中,假如我们想要将某个页面(即某个Action)缓存在服务器端,可以在Action上标上以下特性: ...
- Libcap的简介及安装
Libpcap 简介 libpcap 是unix/linux 平台下的网络数据包捕获函数包, 大多数网络监控软件都以它为基础. Libpcap 可以在绝大多数类unix 平台下工作. Libpcap ...