1. 程式人生 > 其它 >python資料分析——灰度預測+LinearSVR和AERIMA預測財政收入

python資料分析——灰度預測+LinearSVR和AERIMA預測財政收入

一、郵箱設定

Springboot想要使用郵箱服務傳送郵件需修改郵箱配置,以QQ郵箱為例

登入QQ郵箱,點選開啟SMTP服務,如下圖所示

這裡會生成一個授權碼,複製出來,配置檔案裡要用

二、專案程式碼

引入maven依賴

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

application.properties 配置中新增如下內容,username

password 要改成自己的

# 郵箱伺服器地址
spring.mail.host=smtp.qq.com
# 傳送方的郵箱
[email protected]
# 傳送方的授權碼,郵箱設定裡面生成的授權碼,這個不是真實的密碼
spring.mail.password=mecjeueyzejqzuaf
spring.mail.port=465
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
# 是否用啟用加密傳送的協議驗證項
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 郵件編碼
spring.mail.default-encoding=UTF-8

定義郵件服務介面

package com.rkyao.spring.boot.email.service;

/**
 * 郵件服務介面
 */
public interface MailService {

    /**
     * 傳送普通純文字郵件
     *
     * @param to 收件人地址
     * @param subject 郵件主題
     * @param text 郵件內容
     */
    void sendSimpleMail(String to, String subject, String text);

}

郵件服務介面實現類

package com.rkyao.spring.boot.email.service.impl;

import com.rkyao.spring.boot.email.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 郵件傳送服務
 */
@Service
public class MailServiceImpl implements MailService {

    private Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String senderMail;

    @Override
    public void sendSimpleMail(String to, String subject, String text) {
        Assert.hasText(to, "收件人郵箱不能為空");
        Assert.hasText(subject, "郵件主題不能為空");
        Assert.hasText(text, "郵件內容不能為空");

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(senderMail);   //郵件傳送人
        message.setTo(to);  //郵件接收人
        message.setSubject(subject);   //郵件主題
        message.setText(text);   //郵件內容
        mailSender.send(message);
        logger.info("The mail has been sent, to: {}, subject: {}, text: {}", to, subject, text);
    }

}

寫一個controller方法

package com.rkyao.spring.boot.email.controller;

import com.rkyao.spring.boot.email.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 郵件傳送
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private MailService mailService;

    /**
     * 傳送郵件
     * localhost:8080/mail/[email protected]&subject=這是郵件主題&text=這是郵件內容
     *
     * @param to 收件人郵箱
     * @param subject 郵件主題
     * @param text 郵件內容
     */
    @RequestMapping("/send")
    public void send(String to, String subject, String text) {
        mailService.sendSimpleMail(to, subject, text);
    }

}

呼叫這個controller方法測試一下,收件郵箱改成實際地址,目標郵箱成功收到郵件,驗證成功

localhost:8080/mail/[email protected]&subject=這是郵件主題&text=這是郵件內容

三、GitHub原始碼地址

https://github.com/yaorongke/spring-boot-demos/tree/main/spring-boot-email