zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    get pc name in LAN
    study spring
    android install
    【转】Java:Session详解
    classic problem: producer and consumer
    tomcat install
    验证Monitor.PulseAll功效
    (转)Windows7的图形架构与DX的那点事
    Cannot open your default email folders. The attempt to log on to Microsoft Exchange has failed.
    Monitor.Wait初探(8)
  • 原文地址:https://www.cnblogs.com/jimisun/p/9850340.html
Copyright © 2011-2022 走看看