Spring-Mail

宗意蕴
2023-12-01

基本了解

spring为发送邮件提供了一个抽象层: 定义了MailMessage 和 MailSender抽象 来描述邮件消息和邮件发送者

# 消息
MailMessage: 有两个实现类: SimpleMainMessage(text格式) 和 MimeMailMessage(多用途)
setTo(String to) 发送地址,可以设置多个
setFrom(String from) 发送方
setCc(String c) 抄送
SetSubject(String subject) 主题
setText(String text) 邮件内容

MimeMessageHelper: 简化对mimeMessage的封装

# 发送
JavaMailSender抽象类:
最常用的方法: send(message)

可以创建MimeMessage:
createMimeMessage()

JavaMailSenderImpl: 常用的属性:
host:
port:
protocol: 默认STMP
username: 用户名
password: 
defaultEncoding: 编码

发送纯文本邮件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    public static void main(String[] args) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtphz.qiye.163.com");
        //sender.setPort(994);
        sender.setUsername("ops@mxx.com");
        sender.setPassword("xxxx");
        sender.setDefaultEncoding("UTF-8");

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("oxx@mxx.com");
        message.setTo("x2@163.com","xx@163.com");
        message.setSubject("验证码发送");
        message.setText("984536");
        // 抄送
        message.setCc("xxx@xx.com");
        // 发送邮件
        sender.send(message);
    }

发送HTML邮件

    public static void main(String[] args) throws MessagingException {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtphz.qiye.163.com");
        //sender.setPort(994);
        sender.setUsername("ops@cc.com");
        sender.setPassword("xxx");
        sender.setDefaultEncoding("UTF-8");

        MimeMessage message = sender.createMimeMessage();
        // 简化对mimeMessage的封装
        MimeMessageHelper helper = new MimeMessageHelper(message, false, "UTF-8");

        helper.setFrom("ops@mfdfgd.com");
        helper.setCc("dff@mfdf.com");
        helper.setTo(new String[]{"fdf@163.com", "fdfer@163.com"});
        helper.setSubject("HTML邮件");
        helper.setText("<h1>hello,world!</h1>",true);

        sender.send(message);
    }

发送内嵌资源的邮件

    public static void main(String[] args) throws MessagingException {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtphz.qiye.163.com");
        //sender.setPort(994);
        sender.setUsername("ops@dfg.com");
        sender.setPassword("ddx");
        sender.setDefaultEncoding("UTF-8");

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

        helper.setFrom("ops@meifute.com");
        helper.setCc("ddd@dddd.com");
        helper.setTo(new String[]{"dd@163.com", "ddd@163.com"});
        helper.setSubject("HTML邮件");
        // 内嵌文件的id已特定的标志引用 格式为cid:<内嵌文件id>
        helper.setText("<html><body>hello<img src='cid:id1'/><body></html>",true);
        FileSystemResource resource = new FileSystemResource("/Users/jinxingguang/java_project/spring-boot-all/spring-boot-mail-demo/src/main/resources/tiger.jpeg");

        // 使用addInline 方法将文件内嵌到邮件中
        helper.addInline("id1",resource);
        sender.send(message);
    }

发送带附件的邮件

    // 带附件邮件
    public static void main(String[] args) throws MessagingException {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtphz.qiye.163.com");
        //sender.setPort(994);
        sender.setUsername("ops@ddf.com");
        sender.setPassword("ddd");
        sender.setDefaultEncoding("UTF-8");

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

        helper.setFrom("ga@gg.com");
        helper.setCc("ddd@fgg.com");
        helper.setTo(new String[]{"ddd@163.com", "gsfgh@163.com"});
        helper.setSubject("HTML邮件");
        // 内嵌文件的id已特定的标志引用 格式为cid:<内嵌文件id>
        helper.setText("<html><body>hello<body></html>",true);
        FileSystemResource resource = new FileSystemResource("/Users/jinxingguang/java_project/spring-boot-all/spring-boot-mail-demo/src/main/resources/tiger.jpeg");
        // 使用addAttachment 方法 添加附件
        helper.addAttachment("tiger.jpeg",resource);
        sender.send(message);
    }
 类似资料: