“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

๐Ÿ‘จ๐ŸŽ“ Author: Bug Bacteria

โœ๏ธ blog: CSDN, Nuggets, etc

๐Ÿ’Œ public account: Magic House of the Circle of the Apes

๐Ÿšซ special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

๐Ÿ™ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles ๐Ÿ‘. Your likes (collect โญ๏ธ+ pay attention to ๐Ÿ‘จ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon ๐Ÿƒ๐Ÿปโ™€๏ธ, creation stopped ๐Ÿ’•, refueling ๐Ÿป

A: the preface

Today I received a request temporarily, which required the bug bacteria to realize a function of sending emails at a regular time, and it was required to be done in one day. The first response to this request is, boy, this is not easy, tell me the sender and recipient email address and I will do it in 15 minutes, guys, have you ever written a similar email service? If not, take a look at how bug bacteria do it.

Ok, let’s get started.

Second, preparation

First, I will tell you that I will divide this function into several parts for one teaching, in order not to cause too much length, which will lead to visual fatigue of friends and no desire to finish reading. So please also some small partners more excuse ha ~~~

Take QQ mailbox as an example to give you a demonstration.

1. Enable the POP3/SMTP service

1, how to open your email POP3/SMTP service? I’ll show you. Let’s move on.

As for why it’s open, I’ll show you later, if it’s not open, what’s the problem? A -> B -> C -> D -> E

A, web login QQ mailbox, home page click mailbox Settings, and then click Breadcrumb-account

B, slipping, find the POP3 / IMAP/SMTP/Exchange/CardDAV/CalDAV service; Then click POP3 on the far right.

C. Click open, a box will pop up for you to verify the opening of SMS. As follows:

D, first of all, close the prompt box, put it, and then you go to your mobile phone for SMS sending, the specific content and sender I have given below, we can copy and paste. After sending, click “I have sent” in the popbox.

The message content is: ‘Configure the mail client’

Send to: ‘1069070069’

E. After the SMS is sent successfully, click “I have sent”, a window will pop up again in the mailbox interface, as shown below. The POP3 service is successfully enabled. Remember, the license code must be copied, find a document to save, later must use it, otherwise you have to apply for another license code, it will be very troublesome.

This authorization code is the password we use to log in to third-party clients, and it is a key part of your code to send emails.

2. Introduce the spring-boot-starter-mail dependency.

Since Spring introduced the JavaMailSender class about Mail, Spring Boot further encapsulated it based on this class, so as to realize the integration of easily sending Mail. Moreover, the JavaMailSender class provides powerful mail sending capabilities, supporting various types of mail sending. So let’s use Spring Boot directly to operate on mail-starter, and then dig into the source code.

That is, we can introduce the starter-mail dependency in the POM file.

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>Copy the code

3, application-dev.yaml file configuration

The following configuration is in the system configuration file. If you use a. Properties configuration file, then you can change the following configuration format to the corresponding format, such as the syntax spring.mail.hots = smtp.qq.com. Yaml to. Properties: Change the colon to [.] and use the value [=].

And then I have the same configuration file format as I did, so you can just copy it, two things.

  • First, add this to the environment in which you are running active. I am in dev.
  • Second, the authorization code that reminds you to save, corresponding to the password below.

The configuration is as follows: For reference only.

Spring: mail: host: smtp.qq.com # configure the SMTP server address port: 25 # SMTP server port: [email protected] # configure the email username; Password: iptxxxxxkbffjbDHg # configure the requested authorization code; Default-encoding: UTF-8 # Specifies the email encoding protocol: SMTP #Copy the code

Finish the above 3 steps in order, the environment configuration of the mail is finished, and then I will answer your questions.

What if POP3/SMTP service is not enabled?

I do not open, directly configure, and then directly write a test test class to send an email, error, isn’t it? Oh ho, so so, execute again, true error ah? Then the error reporting is studied. The password of the configuration file I filled in the email number password at the beginning, the result reported an error, and then according to the error, only response, QQ mailbox guide me to open the service. However, you can try it, it will impress you a little bit more.

The following is the error source:

org.springframework.mail.MailAuthenticationException: Authentication failed;
nested exception is javax.mail.AuthenticationFailedException: 535 Login Fail.
Please enter your authorization code to login.
More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
Copy the code

This is also the protection mechanism of QQ email account. Otherwise, if the account password is stolen, it can be used to send the source casually. Think about it, isn’t it?

. .

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Appendix – Mail properties

The following is the general configuration mail properties of Spring Boot for your reference.

Above is the spring configuration to mail some configuration properties, specific configuration is with me to the above several main attributes are common, basic is to send a piece of business, those a few basic configuration properties is enough, the rest, you can also to fumble, because I don’t understand ah ๐Ÿค“, ha, ha, ha, food is very real.

Five, the past popular recommendation

Springboot series (12) : How to code simple email (part 1)

Springboot series (12) : how to code the implementation of pictures and other attachments to mail (in the middle)

Springboot series (12) : How to code static template mail (part 2)

Springboot series (12) : how to code to achieve a large email send disk (summary)

. .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ˜˜๏ธ Be who you want to be, there is no time limit, you can start whenever you want,

๐Ÿ€ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ€‹

๐Ÿ’“ If this article is helpful to you, please leave a like! (# ^. ^ #);

๐Ÿ’ if you like the article shared by bug fungus, please give bug fungus a point of concern! (เน‘ ‘แด— โ€ต เน‘);

๐Ÿ’— if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

๐Ÿ’ž In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

๐Ÿ’• copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you