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();
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM