spring boot join mail message support

First, add dependencies

<!-- 邮件整合 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>

Second, add mail.properties profile

# Set mailbox host 
spring.mail.host = smtp.qq.com 
# Set the user name 
spring.mail.username = xxxxxxx 
# password 
#QQ Mailbox -> Settings -> Accounts -> POP3 / SMTP service: get open after the service QQ authorization code 
spring.mail.password = xxxxxxxxxxxxxxxx 
# port 
spring.mail.port = 465 
# agreement 
# spring.mail.protocol = smtp 
whether # set authentication is required, if true, then the user name and password necessary, 
# if setting false, you can not set up a user name and password, of course, depends on your butt platform supports no password access. 
= to true spring.mail.properties.mail.smtp.auth 
#STARTTLS [. 1] is an extension of the plain-text communication protocol. It provides a way to connect the upgrade to the plaintext encrypted connection (TLS or SSL), instead of using one additional port for encrypted communications. 
= to true spring.mail.properties.mail.smtp.starttls.enable 
spring.mail.properties.mail.smtp.starttls.required to true = 
spring.mail.properties.mail.smtp.socketFactory.class = in javax.net.ssl. SSLSocketFactory

Third, add MailConfig.java

package com.spring.config;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
importorg.springframework.mail.javamail.MimeMessageHelper; 

@Configuration public class MailConfig { 
	@Resource Private JavaMailSenderImpl mailSender; 
	@Value ( " $ {} spring.mail.username ")
	 Private String username; / ** 
	 * sending plain text email 
	 * 
	 * @param toEmail recipient's mailbox 
	 * @param title the message header 
	 * @param content message contents 
	 * / public void sendTextMail (toEmail String, String title, String content) { 
		SimpleMailMessage msg = new new SimpleMailMessage (); 
		msg.setFrom (username ); 
		msg.setTo (toEmail);
 

	


	
	  
		msg.setSubject (title);
		msg.setText (Content); 
		mailSender.send (MSG); 
	} / ** 
	 * transmit content with the html 
	 * 
	 * @param toEmail recipient mailbox 
	 * @param title Message header 
	 * @param htmlContent message contents 
	 * / public void sendHtmlMail (toEmail String, String title, the htmlContent String) throws MessagingException { 
		the MimeMessage mailSender.createMimeMessage MSG = (); 
		MimeMessageHelper Helper = new new MimeMessageHelper (MSG, to false , " UTF-. 8 "); 
		helper.setFrom (username); 
		helper.setTo (toEmail); 
		helper.setText (the htmlContent, to true );

	
	  
		helper.setSubject (title); 
		mailSender.send (MSG); 
	} / ** 
	 * add the sending email attachments 
	 * 
	 * @param toEmail recipient address 
	 * @param title Message header 
	 * @param content text 
	 * @param aboutFiles attachment information for each child is a map Map file related information <String, String>: 1.filePath 
	 * 2.fileName 
	 * @throws Exception abnormal 
	 * / public void sendAttachmentMail (toEmail String, String title, String Content, List <the Map < String, String >> aboutFiles) throws Exception { 
		the MimeMessage mailSender.createMimeMessage MSG = (); 
		MimeMessageHelper Helper = new new MimeMessageHelper (MSG,

	
	  to true , " UTF-. 8 ");
		helper.setFrom(username);
		helper.setTo(toEmail);
		helper.setSubject(title);
		helper.setText(content);
		FileSystemResource resource = null;
		for (Map<String, String> file : aboutFiles) {
			resource = new FileSystemResource(file.get("filePath"));
			if (resource.exists()) {// 是否存在资源
				File attachmentFile = resource.getFile();
				helper.addAttachment(file.get("fileName"), attachmentFile);
			}
		}
		mailSender.send(msg);
	}

}

Fourth, the use MailConfig

@Autowired
private MailConfig mailConfig;
MailConfig transmitted using a method which can be

Guess you like

Origin www.cnblogs.com/mowen120/p/12013290.html