1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > SpringBoot整合aliyun邮件发送

SpringBoot整合aliyun邮件发送

时间:2019-04-13 09:30:54

相关推荐

SpringBoot整合aliyun邮件发送

本文介绍aliyun邮件发送 Java SDK的使用

文档地址:/document_detail/29459.html?spm=a2c4g.11186623.6.635.28ae3a35TjrYsQ

1.添加 maven 库

<repositories><repository><id>sonatype-nexus-staging</id><name>Sonatype Nexus Staging</name><url>/service/local/staging/deploy/maven2/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories>

2.添加JAR 包依赖

<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>3.0.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dm</artifactId><version>3.1.0</version></dependency>

3.配置邮件参数(方便管理,增加代码复用性)

#邮箱服务器配置,获取方式参考aliyun邮件发送文档ali.email.regionId=ali.email.accessKeyId=ali.email.secret=ali.email.accountName=ali.email.fromAlias=

4.邮件发送demo(SpringBoot)

import org.junit.After;import org.junit.Before;import org.junit.Test;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.dm.model.v1123.SingleSendMailRequest;import com.aliyuncs.dm.model.v1123.SingleSendMailResponse;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.exceptions.ServerException;import com.aliyuncs.http.MethodType;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.profile.IClientProfile;/*** 邮件测试*/public class MailApp {/*** 配置信息*/@Value("${ali.email.regionId}")private String REGION_ID;@Value("${ali.email.accessKeyId}")private String ACCESS_KEY_ID;@Value("${ali.email.secret}")private String SECRET;@Value("${ali.email.accountName}")private String ACCOUNT_NAME;@Value("${ali.email.fromAlias}")private String FROM_ALIAS;@Beforepublic void setUp() {System.out.println("初始化");}/*** 发送邮件*/@Testpublic void sendMail() {System.out.println("发送邮件中...");IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, SECRET);IAcsClient client = new DefaultAcsClient(profile);SingleSendMailRequest request = new SingleSendMailRequest();try {request.setAccountName(ACCOUNT_NAME);request.setFromAlias(FROM_ALIAS);request.setAddressType(1);request.setToAddress("ceshi@");request.setReplyToAddress(true);//可以给多个收件人发送邮件,收件人之间用逗号分开,批量发信建议使用BatchSendMailRequest方式//request.setToAddress("邮箱1,邮箱2");request.setSubject("测试主题");//如果采用byte[].toString的方式的话请确保最终转换成utf-8的格式再放入htmlbody和textbody,若编码不一致则会被当成垃圾邮件。//注意:文本邮件的大小限制为3M,过大的文本会导致连接超时或413错误request.setHtmlBody("测试邮件正文");//SDK 采用的是http协议的发信方式, 默认是GET方法,有一定的长度限制。//若textBody、htmlBody或content的大小不确定,建议采用POST方式提交,避免出现uri is not valid异常request.setMethod(MethodType.POST);SingleSendMailResponse httpResponse = client.getAcsResponse(request);} catch (ServerException e) {//捕获错误异常码System.out.println("ErrCode : " + e.getErrCode());e.printStackTrace();} catch (ClientException e) {//捕获错误异常码System.out.println("ErrCode : " + e.getErrCode());e.printStackTrace();}}@Afterpublic void tearDown() {System.out.println("垃圾回收");}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。