首頁 > 軟體

SpringBoot 整合簡訊和郵件的設定範例詳解

2022-04-22 10:00:49

準備工作

1、整合郵件

以QQ郵箱為例

在傳送郵件之前,要開啟POP3和SMTP協定,需要獲得郵件伺服器的授權碼,獲取授權碼:

1、設定>賬戶

在賬戶的下面有一個開啟SMTP協定的開關並進行密碼驗證:

2、獲取成功的授權碼

2、整合簡訊

以阿里雲簡訊服務為例

1、登陸阿里雲—>進入控制檯—>開通簡訊服務

進入後根據提示開通簡訊服務即可。

2、充值

後期傳簡訊測試需要,暫時可以跳過此步驟。

3、獲取AccessKey和AccessSercet

檔案使用指引: https://help.aliyun.com/document_detail/59210.html?spm=a2c6h.13066369.0.0.4b3516b4kN0052

4、API

OpenAPI地址: https://next.api.aliyun.com/document/Dysmsapi/2017-05-25/overview

依賴

1、郵件

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

2、簡訊

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
    <version>2.0.4</version>
</dependency>

設定

1、設定郵箱基本資訊

spring:
  mail:
    # 設定 SMTP 伺服器地址
    host: smtp.qq.com
    # 傳送者郵箱
    username: 742354529@qq.com
    # 設定密碼,注意不是真正的密碼,而是申請的授權碼
    password: vjstfghblprwbdbd
    # 埠號465或587
    port: 587 
    # 預設的郵件編碼為UTF-8
    default-encoding: UTF-8
    # 設定SSL 加密工廠
    properties:
      mail:
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        # 表示開啟DEBUG模式,郵件傳送過程的紀錄檔會在控制檯列印出來
        debug: true

SMTP 伺服器地址

  • 126郵箱SMTP伺服器地址:smtp.126.com,埠號:465或者994
  • 2163郵箱SMTP伺服器地址:smtp.163.com,埠號:465或者994
  • yeah郵箱SMTP伺服器地址:smtp.yeah.net,埠號:465或者994
  • qq郵箱SMTP伺服器地址:smtp.qq.com,埠號465或587*

2、簡訊設定

# 阿里雲簡訊設定
sms:
  access-id: LTAI5tDP3SDQC9yvCguiiFDr
  access-key: EGSDQsLxCVS5dwjS8DCxmYQ124XySV
  sign-name: 
  endpoint: dysmsapi.aliyuncs.com

編碼

1、郵件

1.1、MailService.java

package com.tanersci.service;

import com.tanersci.dto.MailMessageDto;
import com.tanersci.vo.MessageVo;
/**
 * @ClassName: MailService.java
 * @ClassPath: com.tanersci.service.MailService.java
 * @Description: 郵件
 * @Author: tanyp
 * @Date: 2021/6/7 9:18
 **/
public interface MailService {
	/**
	 * @MonthName: sendSimple
	 * @Description: 普通郵件傳送
	 * @Author: tanyp
	 * @Date: 2021/6/7 9:30
	 * @Param: [dto]
	 * @return: void
	 **/
	MessageVo sendSimple(MailMessageDto dto);
	 * @MonthName: sendAttachFile
	 * @Description: 帶附件的郵件
	MessageVo sendAttachFile(MailMessageDto dto);
	 * @MonthName: sendImgRes
	 * @Description: 帶圖片資源的郵件
	MessageVo sendImgRes(MailMessageDto dto);
}

1.2、MailServiceImpl.java

package com.tanersci.service.impl;

import com.alibaba.fastjson.JSON;
import com.tanersci.dto.MailMessageDto;
import com.tanersci.vo.MessageVo;
import com.tanersci.constant.Constants;
import com.tanersci.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
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.MessagingException;
import javax.mail.internet.MimeMessage;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
/**
 * @ClassName: MailServiceImpl.java
 * @ClassPath: com.tanersci.service.impl.MailServiceImpl.java
 * @Description: 郵件
 * @Author: tanyp
 * @Date: 2021/6/7 9:18
 **/
