前言
作者是魔王不造反/biezhi ,他有很多开源项目,比如Tale个人博客,感兴趣的小伙伴可以看下。 这玩意很小,但是很好用,用法也很简单。对常用邮箱进行封装,比如163邮箱,qq邮箱。
- 简洁的邮件发送API
- 支持自定义发件人昵称
- 支持扩展邮件Message
- 支持抄送/HTML/附件
- 支持异步发送
- 支持邮件模板
- 可能是代码量最小的库了,200多行 非常好维护
使用方法
github地址 oh-my-email 基本用法介绍里面都有说明,看一下就知道怎么用了。
package io.github.biezhi.ome;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import jetbrick.template.JetEngine;
import jetbrick.template.JetTemplate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import static io.github.biezhi.ome.OhMyEmail.SMTP_ENT_QQ;
import static io.github.biezhi.ome.OhMyEmail.SMTP_QQ;
/**
* 发送邮件测试
*
* @author biezhi
* 2017/5/30
*/
public class OhMyEmailTest {
// 该邮箱修改为你需要测试的邮箱地址
private static final String TO_EMAIL = "xiaojiejie@gmail.com";
@Before
public void before() {
// 配置,一次即可
OhMyEmail.config(SMTP_QQ(false), "xxx@qq.com", "*******");
// 如果是企业邮箱则使用下面配置
OhMyEmail.config(SMTP_ENT_QQ(false), "xxx@qq.com", "*******");
}
@Test
public void testSendText() throws SendMailException {
OhMyEmail.subject("这是一封测试TEXT邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.text("信件内容")
.send();
Assert.assertTrue(true);
}
@Test
public void testSendHtml() throws SendMailException {
OhMyEmail.subject("这是一封测试HTML邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.html("<h1 font=red>信件内容</h1>")
.send();
Assert.assertTrue(true);
}
@Test
public void testSendAttach() throws SendMailException {
OhMyEmail.subject("这是一封测试附件邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.html("<h1 font=red>信件内容</h1>")
.attach(new File("/Users/biezhi/Downloads/hello.jpeg"), "测试图片.jpeg")
.send();
Assert.assertTrue(true);
}
@Test
public void testSendAttachURL() throws SendMailException, MalformedURLException {
OhMyEmail.subject("这是一封测试网络资源作为附件的邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.html("<h1 font=red>信件内容</h1>")
.attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "测试图片.jpeg")
.send();
Assert.assertTrue(true);
}
@Test
public void testPebble() throws IOException, PebbleException, SendMailException {
PebbleEngine engine = new PebbleEngine.Builder().build();
PebbleTemplate compiledTemplate = engine.getTemplate("register.html");
Map<String, Object> context = new HashMap<String, Object>();
context.put("username", "biezhi");
context.put("email", "admin@biezhi.me");
Writer writer = new StringWriter();
compiledTemplate.evaluate(writer, context);
String output = writer.toString();
System.out.println(output);
OhMyEmail.subject("这是一封测试Pebble模板邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.html(output)
.send();
Assert.assertTrue(true);
}
@Test
public void testJetx() throws SendMailException {
JetEngine engine = JetEngine.create();
JetTemplate template = engine.getTemplate("/register.jetx");
Map<String, Object> context = new HashMap<String, Object>();
context.put("username", "biezhi");
context.put("email", "admin@biezhi.me");
context.put("url", "<a href='http://biezhi.me'>https://biezhi.me/active/asdkjajdasjdkaweoi</a>");
StringWriter writer = new StringWriter();
template.render(context, writer);
String output = writer.toString();
System.out.println(output);
OhMyEmail.subject("这是一封测试Jetx模板邮件")
.from("小姐姐的邮箱")
.to(TO_EMAIL)
.html(output)
.send();
Assert.assertTrue(true);
}
}
maven坐标
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>oh-my-email</artifactId>
<version>0.0.4</version>
</dependency>
注意事项
- password尽量使用授权码,尽管有的邮箱用密码也可以,遇到问题有时候与这个有关
- 还有就是主题,有的有些可能限制主题字数不能太少,或者其它。使用邮件遇到问题可以排查下。
- 默认端口为645(因为很多云服务器默认屏蔽25,开启还有点麻烦)
- 重点是cc方法是抄送,bcc是密送。
本文由 止戈 创作,采用 知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jan 4,2020