SpringBoot框架发送邮件

一、引入jar

  org.springframework.boot

  spring-boot-starter-mail

二、添加mail配置

  # 邮箱
spring:
  mail:
    host: smtp.126.com
    password: GJZKGIOYUGWDZMUF
    username: [email protected]
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

 三、简单的发送邮件


public void sendSimpleMail(){

    SimpleMailMessage message = new SimpleMailMessage();
    message.setSubject("这是测试邮件");
    message.setText("这是正文");
    message.setTo("[email protected]");
    message.setFrom("[email protected]");
    javaMailSender.send(message);


}

四、发送带有附件的

	
public void sendMimeMail(){

    MimeMessage message = javaMailSender.createMimeMessage();
	message.addHeader("X-Mailer",
					"Microsoft Outlook Express 6.00.2900.2869");//防止发送到垃圾邮箱
	MimeMessageHelper helper = new MimeMessageHelper(message, true);
	helper.setFrom("发送方Mail");
	helper.setSubject("激光码信息");
	helper.setText("激光码信息");
	FileSystemResource ss = new FileSystemResource(new File("C:/jgm.xlsx"));
	helper.addAttachment("激光码信息.xlsx", ss);
	helper.setTo("接收方Mail");
    javaMailSender.send(message);


}

 

你可能感兴趣的:(javaUtils)