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

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2. Look at the source code first

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

@ConfigurationProperties(
    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;
        this.properties = 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 application.properties

spring.mail.username=30xxxxxxxx@qq.com//The user name of your QQ email
spring.mail.password=pkbkmjikcwimdgie//qq mailbox password
spring.mail.host=smtp.qq.com//Start with smtp. If you are qq, you are qq. com
 If you are Sina, you are Sina.com
//Turn on encryption verification
spring.mail.properties.maill.smtp.ssl.enable=true

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 MailSenderAutoConfiguration.java

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

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

MailSenderJndiConfiguration.class and
MailSenderPropertiesConfiguration.class

Open MailSenderJndiConfiguration.class

Here's the code

@Bean
JavaMailSenderImpl mailSender(Session session) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
    sender.setSession(session);
    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

@SpringBootTest
class SpringbootAsyncApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
            mailMessage.setSubject("Hello, little basket");
            mailMessage.setText("Thank you for lending me $500");
            mailMessage.setTo("3066xxxxxxx@qq.com");
            mailMessage.setFrom("30666xxxxxx@qq.com");
            mailSender.send(mailMessage);
    }

//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
mailMessage.setTo("3066xxxxxxx@qq.com");
//This is the sender
mailMessage.setFrom("30666xxxxxx@qq.com");
//The information object is packaged and sent to the past
mailSender.send(mailMessage);

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

    @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);
        //enclosure
        helper.addAttachment("1.jpg",new File("D:\\computer software\\Vitality wallpaper\\Vitality wallpaper cache\\img\\Vitality wallpaper cache\\img\\1.jpg"));
        helper.setTo("1106626347@qq.com");
        helper.setFrom("3066624292@qq.com");
        mailSender.send(mimeMessage);
    }

}

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