Spring mail sends mail via QQ mailbox server

Spring mail encapsulates the mail service of JavaMail, which makes the mail service easier to use. Next, take QQ mailbox server as an example, use spring mail service to send mail

Configure QQ mailbox, "Settings" - "account", open SMTP service and generate authorization code

To generate the authorization code, you need to verify the mobile phone. Next, you can send mail with QQ email account and authorization code. You don't need QQ password

Spring mail service configures dependency in spring context support, and then you can send mail with the help of the sending server provided by QQ mailbox

Plain text mail

The first test is plain text mail

After running, you can send an email. Note: the authorization code is not the password, and the port is not 25 but 587

Next, leave the mailsender unchanged, modify the mail type, and send rich mail

Simple HTML mail

To display the email content in HTML format, you only need to modify the following

Still use mailsender to send this mail

mailSender. send(mail);

HTML message with picture

Insert a picture display in the HTML content of the email and modify the text content

HTML message with attachments

Add an attachment to the message. The text content remains unchanged. You only need to modify it as follows

Freemaker template mail

HTML content is usually very rich. It's too messy to write it directly in the settext () method. Therefore, HTML should be managed separately as a file, and then its content should be converted into a string with a tool as the parameter of settext (). The freemaker template engine is taken as an example below

Create a new templates directory under the Src / main / resources directory of the project, and put a test FTL file, as follows

Then, use the tools provided by freemaker and spring to convert the content into strings, which of course depends on the new jar

Create a new freemarkerparser java

Replace the ${} content in the template with the value in the map, and convert the template file into a string string

Note: it is troublesome to configure and read the template path in the process. It is handled in this way for the time being

The code for sending mail requires only very small changes

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>