spring mail 邮件发送demo

2023-12-15 19:18:02

首先配置号邮件服务器(我用的是126邮箱),获取密钥:

进入下图的【邮箱中心】->点击右侧的齿轮按钮

接着,开启 下图位置 的 POP3/SMTP(我当时点击开启时,需要手机扫码认证下)

最后成功后,会给你弹出个授权码窗口,里面显示授权码。这个要保留,就是后面的密码。

同时,本页面底部,又服务器地址,记得备份下,后面会用:

后面就是使用 spring mail 库了。很方便的

maven 引入库:

 <!-- spring mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

给出自己整理的代码示例:

package com.example.demo;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

class SpringMailTests {


    public static void main(String[] args) throws MessagingException {

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.126.com");
        //sender.setPort(994);
        sender.setUsername("jiang3331064@126.com");
        sender.setPassword("GQHFSOBQDRKKKTIE");
        sender.setDefaultEncoding("UTF-8");

//        SimpleMailMessage message = sendText(sender);

//        MimeMessage message = sendHtml(sender);

//        MimeMessage message = sendEmbeddedResource(sender);

        MimeMessage message = sendAttachment(sender);

        // 发送
        sender.send(message);

        System.out.println("success");

    }

    /**
     * 发送文本
     *
     * @param sender
     */
    public static SimpleMailMessage sendText(JavaMailSenderImpl sender) {


        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("jiang3331064@126.com");
        message.setTo("rain.jiang@xx.com", "645504059@qq.com");
        message.setSubject("126邮件发送测试222");
        message.setText("我是个测试文本222");
        // 抄送
        message.setCc("xxx@xx.com");

        return message;
    }

    /**
     * 发送HTML邮件
     *
     * @param sender
     * @throws MessagingException
     */
    public static MimeMessage sendHtml(JavaMailSenderImpl sender) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();
        // 简化对mimeMessage的封装
        MimeMessageHelper helper = new MimeMessageHelper(message, false, "UTF-8");
        helper.setFrom("jiang3331064@126.com");
        helper.setCc("dff@mfdf.com");
        helper.setTo(new String[]{"rain.jiang@xx.com", "645504059@qq.com"});
        helper.setSubject("HTML邮件");
        helper.setText("<h1 style='color:green;'>hello,world!</h1>", true);
//
        return message;
    }


    /**
     * 发送内嵌资源的邮件
     *
     * @param sender
     * @throws MessagingException
     */
    public static MimeMessage sendEmbeddedResource(JavaMailSenderImpl sender) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();
        // 简化对mimeMessage的封装 内嵌资源要将multipart设置为true
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setFrom("jiang3331064@126.com");
//        helper.setCc("ccc@dddd.com");
        helper.setTo(new String[]{"rain.jiang@xx.com", "645504059@qq.com"});
        helper.setSubject("发送内嵌资源的邮件");
        // 内嵌文件的id已特定的标志引用 格式为cid:<内嵌文件id>
//        helper.setText("<a style='color:red;'>hello</a>",true);


        helper.setText("<html><body>hello<img src='cid:id1'/><br /><img src='cid:id2'/></body></html>", true);
        FileSystemResource resource = new FileSystemResource("C:/Users/we749/Documents/workspace/onestophealthcheck/project-server/src/main\\resources\\static\\image\\bg1.png");
        FileSystemResource resource2 = new FileSystemResource("C:\\Users\\we749\\Pictures\\bg1.png");
//
//        // 使用addInline 方法将文件内嵌到邮件中
        helper.addInline("id1", resource);
        helper.addInline("id2", resource2);

        return message;

    }

    /**
     * @param sender
     * @return
     * @throws MessagingException
     */
    public static MimeMessage sendAttachment(JavaMailSenderImpl sender) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();
        // 简化对mimeMessage的封装 内嵌资源要将multipart设置为true
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

        helper.setFrom("jiang3331064@126.com");
//        helper.setCc("ccc@dddd.com");
        helper.setTo(new String[]{"rain.jiang@xx.com", "645504059@qq.com"});

        helper.setSubject("发送带附件的邮件");
        // 内嵌文件的id已特定的标志引用 格式为cid:<内嵌文件id>
        helper.setText("<html><body>发送带附件的邮件<body></html>", true);
        FileSystemResource resource = new FileSystemResource("C:/Users/we749/Documents/workspace/onestophealthcheck/project-server/src/main\\resources\\static\\image\\bg1.png");
        // 使用addAttachment 方法 添加附件
        helper.addAttachment("tiger.jpeg", resource);

        return message;

    }
}

文章来源:https://blog.csdn.net/rainyspring4540/article/details/135022226
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。