• SpringBoot慕课学习-SpringBoot发送邮件


    1. 两种基本邮件传输协议:STMP协议、POP3协议

    2. 发展的两种协议 IMAP协议、Mime协议

    文本、图片、附件、HTML邮件、邮件模板、异常处理、

    1. 引入依赖pom.xml

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>

    2.  配置application.yml

    配置用户名和密码;  QQ邮箱密码需要获取授权码,不是真实QQ密码

    spring: 
        mail: 
            username: yourMail@163.com
            password: xxxxx
         host: smtp.163.com

    3. 自动注入

    @Autowired
    JavaMailSenderImpl mailSender;
    

     4. java代码

    @Service
    public class MailService {
    
        private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Value("${spring.mail.username}")
        private String from;
    
        @Autowired
        private JavaMailSender mailSender;
    
        public void sysHello() {
            System.out.println("Hello world");
    
        }
    
        public void sendSimpleMail(String to, String subject, String content) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            message.setFrom(from);
            mailSender.send(message);
        }
    
        public void sendHtmlMail(String to, String subject, String content) {
    
            logger.info("发送HTML邮件开始:{},{},{}",to,subject,content);
            try {
                MimeMessage message = mailSender.createMimeMessage();
    
                MimeMessageHelper helper = null;
                helper = new MimeMessageHelper(message, true);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(content, true);
                helper.setFrom(from);
                mailSender.send(message);
                logger.error("发送HTML邮件成功");
            } catch (MessagingException e) {
                logger.error("发送HTML邮件失败",e);
            }
    
    
        }
    
        public void sendAttachmentMail(String to, String subject, String content, String filePath) throws MessagingException {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            //文件
            FileSystemResource file = new FileSystemResource(new File(filePath));
    
            String fileName = file.getFilename();
            helper.addAttachment(fileName, file);
            //将filePath作为数组进行遍历发送多个附件
            helper.addAttachment(fileName + "2", file);
            mailSender.send(message);
    
        }
    
        /**
         * 发送静态图片
         *
         * @param to
         * @param subject
         * @param content
         * @param rscPath
         * @param rscId
         * @throws MessagingException
         */
        public void sendInlinResourceMail(String to, String subject, String content,
                                          String rscPath, String rscId) throws MessagingException {
            //1. 创建MimeMessage实例
            MimeMessage message = mailSender.createMimeMessage();
            //2. 创建MimeMessageHelper实例
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
            //发件人
            helper.setFrom(from);
    
            helper.setTo(to);
            helper.setSubject(subject);
            // true/false是否为html
            helper.setText(content, true);
    
            //文件
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            //添加
            helper.addInline(rscId, res);
    //        helper.addInline(rscId,res);
            mailSender.send(message);
        }
    }

    邮件自动配置

    MailSenderAutoConfiguration

  • 相关阅读:
    STL函数对象和Lambda表达式
    STL算法之排序算法
    STL 算法
    STL源码剖析---根据最新版本的g++4.9.0(支持C++11)的修订(1)空间配置器
    STL Iterator的里里外外(一)?
    STL对比解说——关联容器
    Select单进程非阻塞TCP echo服务器
    TCP建立(3次握手)与终止(4次挥手)
    TIME_WAIT状态
    C标准I/O库
  • 原文地址:https://www.cnblogs.com/bigorang/p/9644430.html
Copyright © 2020-2023  润新知