Java项目使用oh-my-email发送邮件

本文使用Github开源项目oh-my-email进行测试邮件发送,并未进行更为深度的测试,如果想要快速使用,的确是一个很好的邮件发送组件。https://github.com/biezhi/oh-my-email

oh-my-email仓库地址

<dependency>
    <groupId>io.github.biezhi</groupId>
    <artifactId>oh-my-email</artifactId>
    <version>0.0.4</version>
</dependency>

配置oh-my-email

    // 配置一次即可,可以配置为静态方法
    OhMyEmail.config(SMTP_QQ(false), "xxxx@qq.com", "your@password");

发送Email

测试发送text邮件

@Test
public void testSendText() throws MessagingException {
    OhMyEmail.subject("这是一封测试TEXT邮件")
            .from("小姐姐的邮箱")
            .to("xxxx@gmail.com")
            .text("信件内容")
            .send();
}

测试发送html邮件

@Test
public void testSendHtml() throws MessagingException {
    OhMyEmail.subject("这是一封测试HTML邮件")
            .from("小姐姐的邮箱")
            .to("xxxx@gmail.com")
            .html("<h1 font=red>信件内容</h1>")
            .send();
}

测试发送附件邮件

@Test
public void testSendHtml() throws MessagingException {
    OhMyEmail.subject("这是一封测试HTML邮件")
            .from("小姐姐的邮箱")
            .to("xxxx@gmail.com")
            .html("<h1 font=red>信件内容</h1>")
            .send();
}

测试发送网络资源附件邮件

@Test
public void testSendAttachURL() throws MessagingException {
    try {
        OhMyEmail.subject("这是一封测试网络资源作为附件的邮件")
                .from("小姐姐的邮箱")
                .to("xxxx@gmail.com")
                .html("<h1 font=red>信件内容</h1>")
                .attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "测试图片.jpeg")
                .send();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}
posted @ 2018-10-25 15:50  jimisun  阅读(1411)  评论(1编辑  收藏  举报
豫ICP备17005230号-3