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 提供了对于 ...
随机推荐
- 解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题
症状: Windows操作系统,eclipse开发环境下,在本机上使用http://localhost:8080/...访问本机上的页面,使用tomcat作为服务器 在Servlet或者Action中 ...
- [android] AndroidManifest.xml - 【 manifest -> application】
语法: <application android:allowTaskReparenting=["true" | "false"] android:back ...
- C++ 类中的静态成员变量,静态成员函数
//类中的静态成员变量,静态成员函数 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; /* ...
- selenium测试环境搭建(一)
selenium测试环境搭建 下载资源 1. selenium-java-2.53.0.zip 下载地址:http://pan.baidu.com/s/1dFDf27Z 2. Firefox Set ...
- Differential Geometry之第十章极小曲面
第十章.极小曲面 1.极小图 Animation showing the deformation of a helicoid into a catenoid. Animation of Scherk' ...
- CListBox自动换行显示
需要在ListBox控件中显示一些信息.为方便查看,不使用水平滚动条.当要输出的字符串占用的宽度超过ListBox的宽度时,截断字符串,剩余的在下一行显示. 1. 计算ListBox所占的宽度,用Ge ...
- 如今就报名上海ORACLE用户组2014年高峰论坛吧!
COLLABORATE 14 – SHOUG Forum 上海ORACLE用户组2014年高峰论坛报名本次活动由ORACLE ACS高级服务部门与 SHOUG 上海ORACLE用户组合办. 大会议程包 ...
- iOS自动化测试需求实现(iOS按键精灵类似)
需求分析: 作为以需求为驱动的IT公司,有再奇怪的需求都不奇怪,所以“24小时循测第三方应用”这样的需求也可以接受.业务需求重点为: 1.24小时循测 2.无人值守,自动完成 3.自动界面操作(点击. ...
- 在静态工具类中需要注入mapper
在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" ini ...
- JqGrid 获取所有数据
jqGrid使用本地数据时,当jqGrid配置的rowNum小于本地总数据量(records属性记录总数据,可以通过records获取到本地总数据量),调用getRowData方法获取到的只是显示的部 ...