content: package com.wenmrong.community1.community.service;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.core.io.FileSystemResource;
  import org.springframework.mail.javamail.JavaMailSenderImpl;
  import org.springframework.mail.javamail.MimeMessageHelper;
  import org.springframework.stereotype.Service;
  
  import javax.mail.MessagingException;
  import javax.mail.internet.MimeMessage;
  import java.io.File;
  
  @Service
  public class MailService {
      private final Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @Autowired
      JavaMailSenderImpl mailSender;
  
      @Value("${spring.mail.username}")
      private String from;
  
  
      public void sendMimeMail(String to, String subject, String content) {
          MimeMessage message = mailSender.createMimeMessage();
          MimeMessageHelper helper = null;
          try {
              helper = new MimeMessageHelper(message, true);
              helper.setFrom(from);
              helper.setSubject(subject);
              helper.setTo(to);
              helper.setText(content, true);
              mailSender.send(message);
              //日志信息
              logger.info("邮件已经发送。");
          } catch (MessagingException e) {
              logger.error("发送邮件时发生异常！", e);
          }
  
      }
  
  
      public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
          MimeMessage message = mailSender.createMimeMessage();
          MimeMessageHelper helper = null;
  
          try {
              helper = new MimeMessageHelper(message, true);
              helper.setFrom(from);
              helper.setSubject(subject);
              helper.setTo(to);
              helper.setText(content, true);
  
              FileSystemResource fs = new FileSystemResource(new File(rscPath));
              helper.addInline(rscId, fs);
  
              mailSender.send(message);
          } catch (MessagingException e) {
              logger.error("发送邮件时发生异常！", e);
          }
      }
  
  
  }
  