3. SpringBoot ------ email (1)

Development tools: STS

Code download link: https://github.com/theIndoorTrain/Springboot/tree/8878e8e89ce01ceb967ef8c1193ac740a6f7dd40

Preface:

On your birthday, Tencent will send you a beautiful birthday wish email

When you register an account on a website, you often need to activate the verification in the email

Let's talk about this technology today, email delivery.

Our usual steps of sending email are:

1. Log in to mailbox website or client

2. Enter the account number and password

3. Fill in the recipient

4. Write email content

5. send

In our web project, we want to send an email to the specified user. The steps are as follows:

1. Bind mailbox server

2. Verify account number and password (authorization code)

3. Create mail

4. Fill in the receiver and mail content

5. send

Now let's send the email.

1, Sending simple mail

1. Add mail dependency in pom.xml

1         <!--Add to mail rely on  -->
2         <dependency>
3             <groupId>org.springframework.boot</groupId>
4             <artifactId>spring-boot-starter-mail</artifactId>
5         </dependency>

2. Configure mail in application

 1. Configure mailbox
 2 spring:
 3   mail:
 4     host: smtp.qq.com
 5     username: 1373572467@qq.com
 6 password: email password (fill in authorization code for qq email)
 7     default-encoding:  UTF-8
 8  
 9. Configure email sender
10 mail: 
11   from: 
12     addr: 1373572467@qq.com

3. Define the mail sending business interface:

 1 package com.xm.service;
 2 
 3 
 4 /**
 5  * Mail sending business
 6  * @author xm
 7  *
 8  */
 9 public interface EmailService {
10     
11     /**
12      * Send simple mail
13      * @param to : Addressee
14      *  @param subject : Title
15      * @param content : Mail content
16      */
17     void sendSimpleEmail(String to,String subject,String content);
18 
19 }

4. Realize the mail sending business:

 1 package com.xm.service.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.mail.SimpleMailMessage;
 6 import org.springframework.mail.javamail.JavaMailSender;
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.xm.service.EmailService;
10 /**
11  * Implementation of mail sending business
12  * @author xm
13  *
14  */
15 @Service
16 public class EmailServiceImpl implements EmailService {
17     
18     //Get sender information
19     @Value("${mail.from.addr}")
20     private String from; 
21     
22     //Define the sender
23     @Autowired
24     private JavaMailSender sender;
25 
26     @Override
27     public void sendSimpleEmail(String to, String subject,String content) {
28         //Define simple mail
29         SimpleMailMessage message = new SimpleMailMessage();
30         //Encapsulate sender, receiver, title and content into simple mail
31         System.out.println("from: "+from+",to:"+to+",subject:"+subject);
32         message.setFrom(from);
33         message.setTo(to);
34         message.setSubject(subject);
35         message.setText(content);
36         //Send to sender for forwarding
37         sender.send(message);
38         System.out.println("Send out");
39     }
40 
41 }

5. Define mail test class:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.service.EmailService;
10 
11 @RunWith(SpringRunner.class)
12 @SpringBootTest
13 public class EmailTest {
14     
15     @Autowired
16     private EmailService emailService;
17     
18     @Test
19     /**
20      * Test sending simple mail
21      */
22     public void sendSimpleMassage() {
23         emailService.sendSimpleEmail("1373572467@qq.com", "122", "Hello Mail!");
24     }
25 
26 }

6. Screenshot of operation result:

2, Email with attachments

1. Define the mail interface with attachments:

1      /**
2      * Send mail with attachments
3      * @param to: Addressee
4      * @param subject : Title
5      * @param content: Mail content
6      * @param attachment: Enclosure
7      */
8     void sendAttachmentEmail(String to,String subject,String content,File attachment);

2. Realize this business:

   @Override
    public void sendAttachmentEmail(String to, String subject, String content, File attachment) {
        //Create Multipurpose Internet Mail
        MimeMessage message = sender.createMimeMessage();
        
        try {
            //Encapsulating Multipurpose Internet Mail
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            helper.addAttachment("Enclosure", attachment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sender.send(message);
    }

 

3. Define test:

1   @Test
2     /**
3      * Multipurpose Internet Mail
4      */
5     public void sendAttachmentEmail() {
6         File attachment = new File("src/main/resources/static/1.txt");
7         emailService.sendAttachmentEmail("1373572467@qq.com", "122", "Hello Mail!",attachment);
8     }

 

4. Screenshot of operation result:

                                        2018-07-16

Keywords: Java Spring Junit github SpringBoot

Added by sssphp on Thu, 13 Feb 2020 18:53:30 +0200