Springboot mail邮件发送

需求:

      最近公司新上线一款产品,业务提出需求,要求发送邮件统计每个小时的销量情况及埋点分析数据以附件excel形式发送,并且邮件正文要显示销量表格。

开发点:

  1. 邮件内容要包含附件
  2. 邮件正文要写成表格

对于邮件发送功能,SpringBoot默认也帮助我们集成好,只需要引入如下第三方依赖即可:

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

 了解下邮件

     发送方通过自己的用户密码登陆自己的邮箱,编写邮件推到自己邮箱所在的邮件服务器,在由邮件服务器发送到收件人对应的邮件服务器上,收件人登陆邮箱,获取邮件服务器的邮件进行阅读处理。

发送邮件:

  1. SMTP:简单邮件传输协议,发送纯文本内容的邮件协议
  2. MIME:多用途互联网邮件扩展,发送图片,视频,音频,文件,附件扩展协议

收件:

  1. POP:邮局协议,接收电子邮件
  2. 交互邮件访问协议,直接从服务器上拉取邮件

spring-boot-starter-mail 已经帮我们写好了配置类 :MailSenderAutoConfiguration

//MailSenderAutoConfiguration
@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import(JndiSessionConfiguration.class)
public class MailSenderAutoConfiguration {
 
    private final MailProperties properties;
 
	private final Session session;
 
	public MailSenderAutoConfiguration(MailProperties properties,
			ObjectProvider<Session> session) {
		this.properties = properties;
		this.session = session.getIfAvailable();
	}
 
	@Bean
	public JavaMailSenderImpl mailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		if (this.session != null) {
			sender.setSession(this.session);
		}
		else {
			applyProperties(sender);
		}
		return sender;
	}
    
    //other code...
}

      以及发送邮件配置对象:MailProperties(想想你用qq邮件发送邮件,都需要什么:邮箱账号,密码...)

扫描二维码关注公众号,回复: 8731351 查看本文章
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {
 
	private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
 
	/**
	 * SMTP server host.
	 */
	private String host;
 
	/**
	 * SMTP server port.
	 */
	private Integer port;
 
	/**
	 * Login user of the SMTP server.
	 */
	private String username;
 
	/**
	 * Login password of the SMTP server.
	 */
	private String password;
 
	/**
	 * Protocol used by the SMTP server.
	 */
	private String protocol = "smtp";
 
	/**
	 * Default MimeMessage encoding.
	 */
	private Charset defaultEncoding = DEFAULT_CHARSET;
 
	/**
	 * Additional JavaMail session properties.
	 */
	private Map<String, String> properties = new HashMap<String, String>();
 
	/**
	 * Session JNDI name. When set, takes precedence to others mail settings.
	 */
	private String jndiName;
 
	/**
	 * Test that the mail server is available on startup.
	 */
	private boolean testConnection;
    
    //other code...
    
}

MailSenderAutoConfiguration 配置类配置了一个bean:

  1. JavaMailSenderImpl实现邮件发对象,是JavaMailSender接口的实现类,只要注入JavaMailSender,就可以通send()方法发送邮件

邮件内容形式分两种:1.纯文本的简单邮件 2.非纯文本的复杂邮件。因此springboot也给我们提供了两个对象:

  1. SimpleMailMessage:纯文本内容邮件对象
  2. MimeMessage:复杂内容邮件对象

开始说的需求,我们的邮件既包含表格内容,又包含附件表格,就可以确定我们要发的是一个复杂内容的邮件

1.配置发送邮件需要的参数

spring:
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: [email protected] #QQ邮箱
    password: xxxxxxxxxxx #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口号465或587
    properties.mail.display.sendmail: Javen #可以任意
    properties.mail.display.sendname: Spring Boot Guide Email #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
    from: [email protected] #与上面的username保持一致

注意:这里的password不是你邮箱的密码,就是如下图你通过POP3生成的第三方登陆的授权密码,切记!

根据平常发送邮件的内容大致定义接口

  1. 纯文本内容邮件内容接口
  2. 页面内容邮件内容接口
  3. 带附件邮件内容接口
  4. 页面及附件邮件内容接口

对于发送类似表格内容,我们可以通过,模板引擎来完成,写入一个提前准备好的html模板,引入模板依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
发布了38 篇原创文章 · 获赞 1 · 访问量 1047

猜你喜欢

转载自blog.csdn.net/yu13843271857/article/details/103450899