1. 程式人生 > >springboot---發送郵件

springboot---發送郵件

pre xml配置 mes pat qq郵箱 prop tle bean trac

技術分享圖片

1、pom.xml配置

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

        <dependency>
            <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- SpringBoot 發送郵件 --> <dependency> <groupId>org.springframework.boot</groupId> <
artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>

2、在application.properties中添加郵箱配置

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#發送者的郵箱密碼
spring.mail.password=xxx
#端口
spring.mail.port=25
#協議
spring.mail.protocol=smtp
#發送者的郵箱賬號
[email protected]
server.port=8081

郵箱密碼:

QQ郵箱 --> 郵箱設置 --> 賬號

技術分享圖片

3、項目整體結構

技術分享圖片

4、業務層service接口

package org.zyu.springboot_email.service;

public interface EmailService {

    /**
     * 發送簡單郵件
     * @param sendTo    接收人
     * @param title     郵件標題
     * @param content   郵件內容
     */
    void sendSimpleMail(String sendTo,String title,String content);

    /**
     * 發送帶靜態資源的郵件
     * @param sendTo    接收人
     * @param title     郵件標題
     * @param content   郵件內容
     * @param path      資源路徑
     */
    void sendInlineMail(String sendTo,String title,String content,String path);

    /**
     * 發送帶附件的郵件
     * @param sendTo    接收人
     * @param title     郵件標題
     * @param content   郵件內容
     * @param filePath  附件路徑
     */
    void sendAttachmentsMail(String sendTo,String title,String content,String filePath);
}

5、serviceImpl實現service接口

package org.zyu.springboot_email.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class EmailServiceImpl implements EmailService {


    @Autowired
    JavaMailSender mailSender;

    //發送人
    @Value("${spring.mail.username}")
    private String sendFrom;

    @Override
    public void sendSimpleMail(String sendTo, String title, String content) {
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        mainMessage.setFrom(sendFrom);
        //接收者
        mainMessage.setTo(sendTo);
        //發送的標題
        mainMessage.setSubject(title);
        //發送的內容
        mainMessage.setText(content);
        mailSender.send(mainMessage);
    }

    @Override
    public void sendInlineMail(String sendTo, String title, String content,String path) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            FileSystemResource file = new FileSystemResource(new File(path));
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content, true);
            helper.addInline("weixin", file);
        } catch (Exception e) {
            System.out.println("發送帶靜態資源的郵件失敗");
        }

        mailSender.send(mimeMessage);
    }

    @Override
    public void sendAttachmentsMail(String sendTo, String title, String content, String filePath) {
        MimeMessage message=mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content);
            FileSystemResource file=new FileSystemResource(new File(filePath));
            String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
            //添加多個附件可以使用多條
            //helper.addAttachment(fileName,file);
            helper.addAttachment(fileName,file);
            mailSender.send(message);
            System.out.println("帶附件的郵件發送成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("發送帶附件的郵件失敗");
        }
    }
}

5、業務層Controller

package org.zyu.springboot_email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zyu.springboot_email.service.EmailService;

import java.nio.file.Path;

@RestController
public class MailController {

    @Autowired
    private EmailService emailService;

    /**
     * 簡單發送郵件
     * @return
     */
    @GetMapping("/send")
    public String send(){
        emailService.sendSimpleMail( "[email protected]", "測試標題", "hello world");
        return "發送成功";
    }

    /**
     * 發送靜態資源郵件
     * @return
     */
    @GetMapping("/sendInlineMail")
    public String sendInlineMail(){
        emailService.sendInlineMail("[email protected]", "主題:嵌入靜態資源", "<html><body><img src=\"cid:weixin\" ></body></html>","C:\\Users\\Administrator\\Desktop\\timg.jpg");
        return "發送成功";
    }

    /**
     * 發送帶附件的郵件
     * @return
     */
    @GetMapping("/sendAttachmentsMail")
    public String sendAttachmentsMail(){
        emailService.sendAttachmentsMail("[email protected]", "主題:攜帶附件郵件", "hello world", "D:\\谷歌下載\\註冊補貼明細列表.xlsx");
        return "發送成功";
    }


}

springboot---發送郵件