@Slf4j
@Service
public class MailServiceImpl implements MailService {
	@Value("${spring.mail.username}")
	private String sender;
	@Autowired
	private JavaMailSender javaMailSender;
	/**
	 * @MonthName: sendSimple
	 * @Description: 普通郵件傳送
	 * @Author: tanyp
	 * @Date: 2021/6/7 9:30
	 * @Param: [dto]
	 * @return: void
	 **/
	@Override
	public MessageVo sendSimple(MailMessageDto dto) {
		try {
			log.info("=======普通郵件傳送開始,請求引數:{}", JSON.toJSON(dto));
			// 構建一個郵件物件
			SimpleMailMessage message = new SimpleMailMessage();
			// 設定郵件主題
			message.setSubject(dto.getSubject());
			// 設定郵件傳送者,這個跟application.yml中設定的要一致
			message.setFrom(sender);
			// 設定郵件接收者,可以有多個接收者,中間用逗號隔開,以下類似
			// message.setTo("10*****16@qq.com","12****32*qq.com");
			message.setTo(dto.getRecipient());
			// 設定郵件抄送人,可以有多個抄送人
			if (Objects.nonNull(dto.getCc())) {
				message.setCc(dto.getCc());
			}
			// 設定隱祕抄送人,可以有多個
			if (Objects.nonNull(dto.getBcc())) {
				message.setBcc(dto.getBcc());
			// 設定郵件傳送日期
			message.setSentDate(new Date());
			// 設定郵件的正文
			message.setText(dto.getText());
			// 傳送郵件
			javaMailSender.send(message);
			log.info("=======普通郵件傳送結束");
			return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
		} catch (MailException e) {
			log.error("====郵件====sendSimple=====異常:{}", e);
			return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
		}
	}
	 * @MonthName: sendAttachFile
	 * @Description: 帶附件的郵件
	public MessageVo sendAttachFile(MailMessageDto dto) {
			log.info("=======帶附件的郵件開始,請求引數:{}", JSON.toJSON(dto));
			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
			// true表示構建一個可以帶附件的郵件物件
			MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
			// 第一個引數是自定義的名稱,字尾需要加上,第二個引數是檔案的位置
			dto.getAttachments().forEach(file -> {
				try {
					message.addAttachment(file.getName(), file);
				} catch (MessagingException e) {
					log.error("=========郵件附件解析異常:{}", e);
				}
			});
			javaMailSender.send(mimeMessage);
			log.info("=======帶附件的郵件結束");
		} catch (MessagingException e) {
			log.error("==========郵件====sendAttachFile=====異常:{}", e);
	 * @MonthName: sendImgRes
	 * @Description: 帶圖片資源的郵件
	public MessageVo sendImgRes(MailMessageDto dto) {
			log.info("=======帶圖片資源的郵件開始,請求引數:{}", JSON.toJSON(dto));
			// 第一個引數指的是html中預留位置的名字,第二個引數就是檔案的位置
					message.addInline(file.getName(), new FileSystemResource(file));
					log.error("=========郵件圖片解析異常:{}", e);
			log.info("=======帶圖片資源的郵件結束");
			log.error("====郵件====sendImgRes=====異常:{}", e);
}

1.3、VO、DTO及常數類

MailMessageDto.java

package com.tanersci.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.File;
import java.io.Serializable;
import java.util.List;
/**
 * @ClassName: MailMessageDto.java
 * @ClassPath: com.tanersci.dto.MailMessageDto.java
 * @Description: 郵件訊息
 * @Author: tanyp
 * @Date: 2021/6/7 9:20
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "郵件訊息")
public class MailMessageDto implements Serializable {
	private static final long serialVersionUID = 5483400172436286831L;
	@ApiModelProperty(value = "郵件主題")
	private String subject;
	@ApiModelProperty(value = "接收者:可以有多個接收者,中間用逗號隔開")
	private String recipient;
	@ApiModelProperty(value = "抄送人:可以有多個抄送人,中間用逗號隔開")
	private String cc;
	@ApiModelProperty(value = "隱祕抄送人:可以有多個抄送人,中間用逗號隔開")
	private String bcc;
	@ApiModelProperty(value = "正文")
	private String text;
	@ApiModelProperty(value = "模板編碼")
	private String code;
	@ApiModelProperty(value = "附件、圖片")
	private List<File> attachments;
}

MessageVo.java

package com.tanersci.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
 * @ClassName: MessageVo.java
 * @ClassPath: com.tanersci.vo.MessageVo.java
 * @Description: 簡訊、郵件訊息返回值
 * @Author: tanyp
 * @Date: 2021/6/7 11:35
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "簡訊、郵件訊息返回值")
public class MessageVo implements Serializable {
	private static final long serialVersionUID = 5287525465339500144L;
	@ApiModelProperty(value = "狀態碼")
	private String code;
	@ApiModelProperty(value = "狀態碼的描述")
	private String message;
	@ApiModelProperty(value = "請求ID")
	private String requestId;
	@ApiModelProperty(value = "傳送回執ID")
	private String bizId;
	@ApiModelProperty(value = "模板編碼")
	private String templateCode;
}

Constants.java

package com.tanersci.constant;

/**
 * @ClassName: Constants.java
 * @ClassPath: com.tanersci.constant.Constants.java
 * @Description: 常數
 * @Author: tanyp
 * @Date: 2021/5/22 15:54
 **/
public class Constants {
	/**
	 * 訊息傳送狀態碼
	 */
	public final static String NEWS_SUCCESS_CODE = "OK";
	public final static String NEWS_SUCCESS_MESSAGE = "傳送成功";
	public final static String NEWS_FAIL_CODE = "FAIL";
	public final static String NEWS_FAIL_MESSAGE = "傳送失敗";
}

2、簡訊

2.1、SmsService.java

package com.tanersci.service;

import com.tanersci.dto.SmsMessageDto;
import com.tanersci.dto.SmsTemplateDto;
import com.tanersci.vo.MessageVo;
/**
 * @ClassName: SmsService.java
 * @ClassPath: com.tanersci.service.SmsService.java
 * @Description: 簡訊
 * @Author: tanyp
 * @Date: 2021/6/7 10:56
 **/
public interface SmsService {
	/**
	 * @MonthName: send
	 * @Description: 傳簡訊
	 * @Author: tanyp
	 * @Date: 2021/6/7 14:50
	 * @Param: [dto]
	 * @return: com.tanersci.vo.MessageVo
	 **/
	MessageVo send(SmsMessageDto dto);
	 * @MonthName: addSmsTemplate
	 * @Description: 申請簡訊模板
	 * @Param: [template]
	MessageVo addSmsTemplate(SmsTemplateDto template);
	 * @MonthName: deleteSmsTemplate
	 * @Description: 刪除簡訊模板
	MessageVo deleteSmsTemplate(SmsTemplateDto template);
	 * @MonthName: modifySmsTemplate
	 * @Description: 修改未通過稽核的簡訊模板
	MessageVo modifySmsTemplate(SmsTemplateDto template);
	 * @MonthName: querySmsTemplate
	 * @Description: 查詢簡訊模板的稽核狀態
	MessageVo querySmsTemplate(SmsTemplateDto template);
}

2.2、SmsServiceImpl.java

package com.tanersci.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.tanersci.dto.SmsMessageDto;
import com.tanersci.dto.SmsTemplateDto;
import com.tanersci.vo.MessageVo;
import com.tanersci.config.SmsConfig;
import com.tanersci.constant.Constants;
import com.tanersci.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;
/**
 * @ClassName: SmsServiceImpl.java
 * @ClassPath: com.tanersci.service.impl.SmsServiceImpl.java
 * @Description: 簡訊
 * @Author: tanyp
 * @Date: 2021/6/7 10:57
 **/
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {
	@Autowired
	private SmsConfig smsConfig;
	/**
	 * @MonthName: createClient
	 * @Description: SK初始化賬號Client
	 * @Author: tanyp
	 * @Date: 2021/6/7 15:44
	 * @Param: [accessId, accessKey, endpoint]
	 * @return: com.aliyun.teaopenapi.Client
	 **/
	public Client createClient() throws Exception {
		Config config = new Config();
		config.accessKeyId = smsConfig.getAccessId();
		config.accessKeySecret = smsConfig.getAccessKey();
		config.endpoint = smsConfig.getEndpoint();
		return new Client(config);
	}
	 * @MonthName: send
	 * @Description: 傳簡訊
	 * @Date: 2021/6/7 14:50
	 * @Param: [dto]
	 * @return: com.tanersci.vo.MessageVo
	@Override
	public MessageVo send(SmsMessageDto dto) {
		try {
			log.info("======傳送簡訊開始,請求引數:{}", JSON.toJSON(dto));
			Client client = createClient();
			// 組裝請求物件
			SendSmsRequest request = new SendSmsRequest();
			// 外部流水擴充套件欄位
			String outId = UUID.randomUUID().toString();
			request.setOutId(outId);
			// 支援對多個手機號碼傳送簡訊,手機號碼之間以英文逗號(,)分隔。上限為1000個手機號碼。批次呼叫相對於單條呼叫及時性稍有延遲。
			request.setPhoneNumbers(dto.getPhone());
			// 簡訊簽名名稱
			request.setSignName(smsConfig.getSignName());
			// 簡訊模板ID
			request.setTemplateCode(dto.getTemplateCode());
			// 簡訊模板變數對應的實際值,JSON格式。如果JSON中需要帶換行符,請參照標準的JSON協定處理。
			request.setTemplateParam(JSON.toJSONString(dto.getParam()));
			// 傳送簡訊
			SendSmsResponse res = client.sendSms(request);
			
			MessageVo message = MessageVo.builder().build();
			if (Objects.equals(Constants.NEWS_SUCCESS_CODE, res.body.getCode())) {
				log.info("======傳送簡訊成功,返回值:{}", JSON.toJSON(res.body));
				message.setCode(Constants.NEWS_SUCCESS_CODE);
				message.setMessage(Constants.NEWS_SUCCESS_MESSAGE);
			} else {
				log.info("======傳送簡訊失敗,返回值:{}", JSON.toJSON(res.body));
				message.setCode(Constants.NEWS_FAIL_CODE);
				message.setMessage(Constants.NEWS_FAIL_MESSAGE);
			}
			return message;
		} catch (Exception e) {
			log.error("======傳送簡訊異常:{}", e.getMessage());
			e.printStackTrace();
			return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
		}
	 * @MonthName: addSmsTemplate
	 * @Description: 申請簡訊模板
	 * @Param: [template]
	public MessageVo addSmsTemplate(SmsTemplateDto template) {
			log.info("======申請簡訊模板,請求引數:{}", JSON.toJSON(template));
			AddSmsTemplateRequest request = new AddSmsTemplateRequest();
			request.setTemplateType(template.getTemplateType());
			request.setTemplateName(template.getTemplateName());
			request.setTemplateContent(template.getTemplateContent());
			request.setRemark(template.getRemark());
			AddSmsTemplateResponse res = client.addSmsTemplate(request);
			if (Objects.equals(TeamConstants.NEWS_SUCCESS_CODE, res.body.getCode())) {
				log.info("======申請簡訊模板,返回值:{}", JSON.toJSON(res.body));
				return MessageVo.builder()
						.code(Constants.NEWS_SUCCESS_CODE)
						.message(Constants.NEWS_SUCCESS_MESSAGE)
						.templateCode(res.getBody().templateCode)
						.build();
				return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
			log.error("======申請簡訊模板,異常:{}", e.getMessage());
	 * @MonthName: deleteSmsTemplate
	 * @Description: 刪除簡訊模板
	public MessageVo deleteSmsTemplate(SmsTemplateDto template) {
			log.info("======刪除簡訊模板,請求引數:{}", JSON.toJSON(template));
			DeleteSmsTemplateRequest request = new DeleteSmsTemplateRequest();
			request.setTemplateCode(template.getTemplateCode());
			DeleteSmsTemplateResponse res = client.deleteSmsTemplate(request);
				log.info("======刪除簡訊模板,返回值:{}", JSON.toJSON(res.body));
				return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
			log.error("======刪除簡訊模板,異常:{}", e);
	 * @MonthName: modifySmsTemplate
	 * @Description: 修改未通過稽核的簡訊模板
	public MessageVo modifySmsTemplate(SmsTemplateDto template) {
			log.info("======修改未通過稽核的簡訊模板,請求引數:{}", JSON.toJSON(template));
			ModifySmsTemplateRequest request = new ModifySmsTemplateRequest();
			ModifySmsTemplateResponse res = client.modifySmsTemplate(request);
				log.info("======修改未通過稽核的簡訊模板,返回值:{}", JSON.toJSON(res.body));
			log.error("======修改未通過稽核的簡訊模板,異常:{}", e.getMessage());
	 * @MonthName: querySmsTemplate
	 * @Description: 查詢簡訊模板的稽核狀態
	public MessageVo querySmsTemplate(SmsTemplateDto template) {
			log.info("======查詢簡訊模板的稽核狀態,請求引數:{}", JSON.toJSON(template));
			QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
			QuerySmsTemplateResponse res = client.querySmsTemplate(request);
				log.info("======查詢簡訊模板的稽核狀態,返回值:{}", JSON.toJSON(res.body));
			log.error("======查詢簡訊模板的稽核狀態,異常:{}", e.getMessage());
}

2.3、SmsConfig.java

package com.tanersci.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
 * @ClassName: SmsConfig.java
 * @ClassPath: com.tanersci.config.SmsConfig.java
 * @Description: 簡訊設定
 * @Author: tanyp
 * @Date: 2021/6/7 16:41
 **/
@Data
@Component
public class SmsConfig {
	@Value("${sms.access-id}")
	private String accessId;
	@Value("${sms.access-key}")
	private String accessKey;
	@Value("${sms.sign-name}")
	private String signName;
	@Value("${sms.endpoint}")
	private String endpoint;
}

2.4、VO、DTO類

MessageVo 同用郵件的

MailMessageDto.java

package com.tanersci.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
 * @ClassName: MailMessageDto.java
 * @ClassPath: com.tanersci.dto.SmsMessageDto.java
 * @Description: 簡訊
 * @Author: tanyp
 * @Date: 2021/6/7 9:20
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "簡訊訊息")
public class SmsMessageDto implements Serializable {
	private static final long serialVersionUID = 3427970548460798908L;
	@ApiModelProperty(value = "手機號,多個以逗號隔開")
	private String phone;
	@ApiModelProperty(value = "模板編碼")
	private String templateCode;
	@ApiModelProperty(value = "模板引數")
	private TemplateParamDto param;
	private String code;
}

SmsTemplate.java

package com.tanersci.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
 * @ClassName: SmsTemplate.java
 * @ClassPath: com.tanersci.dto.SmsTemplateDto.java
 * @Description: 簡訊模板
 * @Author: tanyp
 * @Date: 2021/6/7 15:02
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SmsTemplateDto implements Serializable {
	private static final long serialVersionUID = -8909531614461840038L;
	/**
	 * 模板型別:0:驗證碼,1:簡訊通知,2:推廣簡訊,3:國際/港澳臺訊息。
	 */
	private Integer templateType;
	 * 模板名稱,長度為1~30個字元
	private String templateName;
	 * 模板內容,長度為1~500個字元
	private String templateContent;
	 * 簡訊模板CODE
	private String templateCode;
	 * 簡訊模板申請說明。請在申請說明中描述您的業務使用場景,長度為1~100個字元
	private String remark;
}

注意

專案中使用lombok外掛和swagger依賴,無相關依賴的請自行修改。

到此這篇關於SpringBoot 整合簡訊和郵件的文章就介紹到這了,更多相關SpringBoot 整合簡訊和郵件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com