Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SpringBoot to realize QQ Mail sending email

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use SpringBoot to achieve QQ Mail to send mail", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use SpringBoot to send mail to QQ Mail"!

1. Obtain QQ Mail authorization code

two。 Import mailbox send dependent initiator

The method of customizing mail template is used to achieve general mail delivery, and Thymeleaf needs to import dependencies together to build mail template.

Org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-starter-thymeleaf 3. Configuration file yml add mail service configuration # Spring configuration spring: mail: host: smtp.qq.com username: * @ qq.com # password is the client authorization code password: * default-encoding: UTF-8 properties: mail: smtp obtained after the smtp service activated by QQ Mail in the first step Auth: true starttls: enable: true required: true#thymeleaf template engine configuration is too simple It won't be posted. Write interface IMailServicepublic interface IMailService {void sendHtmlMailThymeLeaf (String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content);} 5. Writing and implementing MailServiceImpl@Servicepublic class MailServiceImpl implements IMailService {/ * JavaMailSender is configured by Spring Boot in the MailSenderPropertiesConfiguration class, which is imported in the Mail * autoconfiguration class MailSenderAutoConfiguration, so you can use * / @ Autowired private JavaMailSender mailSender; @ Override public void sendHtmlMailThymeLeaf (String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content) {MimeMessage mimeMessage = mailSender.createMimeMessage () by injecting JavaMailSender here. Try {MimeMessageHelper mimeMessageHelper = new MimeMessageHelper (mimeMessage, true); mimeMessageHelper.setFrom (new InternetAddress (mailFromNick + ")); / / set multiple recipients String [] toAddress = mailTo.split (", "); mimeMessageHelper.setTo (toAddress); if (! StringUtils.isEmpty (cc)) {mimeMessageHelper.setCc (cc) } mimeMessageHelper.setSubject (subject); / / the second parameter is true, which means the message body is in html format. Default is false mimeMessageHelper.setText (content, true); mailSender.send (mimeMessage);} catch (MessagingException e) {System.out.println (e) } 6.Controller call / / the sender should be the same as the mailbox in the yml configuration file String mailFrom = "* @ qq.com"; / / recipient String mailTo = "* @ qq.com,*@qq.com"; / / CC (which can be empty) String cc = "* @ qq.com" / / inject mailService @ Autowired private IMailService mailService; / / inject TemplateEngine @ Autowired TemplateEngine templateEngine; @ RequestMapping ("/ other/test") / / request path @ ResponseBody public void testMail () {/ / Note 1: here I am querying the corresponding content, using a rich text editor to store the content of the html tag Strategy strategy = strategyService.selectStrategyByStrategyId (Long.valueOf (1)) Context context = new Context (); / / Guide package is org.thymeleaf.context / / Note 2: input context.setVariable ("content", strategy.getStrategyContent ()) in the thymeleaf template to get the sent content; String content = templateEngine.process ("mailTemplate.html", context); / / System.out.println (content) MailService.sendHtmlMailThymeLeaf (mailFrom, "define the sender's name", mailTo, cc, "define the message title", content); System.out.println ("email sent successfully");} 7.thymeleaf template mailTemplate.html email sent Some escaped text here, I believe you have a deeper understanding of "how to use SpringBoot to achieve QQ Mail to send email", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report