In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of springboot how to achieve mail service, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this springboot article on how to achieve mail service. Let's take a look at it.
Preface
Spring boot's project has not been updated for a small half a month, and finally I can write happily when I am free.
Before we configured mybatis multi-data sources, next we need to do a mail service. For example, when you register, you need to enter a CAPTCHA to verify it. This CAPTCHA can be sent by email. Of course, now most of the CAPTCHA is through text messages, and a single email is sometimes essential. So our spring scaffolding will also set up the mail service. The next article integrates SMS.
All right, let's get down to business. It may be troublesome to set up a mail service without contact, or a stand-alone test environment may not be possible. I don't think there is any mail service. In fact, if we use it personally, we can do it. The mailboxes of QQ Mail and NetEase are fine. I use QQ Mail here.
Server preparation > Mailbox server preparation
Log in to QQ Mail and click Settings-> account to find the following figure.
POP3/SMTP service needs to be activated. After activating this, a secret key will be generated. We will use this key in the project later. Take the notebook and write it down. Haha.
Add dependencies and configuration
When the mailbox is ready, let's start our project.
First add the dependency in the pom.xml file
Org.springframework.boot spring-boot-starter-mail
Then add the configuration to the application.proteries file and change it to your own mailbox. Password is the secret key that was just generated. QQ Mail's server address is smtp.qq.com. Everyone of NetEase can search it.
Spring.mail.host=smtp.qq.comspring.mail.username=1186154608@qq.comspring.mail.password=abcdefgqazqazspring.mail.default-encoding=UTF-8mail.from=1186154608@qq.com
Service layer
Once the configuration information is ready, we can use it. Here we don't talk about the database for the time being, just write the Service layer and the controller layer.
Create a MailService and MailServiceImpl under the service package
Code in MailServiceImpl
@ Service@Slf4jpublic class MailServiceImpl implements MailService {@ Autowired private JavaMailSender mailSender; @ Value ("${mail.from}") private String mailFrom; @ Override public void sendSimpleMail (String mailTo) {SimpleMailMessage message=new SimpleMailMessage (); message.setFrom (mailFrom); message.setTo (mailTo); message.setSubject ("simple mail"); message.setText ("hello world"); mailSender.send (message) Log.info ("message has been sent");}}
Here we will simply test to see if the e-mail can be sent. MailFrom is the sender and mailTo is the recipient. Message.setSubject () sets the subject of the message. Message.setText () sets the content of the message.
MailSender.send (message) is to send text messages.
# controller layer
Let's create a MailController class. The code is as follows:
`
@ RestController
@ RequestMapping ("/ mail")
Public class MailController {
@ Autowired
Private MailService mailService
@ RequestMapping (value = "/ send", method = RequestMethod.GET) public String sendMail (@ RequestParam (value = "userName") String userName) {mailService.sendSimpleMail (userName); return "success";}
}
`
You can see that there is only one sending interface. It's very simple, just send the parameters to the recipient's mailbox.
# Test
At this point, the demo of our mail service has been set up. Let's test it next. Let's start the project. Then adjust the interface
Http://localhost:9090/zlflovemm/mail/send?userName=1303123974@qq.com
The prompt has been sent successfully, let's go into the mailbox to see what we sent. You can see that it was sent successfully. So it shows that our mail service has been built successfully.
So now it seems that springboot integrated mail service is very simple, configure the mail server, you can use it directly.
Send attachment
Sometimes we send email not only to send content, but also to send attachments, how to achieve that. It's actually very simple. Those configurations remain the same. We're on the service floor. Write a sendMail method. As follows
@ Override public void sendMail (String mailTo) {MimeMessage message=mailSender.createMimeMessage (); MimeMessageHelper helper = null; try {helper = new MimeMessageHelper (message, true); helper.setFrom (mailFrom); helper.setTo (mailTo); helper.setSubject ("simple mail"); helper.setText ("hello world", true) FileSystemResource file = new FileSystemResource ("E:\ myself\ test.xls"); String fileName = file.getFilename (); helper.addAttachment (fileName, file); mailSender.send (message); log.info ("message sent");} catch (MessagingException e) {log.error ("{}", e);}}
You can see that it's a little different from when we started the test. Here first.
MimeMessage message=mailSender.createMimeMessage ()
MimeMessage is more powerful than SimpleMailMessage. It can send attachments or convert content to html format. Therefore, MimeMessage is generally used in practical use.
In addition, you need to send attachments with the help of MimeMessageHelper. MimeMessageHelper is an auxiliary MimeMessage.
Helper.setFrom (mailFrom); helper.setTo (mailTo); helper.setSubject ("simple mail"); helper.setText ("hello world", true)
These are the same as before, sender, recipient, subject, content.
Helper.addAttachment () adds attachments.
All right, let's test it next. You can see that the e-mail sent has an attachment. Prove it.
This is the end of the article on "how to implement the mail service in springboot". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve mail service in springboot". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.