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 提供了对于 ...
随机推荐
- C语言 · 删除数组中的0元素
算法提高 6-9删除数组中的0元素 时间限制:1.0s 内存限制:512.0MB 编写函数CompactIntegers,删除数组中所有值为0的元素,其后元素向数组首端移动.注意,C ...
- PHP——注册页面,审核页面,登录页面:加Session和Cookie
实现效果: 用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用session和cookie获取用户信息并且不能跳过登录页面直接进入主页面 1.Session存储在服务器可 ...
- m4--宏处理器
m4 是 POSIX 标准中的一部分,所有版本的 UNIX 下都可用.虽然这种语言可以单独使用,但大多数人需要 m4 仅仅是因为 GNU autoconf 中的 “configure” 脚本依赖它.宏 ...
- zabbix的agent端的主动模式关键三个参数
如多主机超过300+和队列内容过多,就采用主动模式. [root@web03 zabbix]# egrep -v "^#|^$" zabbix_agentd.conf PidFil ...
- jQuery 插件设置cookie
对cookies的操作在当访问一个网站就无时无刻的都伴随着我们,记录着我们的一举一动,并将不危害用户隐私的信息,将以保存,这样用户就不用去从新再次操作重复的步骤,这样大大方便了客户,也增加了客户对网站 ...
- java 多线程 3 synchronized 同步
多任务编程的难点在于多任务共享资源.对于同一个进程空间中的多个线程来说,它们都共享堆中的对象.某个线程对对象的操作,将影响到其它的线程. 在多线程编程中,要尽力避免竞争条件(racing condit ...
- 【BZOJ】2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=2440 我觉得网上很多题解都没说清楚...(还是我太弱了? 首先我们可以将问题转换为判定性问题,即给出 ...
- SSH开发环境整合搭建
1.建立动态web工程,加入必要的jar包. antlr-2.7.7.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar c3p0-0.9.1.2 ...
- 表达式树在LINQ动态查询
动态构建表达式树,最佳实践版,很实用! public class FilterCollection : Collection<IList<Filter>> { public F ...
- ionic 弹窗(alert, confirm)
直接上代码吧,不解释了 控制器: angular.module('app.controllers', []) .controller('categoryCtrl', ['$scope', '$http ...