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使用的更多相关文章

  1. 使用Apache Commons Email 发生邮件

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

  2. 使用Commons Email发送邮件

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

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

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

  4. 使用Apache commons email发送邮件

    今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包. 据说这个包是对javamail进行了封装,简化了操作. 这里讲一下具体用法吧 一.首先你需要有邮箱账号和一 ...

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

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

  6. Sending e-mail

    E-mail functionality uses the Apache Commons Email library under the hood. You can use theplay.libs. ...

  7. 编写更少量的代码:使用apache commons工具类库

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

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

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

  9. Apache Commons 工具类简单使用

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

随机推荐

  1. 【Spring实战】—— 3 使用facotry-method创建单例Bean总结

    如果有这样的需求: 1 不想再bean.xml加载的时候实例化bean,而是想把加载bean.xml与实例化对象分离. 2 实现单例的bean 以上的情况,都可以通过工厂方法factory-metho ...

  2. 从文件/文件流的头字节中得到mime信息

    在写网络爬虫的时候,需要根据链接来获取文件类型,将内容正确存储.之前我都是根据链接的后缀来判断的,比如: http://img12.360buyimg.com/da/20120330/88_31_Zy ...

  3. eclipse启动无响应,老是加载不了revert resources,或停留在Loading workbench状态

    做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了.启动画面始终停留在Loading workbench状态.反复重启,状态依旧. 多数情况下,应该是非正常 ...

  4. JavaScript语言精粹读书笔记- JavaScript对象

    JavaScript 对象 除了数字.字符串.布尔值.null.undefined(都不可变)这5种简单类型,其他都是对象. JavaScript中的对象是可变的键控集合(keyed collecti ...

  5. [android] AndroidManifest.xml - 【 manifest -> application】

    语法: <application android:allowTaskReparenting=["true" | "false"] android:back ...

  6. PHPMailer发送邮箱(ThinkPHP实战篇)

    1.下载phpmailer文件库 2.引用文件,此处将代码放到 :函数库中,function.php function sendConsultantMessage($sendData){ Vendor ...

  7. Machine Learning With Spark学习笔记(在10万电影数据上训练、使用推荐模型)

    我们如今開始训练模型,还输入參数例如以下: rank:ALS中因子的个数.通常来说越大越好,可是对内存占用率有直接影响,通常rank在10到200之间. iterations:迭代次数,每次迭代都会降 ...

  8. 《随机出题软件》&《随机分队软件》源码(Windows API)

    1 引言 1.1 编写目的: 为了对院级活动<最强大脑>提供软件支持,同时为了练习使用windows API. 1.2 项目背景: 来自计算机学院学生会信息部指派的任务,规定时间完成软件的 ...

  9. hdu 1520(简单树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...

  10. Nuget包制作最佳解决方案

    https://www.cnblogs.com/drea/p/8418717.html 最近研究ABP框架,下载其全套源码,想“据为己有”,这样添加功能或者修改源码的话就非常方便了,否则搭建项目直接用 ...