• 发送包含邮件附件和图片的邮件


    package com.itheima.service;
    
    import java.io.File;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.mail.MailException;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Service;
    
    @Service
    public class SendEmailService {
    
        @Autowired
        private JavaMailSenderImpl mailSender;
    
        @Value("${spring.mail.username}")
        private String from;
    
        /**
         * 发送纯文本邮件
         * 
         * @param to      收件人地址
         * @param subject 邮件标题
         * @param text    邮件内容
         */
        public void sendSimpleEmail(String to, String subject, String text) {
            // 定制纯文本邮件信息SimpleMailMessage
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            try {
                // 发送邮件
                mailSender.send(message);
                System.out.println("纯文本邮件发送成功");
            } catch (MailException e) {
                System.out.println("纯文本邮件发送失败 " + e.getMessage());
                e.printStackTrace();
            }
        }
    
        /**
         * 发送复杂邮件(包括静态资源和附件)
         * 
         * @param to       收件人地址
         * @param subject  邮件标题
         * @param text     邮件内容
         * @param filePath 附件地址
         * @param rscId    静态资源唯一标识
         * @param rscPath  静态资源地址
         */
        public void sendComplexEmail(String to, String subject, String text, String filePath, String rscId,
                String rscPath) {
            // 定制复杂邮件信息MimeMessage
            MimeMessage message = mailSender.createMimeMessage();
            try {
                // 使用MimeMessageHelper帮助类,并设置multipart多部件使用为true
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(text, true);
                // 设置邮件静态资源
                FileSystemResource res = new FileSystemResource(new File(rscPath));
                helper.addInline(rscId, res);
                // 设置邮件附件
                FileSystemResource file = new FileSystemResource(new File(filePath));
                String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
                helper.addAttachment(fileName, file);
                // 发送邮件
                mailSender.send(message);
                System.out.println("复杂邮件发送成功");
            } catch (MessagingException e) {
                System.out.println("复杂邮件发送失败 " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    package com.itheima;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.itheima.service.SendEmailService;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class ApplicationTest {
    
        @Autowired
        private SendEmailService sendEmailService;
    
        @Test
        public void sendSimpleMailTest() {
            String to = "XXXXXXXXX@qq.com";
            String subject = "【纯文本邮件】标题";
            String text = "Spring Boot纯文本邮件发送内容测试.....";
            // 发送简单邮件
            sendEmailService.sendSimpleEmail(to, subject, text);
        }
    
        @Test
        public void sendComplexEmailTest() {
            String to = "XXXXXXX1@qq.com";
            String subject = "【复杂邮件】标题";
            // 定义邮件内容
            StringBuilder text = new StringBuilder();
            text.append("<html><head></head>");
            text.append("<body><h1>祝大家元旦快乐!</h1>");
            // cid为固定写法,rscId指定一个唯一标识
            String rscId = "img001";
            text.append("<img src='cid:" + rscId + "'/></body>");
            text.append("</html>");
            // 指定静态资源文件和附件路径
            String rscPath = "F:\\email\\newyear.jpg";
            String filePath = "F:\\email\\元旦放假注意事项.docx";
            // 发送复杂邮件
            sendEmailService.sendComplexEmail(to, subject, text.toString(), filePath, rscId, rscPath);
        }
    }
  • 相关阅读:
    rpc中的stub学习
    Centos7 远程打开火狐浏览器 控制台输入命令:firefox profilemanager
    centos7 networkmanager 与 interfaces 冲突 ,实际未成功,
    centos7如何更改系统语言为英文
    虚拟机centos7安装VMwareTools详细教程 注意命令中 #要去掉
    linux centos7系统怎么查看设置ip
    CentOS 7 虚拟机安装&网络配置&链接xshell&换源,亲测可行运行
    centos7系统的ping命令出现 www.baidu.com Name or service not known
    Centos 7下如何调用中文输入法
    centos7查看ip命令,centos7设置虚拟机ip 修改 vi/etc/sysconfig/networkscripts/ifcfgens33 配置为 yes 可显示ip地址
  • 原文地址:https://www.cnblogs.com/tszr/p/15918957.html
Copyright © 2020-2023  润新知