郵箱傳送模組

yuey6670發表於2024-03-10

注入依賴

jdk17
springboot3
<!--        傳送電子郵件-->
<dependency>
	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application配置

server:
  port: 9999  # ?????
spring:
  mail:
    # QQhost: smtp.exmail.qq.com
    host: smtp.qq.com
    # tencent mail port  ??????
    port: 465
    # ??QQ??
    username: xxxxxxxxxxxxx
    password: xxxxxxxxxxxxx
    test-connection: true
    properties:
      mail:
        smtp:
          ssl:
            enable: true

SendMailUtils工具類

package com.mailtoqq.utils;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class SendMailUtils {
    @Autowired
    JavaMailSenderImpl javaMailSender;
    //傳送普通文字郵件
    public void sendText(String Subject,String Text,String setFrom,String setTo){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setSubject(Subject); //標題
        simpleMailMessage.setText(Text); //內容
        simpleMailMessage.setFrom(setFrom); //傳送人郵件
        simpleMailMessage.setTo(setTo); //傳送目的地郵箱
        javaMailSender.send(simpleMailMessage);
    }
    //傳送帶頁面格式加檔案郵件
    public void sendTexts(String Subject,String Text,Boolean t,String setFrom,String setTo
    ,String attachmentFilename,String filePathName) throws MessagingException{
        MimeMessage mimeMessage=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(Subject);//標題
        helper.setText(Text,t);//內容
        helper.setFrom(setFrom);//傳送人郵箱
        helper.setTo(setTo);//目的地郵箱
        helper.addAttachment(attachmentFilename,new File(filePathName));  //圖片路徑
        javaMailSender.send(mimeMessage);
    }
}

SendMailController

package com.mailtoqq.controller;

import com.mailtoqq.utils.SendMailUtils;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class SendMailController {
    @Autowired
    SendMailUtils sendMailUtils;

    @GetMapping("/sendMail")
    public String sendMail() {
        String Subject = "你好,世界";	//題目
        String Text = "hello World";	//內容
        String From = "xxxxxxx@qq.com";		//傳送郵箱
        String To = "xxxxxxx@qq.com";  		//接收郵箱
        sendMailUtils.sendText(Subject, Text, From, To);
        return "傳送成功";
    }

    @GetMapping("/sendMails")
    @ResponseBody
    public String sendMails() throws MessagingException {
        String Subject = "你好,世界";
        String Text = "hello World";
        String From = "xxxxxx@qq.com";	//傳送郵箱
        String To = "xxxxxx@qq.com";	//接收郵箱
	String filename = "xxx.png";	//檔名
        String filePath = "xxx.png"; //檔案絕對路徑
        sendMailUtils.sendTexts("傳送帶頁面格式加檔案郵件測試", "<p style='color:red;'>紅</p>", true, From,
                To, filename, filePath);
//        return "<img src=\"images/002.png\">";
        return "123";
    }
}


相關文章