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 realize the function of sending email in springboot

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to achieve the function of sending mail in springboot". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope that this article "how to achieve the function of sending mail in springboot" can help you solve the problem.

Email is a very common feature, and its implementation in java depends on the interface JavaMailSender. In the springboot project, you need to introduce a dependency called spring-boot-starter-mail, and you can introduce a spring-boot-starter-thymeleaf dependency that can manipulate html files if you have requirements for the format of the message.

Org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-starter-thymeleaf

Like other automated configuration modules, after completing the dependency introduction, you also need to configure the corresponding property values in the application.properties, otherwise the running method will always report a null pointer.

Create a new springboot project.

1. Open idea and click the file button in the upper left toolbar to create a new boot project.

two。 Click next to select the default dependency, such as database connection, web, etc.

3. Click finsh and wait for the boot project directory to be generated. At this time the directory is not complete need to add their own new java and resources folder. Right-click project selection

Carry out the add operation.

4. Then open the pom file and introduce dependencies.

5. Open the configuration file and write down the corresponding parameters

When this project is completed, start writing test classes.

2. Send mail

1. Create a util class and write a business class that implements the sending logic. There is nothing to write about the tool class, because I want to implement a format when sending email, with attachments with html style and asynchronous operation, especially async. We all know that sending email is very time-consuming.

2. Add a way to write an asynchronous call:

* Asynchronous method is required to add @ Async

* you need to add a method to enable async on the startup class, @ EnableAsync

* Note that it may be because of the aop proxy. If the called method and the calling code are in the same class, it is only equivalent to the call of this class, and the proxy class is not used, so @ Async has no effect, that is, in a tool class.

3. Code: message with attachment

@ Test public void sendAttachmentsMail () {Context context = new Context (); context.setVariable ("agencyName", "11"); context.setVariable ("busTypeName", "22"); context.setVariable ("busAllowance", 33); String emailContent = templateEngine.process ("emailTeplate", context); try {emailService.sendAttachmentsMail (new String [] {"xxx.com"}, "Test Tips", emailContent) } catch (Exception e) {e.printStackTrace ();} System.out.println ("send mail fujian * @ throws Exception * / @ Async public void sendAttachmentsMail (String [] to, String subject, String contnet) throws Exception {MimeMessage mimeMessage = mailSender.createMimeMessage (); MimeMessageHelper helper = new MimeMessageHelper (mimeMessage, true)" Helper.setFrom ("xxx.com"); helper.setTo (to); helper.setSubject (subject); helper.setText (contnet,true); Path picturePath = Paths.get ("E:WorkFiles estBill", "test.png"); byte [] bytes = Files.readAllBytes (picturePath); helper.addAttachment ("Annex-1.jpg", picturePath.toFile ()); mailSender.send (mimeMessage);}

The mail was sent successfully

This is the end of the content about "how to achieve the function of sending mail in springboot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

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

12
Report