博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ME - Mail】之Apache Commons Email
阅读量:7009 次
发布时间:2019-06-28

本文共 2858 字,大约阅读时间需要 9 分钟。

hot3.png

Apache Commons Email

参考示例
1. pom.xml
org.apache.commons
commons-email
1.5
2. 编辑:mail.properties文件中相关值
mail.smtp.user=mail.smtp.from=mail.smtp.password=
3. 邮箱属性配置明细
4. 邮箱乱码设置
updateContentType("text/html; charset=utf-8");
4. 测试用例
package net.liuzd.java.mail;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import org.apache.commons.io.IOUtils;import org.apache.commons.mail.EmailException;import org.junit.Assert;import org.junit.Test;public class TestMailActuator {    static String to = "davidliuzd@sina.com";    private File toFile() throws IOException {        InputStream is = TestMailActuator.class.getClassLoader().getResourceAsStream("\\static\\images\\demo.png");        File file = new File("src\\test\\resources\\static\\images\\demo_tmp.png");        if (!file.exists()) {            file.createNewFile();        }        try (OutputStream outputStream = new FileOutputStream(file)) {            IOUtils.copy(is, outputStream);        } catch (FileNotFoundException e) {            // handle exception here        } catch (IOException e) {            // handle exception here        }        return file;    }    private URL toURL() throws MalformedURLException {        return new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4");    }    @Test    public void simpleEmail() throws EmailException {        Actuator.get().subject("文本邮件").setMsg("您好!这是纯文本邮件哟!").addTo(to).send();        Assert.assertTrue(true);    }    @Test    public void attacheFileEmail() throws EmailException, IOException {        Actuator.get().subject("Html邮件+附件").attach(toFile(),"我的文件").setMsg("这是Html邮件,来个红色").addTo(to).send();        Assert.assertTrue(true);    }        @Test    public void attacheUrlEmail() throws EmailException, IOException {        Actuator.get().subject("Html邮件+URl附件").attach(toURL(),"我的文件").setMsg("这是Html邮件,来个红色").addTo(to).send();        Assert.assertTrue(true);    }        @Test    public void attacheImgEmail() throws EmailException, IOException {        String htmlMsg = "这是Html邮件,来个红色 The apache logo - ";                String url = "https://www.apache.org/images/asf_logo_wide.gif";        Actuator.get().subject("Html邮件+图片附件").setMsg(htmlMsg, url, "apache logo").addTo(to).send();        Assert.assertTrue(true);    }        @Test    public void urlImgEmail() throws EmailException, IOException {        String htmlMsg = "

您好!

这是Html邮件,来个红色"; String url ="http://www.apache.org"; Actuator.get().subject("Html邮件+图片URL").setMsg(htmlMsg, url).addTo(to).send(); Assert.assertTrue(true); }}

转载于:https://my.oschina.net/liuzidong/blog/3049410

你可能感兴趣的文章
如何检查一个单向链表上是否有环?
查看>>
spring核心点总结
查看>>
解决:Determining IP Information for eth0...问题
查看>>
使用虚拟机安装 Linux 操作系统
查看>>
Spring mvc中@RequestMapping 6个基本用法小结
查看>>
使用Chrome 浏览器调试移动端网页 chrome://inspect/#devices
查看>>
Windows10下安装spacemacs
查看>>
ELK平台介绍
查看>>
我的推理逻辑设计图
查看>>
关于PHP通过PDO用中文条件查询MySQL的问题。
查看>>
算法 - 理论基础
查看>>
OSChina 技术周刊第二十四期 —— C# 6.0 的新特性
查看>>
Netty NIO 框架性能压测 – 长链接
查看>>
Quartz2.2.1简单使用
查看>>
scrapy在spider中通过pipeline获取数据库内容
查看>>
spring 抽象类注入方式
查看>>
android:dataPath解析
查看>>
AVYAY交换机参考,CM常见命令中文解释
查看>>
angular ui-bootstrap datepicker第二次点击没有显示时间选择 解决方案
查看>>
zhuce
查看>>