Simple mail sending [springboot]

Before learning java web, sending mail is relatively cumbersome. You can see the implementation of Java Web Java web mail sending
In spring boot, the code is encapsulated. It only needs a few words of code to implement, from sender to sender

① First look at simple mail sending

1. Import mail dependency


2. Look at the source code first

Double click shift = "search =" Find @ EnableConfigurationProperties({MailProperties.class}) = "to open MailProperties
We can see the following code

    prefix = "spring.mail"
public class MailProperties {
    private static final Charset DEFAULT_CHARSET;
    private String host;
    private Integer port;
    private String username;
    private String password;
    private String protocol = "smtp";
    private Charset defaultEncoding;
    private Map<String, String> properties;
    private String jndiName;

    public MailProperties() {
        this.defaultEncoding = DEFAULT_CHARSET; = new HashMap();

Through this code, we can know that during automatic configuration, we only need to add spring.mail to the header, and we can automatically assemble it into the container
So we can directly put the following code into user name of your QQ email
spring.mail.password=pkbkmjikcwimdgie//qq mailbox password with smtp. If you are qq, you are qq. com
 If you are Sina, you are
//Turn on encryption verification

The reason to start with smtp is because

private String protocol = "smtp";default

The password of the qq email above is very special. How do we get it (in order not to expose it to others)

Return to

Double click shift = "search
We can also see that two classes are referenced

@Import({MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class})
public class MailSenderAutoConfiguration {
    public MailSenderAutoConfiguration() {

MailSenderJndiConfiguration.class and

Open MailSenderJndiConfiguration.class

Here's the code

JavaMailSenderImpl mailSender(Session session) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    return sender;

Automatically equip a JavaMailSenderImpl and we can use it directly

How to get the clear text password of qq mailbox

1. Follow the steps to open the mailbox

First, log in to the qq mailbox, click settings in the upper left corner = "mailbox settings", click users = "and scroll down to find the POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service

Open service:
POP3/SMTP service (how to use Foxmail and other software to send and receive mail?)
Closed | on
Click to open the service
Sending the verification code will give you a string of plaintext similar to my password above;

Simple code implementation, send text

1. Test

class SpringbootAsyncApplicationTests {

    JavaMailSenderImpl mailSender;

    void contextLoads() {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
            mailMessage.setSubject("Hello, little basket");
            mailMessage.setText("Thank you for lending me $500");

//This is the subject of sending
mailMessage.setSubject("Hello, little basket");
//This is the message sent
mailMessage.setText("thank you for lending me $500");
//This is the sending object
//This is the sender
//The information object is packaged and sent to the past

So you can finish sending to receiving

2. Renderings

Of course, you can also send it to yourself
At this time, many people wonder if it is possible to realize a bomber
Use while(True) to send it to your roommate. In fact, others can think of what you can think of
I've tried. I only sent it successfully. It seems that it's limited more than a dozen times!!!!

② Complete complex mail sending

1. Test

    void contextLoad2() throws MessagingException {
        //A complex email
        MimeMessage mimeMessage=mailSender.createMimeMessage();
      //To execute multiple files, click True
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("Hello, xiaoyiming");
        helper.setText("<p style='color:red'></p>",true);
        helper.addAttachment("1.jpg",new File("D:\\computer software\\Vitality wallpaper\\Vitality wallpaper cache\\img\\Vitality wallpaper cache\\img\\1.jpg"));


This code supports html

        helper.setText("<p style='color:red'></p>",true);

2. Renderings

Keywords: Spring Spring Boot xml javamail webmail

Added by bokerfest on Mon, 29 Nov 2021 02:56:31 +0200