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 send springboot mailbox

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

Share

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

This article mainly introduces "how to send springboot mailbox". In daily operation, I believe many people have doubts about how to send springboot mailbox. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to send springboot mailbox". Next, please follow the editor to study!

Step 1: add dependencies and add spring-boot-starter-mail dependencies to the pom.xml file

Org.springframework.boot

Spring-boot-starter-mail

Org.springframework.boot

Spring-boot-starter-freemarker

Step 2: get the authorization of each mailbox

For example: obtain the authorization of QQ Mail

QQ Mail-> Settings-> account-> POP3/SMTP Service: after enabling the service, you will get the authorization code of QQ.

# configuration for sending JavaMailSender messages

Spring.mail.host=smtp.qq.com

QQ Mail, a spring.mail.username= user

# the authorization code of QQ Mail

Spring.mail.password= authorization code

Spring.mail.properties.mail.smtp.auth=true

Spring.mail.properties.mail.smtp.starttls.enable=true

Spring.mail.properties.mail.smtp.starttls.required=true

Spring.mail.default-encoding=UTF-8

Step 3: realize the function of sending mail

Import javax.mail.MessagingException

Import javax.mail.internet.MimeMessage

Import org.apache.log4j.Logger

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.core.io.FileSystemResource

Import org.springframework.mail.SimpleMailMessage

Import org.springframework.mail.javamail.JavaMailSender

Import org.springframework.mail.javamail.MimeMessageHelper

Import org.springframework.stereotype.Service

/ * *

* @ author XXXXXX

*

, /

@ Service

Public class MailSendService

{

@ Autowired

Private JavaMailSender mailSender

/ * * Log printing. * /

Private Logger logger = Logger.getLogger (MailSendService.class)

/ * *

* send a simple email

* @ param fromPos sending address

* @ param toPos destination address

* @ param subject email subject

* @ param text email content

* @ return whether the delivery is successful

, /

Public boolean sendSimpleMail (String fromPos, String toPos, String subject, String text) {

SimpleMailMessage msg = new SimpleMailMessage ()

Msg.setFrom (fromPos)

/ / multiple destination addresses can be added

Msg.setTo (toPos)

Msg.setSubject (subject)

Msg.setText (text)

/ / msg.setBcc (bcc) BCC address

/ / msg.setCc (cc) CC address

Try

{

MailSender.send (msg)

}

Catch (Exception e)

{

Logger.error ("failed to send simple mail.", e)

Return false

}

Return true

}

/ * *

* send mail with attachments

* @ param fromPos sending address

* @ param toPos destination address

* @ param subject email subject

* @ param file attachment

* @ return whether the delivery is successful

, /

Public boolean sendAttachFileMail (String fromPos, String toPos, String subject, String text, FileSystemResource file) {

MimeMessage msg = mailSender.createMimeMessage ()

Try

{

/ / MimeMessageHelper constructor. If you want to send an attachment message, you must specify the multipart parameter true.

MimeMessageHelper helper = new MimeMessageHelper (msg, true)

Helper.setFrom (fromPos)

Helper.setTo (toPos)

Helper.setSubject (subject)

Helper.setText (text)

Helper.addAttachment (file.getFilename (), file)

MailSender.send (msg)

}

Catch (MessagingException e)

{

Logger.error ("failed to send attachment message.", e)

Return false

}

Return true

}

/ * *

* send files of static resources, such as pictures

* @ param fromPos sending address

* @ param toPos destination address

* @ param subject email subject

* @ param file attachment

* @ return whether the delivery is successful

, /

Public boolean sendInlineMail (String fromPos, String toPos, String subject, FileSystemResource file) {

MimeMessage msg = mailSender.createMimeMessage ()

Try

{

/ / MimeMessageHelper constructor. If you want to send an attachment message, you must specify the multipart parameter true.

MimeMessageHelper helper = new MimeMessageHelper (msg, true)

Helper.setFrom (fromPos)

Helper.setTo (toPos)

Helper.setSubject (subject)

Helper.setText ("static resources:

", true)

Helper.addInline ("pic", file)

MailSender.send (msg)

}

Catch (MessagingException e)

{

Logger.error ("failed to send attachment message.", e)

Return false

}

Return true

}

At this point, the study on "how to send springboot mailbox" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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