springboot發送email郵件

添加依賴html

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

配置:去郵箱中開啓SMTP服務java

注意密碼是郵箱的生成受權碼spring

代碼:springboot

 1 package com.drawnblue.springbootemail;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.core.io.FileSystemResource;
 6 import org.springframework.mail.SimpleMailMessage;
 7 import org.springframework.mail.javamail.JavaMailSender;
 8 import org.springframework.mail.javamail.MimeMessageHelper;
 9 import org.springframework.stereotype.Component;
10 
11 import javax.mail.MessagingException;
12 import javax.mail.internet.MimeMessage;
13 import java.io.File;
14 import java.util.Date;
15 
16 @Component
17 public class EmailUtil {
18     private String from="1248375279@qq.com";
19     @Autowired
20     private JavaMailSender sender;
21 
22     /**
23      * 發送通常文本郵件
24      * @param to
25      * @param subject
26      * @param content
27      */
28     public void sendTextEmail(String to,String subject,String content){
29         SimpleMailMessage message = new SimpleMailMessage();
30         message.setFrom(from);
31         message.setTo(to);
32         message.setSubject(subject);
33         message.setText(content);
34         message.setSentDate(new Date());
35         sender.send(message);
36     }
37 
38     /**
39      * @param to
40      * @param subject
41      * @param content
42      * @param imgPath
43      * @param imgId
44      * @throws MessagingException
45      * 發送帶圖片並顯示在郵件中的郵件
46      */
47     public void sendImageMail(String to, String subject, String content, String imgPath, String imgId) throws MessagingException {
48         //建立message
49         MimeMessage message = sender.createMimeMessage();
50         MimeMessageHelper helper = new MimeMessageHelper(message, true);
51         //發件人
52         helper.setFrom(from);
53         //收件人
54         helper.setTo(to);
55         //標題
56         helper.setSubject(subject);
57         //true指的是html郵件,false指的是普通文本
58         helper.setText(content, true);
59         //添加圖片
60         FileSystemResource file = new FileSystemResource(new File(imgPath));
61         helper.addInline(imgId, file);
62         //發送郵件
63         sender.send(message);
64     }
65 
66 }

測試spring-boot

package com.drawnblue.springbootemail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootEmailApplicationTests {
@Autowired
EmailUtil text;

    /**
     * 文本
     */
    @Test
    public void contextLoads() {
        text.sendTextEmail("yourEmailAddr","test","helloworld!!!");
    }

    /**
     * @throws MessagingException
     * 發送帶圖片的郵件
     */
    @Test
    public void sendImageEmailTest() throws MessagingException {
        text.sendImageMail("yourEmailAddr","image測試","<h1 style='color:red'>helloWorld</h1><img src='cid:0011'/>","G:\\壁紙\\timg.jpg","001");
    }
}

效果測試

 

 

 要了解其餘的也能夠參考http://www.javashuo.com/article/p-vawiykrh-bn.html博文spa

相關文章
相關標籤/搜索