Spring Boot 2.X: Mail Service

Posted by dcro2 on Thu, 31 Oct 2019 07:13:59 +0100

Preface

E-mail service is very common in the development, such as using e-mail to register an account, using e-mail as a way to retrieve password, using for subscription content regular e-mail push and so on. The following is a brief introduction of e-mail implementation.

Get ready

A mailbox for sending. This article uses Tencent's domain name mailbox. You can create a domain name binding by yourself. The login address is: http://domain.mail.qq.com

Mail service implementation

1. Add dependency

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

2. Configuration file

spring.mail.host=smtp.qq.com
spring.mail.username=zwqh@clover1314.com
spring.mail.password=*****
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3. Implementation example

@Service
public class MailTool {
	
	@Value("${spring.mail.username}")
	private String from;

	@Autowired
	private JavaMailSender mailSender;

	/**
	 * Send mail
	 * 
	 * @return
	 */
	public boolean send() {
		try {
			SimpleMailMessage message = new SimpleMailMessage();
			message.setFrom(from);//sender
			message.setTo("zwqh@clover1314.com");//recipient
			message.setCc("sohuniuer@sina.com");// CC
			message.setSubject("Mail theme"); //Mail theme
			message.setText("Here is the content of the email");//Mail content
			mailSender.send(message);
			System.out.println("Mail sent successfully");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Send rich text message
	 * 
	 * @return
	 */
	public boolean sendHtml() {
		// Use java mail's MimeMessage to support more complex mail formats and content
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		try {
			// Create MimeMessageHelper object and process MimeMessage helper class
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			// Use the auxiliary class MimeMessage to set parameters
			helper.setFrom(from);
			helper.setTo("zwqh@clover1314.com");
			helper.setBcc("sohuniuer@sina.com");//bcc
			helper.setSubject("Rich text message subject");
			helper.setText("<h1>This is the rich text message content title</h1><p style='color:red;'>This is paragraph one</p><p style='color:orange;'>This is paragraph two</p>",
					true);		
			mailSender.send(mimeMessage);
			System.out.println("Mail sent successfully");
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * Send a rich text message with attachments
	 * @return
	 */
	public boolean sendHtmlWithAttach() {
		// Use java mail's MimeMessage to support more complex mail formats and content
		MimeMessage mimeMessage = mailSender.createMimeMessage();

		try {
			// Create MimeMessageHelper object and process MimeMessage helper class
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);
			helper.setTo("zwqh@clover1314.com");
			helper.setSubject("Rich text message subject with attachment");
			helper.setText("<h1>This is the rich text message content title</h1><p style='color:red;'>This is paragraph one</p><p style='color:orange;'>This is paragraph two</p>",
					true);
			//Load file resource as attachment
			ClassPathResource file=new ClassPathResource("static/avatar2.jpg");
			//Add attachments and rename
			helper.addAttachment("Enclosure.jpg", file);
			mailSender.send(mimeMessage);
			System.out.println("Mail sent successfully");
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}

	}

}

4.Controller for testing

@RestController
public class MailController {

	@Autowired
	private MailTool mailTool;
	
	@RequestMapping("/send")
	public String send() {
		mailTool.send();
		return "send success";
	}
	
	@RequestMapping("/sendHtml")
	public String sendHtml() {
		mailTool.sendHtml();
		return "sendHtml success";
	}
	
	@RequestMapping("/sendHtmlWithAttach")
	public String sendHtmlWithAttach() {
		mailTool.sendHtmlWithAttach();
		return "sendHtmlWithAttach success";
	}
	
	
}

5. Test effect

Sample code

github

Code cloud

Unless otherwise specified, the copyright of this article belongs to Foggy and cold All, reprint please indicate the source.

Original title: spring boot 2. X (XIII): Mail Service

Original address: https://www.zwqh.top/article/info/22

If the article is helpful to you, please scan the code and pay attention to my public address.

Topics: Programming Spring Java github