赞
踩
本文介绍aliyun邮件发送 Java SDK的使用
文档地址:https://help.aliyun.com/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>https://oss.sonatype.org/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.v20151123.SingleSendMailRequest; import com.aliyuncs.dm.model.v20151123.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; @Before public void setUp() { System.out.println("初始化"); } /** * 发送邮件 */ @Test public 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@qq.com"); 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(); } } @After public void tearDown() { System.out.println("垃圾回收"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。