最近学了点关于阿里云邮件服务的相关内容,写点随笔记录一下。
首先写一下maven文件下。pom.xml需要的相关内容:
com.aliyun
aliyun-java-sdk-core
3.0.0
com.aliyun
aliyun-java-sdk-dm
3.0.0-rc2
之后写一下具体的测试controller类该怎么写;
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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
以上为controller类中导包部分;
public class EmailByAliyun{
private static String ACCESS_KEY_ID = "*********";
private static String ACCESS_KEY_SECRET = "*********";
private static String NOREPLY_ADDRESS = "[email protected]"; //邮件发送者地址
private static String NOTIFY_ADDRESS = "[email protected]";
//这是一个简单的单一发信接口test案例
public boolean singletest(){
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try {
request.setAccountName(NOREPLY_ADDRESS);
request.setFromAlias("****");
request.setAddressType(1); //取值为0-1,0为随机账号,1为发信地址
request.setTagName("NOREPLY");
request.setReplyToAddress(false); //是否使用管理控制台中配置的回信地址
request.setToAddress("[email protected],[email protected]"); //目标地址,多个Email地址可以逗号分隔,最多100个地址。
request.setSubject("触发邮件测试"); //发送邮件标题
request.setHtmlBody("这是一封触发邮件"); //发送邮件内容
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
return false;
}
return true;
}
// 这个是一个实际的发送单个邮件的类
public boolean sendMail(Email amail){
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try{
request.setAccountName(NOREPLY_ADDRESS);
request.setFromAlias("****");
request.setAddressType(1);
request.setReplyToAddress(false);
if (email.getSubject() != null && email.getSubject().length() != 0)
request.setSubject(email.getSubject());
if (email.getHtmlBody() != null && email.getHtmlBody().length() != 0)
request.setHtmlBody(email.getHtmlBody());
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
}catch (Exception e) {
logger.error("sent email error : ", e);
return false;
}
return true;
}
以上就是具体的实现返回值类型根据自己真实需要的情况返回即可,我选择的是返回布尔型,目的是用于是否发送成功的判定。
具体的使用还是需要参照阿里云邮件推送API参考手册。
第二版 关于阿里云批量发送邮件的解决方法
对于批量发送,其实并没有单独的处理方法,选用的方法很简单,如果你看懂了单一接口发信方式,那么批量发送就是写一个for循环,for循环的判定条件就是你的批量目标地址,之后在for循环里调用之前的方法就可以了,我可以写一个简单的例子给大家作为参考,这个例子也是调试了很久,因为之前我没有用调用方法,而是直接在for循环中重新实现了一边单个接口发送的方法,但是最后结果是不能实现批量发送,假设我想要发送2万封,大约一分钟1万封左右的的样子,一分钟大约可以发送10000封。
public boolean sendEmail(Email r) {
try{
List listAddress = new ArrayList();
for (User u : Uservalues()) {
listAddress.add(u.getEmail());
}
List> targetList = new ArrayList>();
targetList = divideGroup(listAddress);
for(List list : targetList){
String targetEmail = jointTargetEmail(list);
sendListEmail(targetEmail,r);
}
}catch (Exception e) {
logger.error("sent email error : ", e);
return false;
}
return true;
}
private void sendListEmail(String targetEmail,Email r){
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try {
request.setAccountName(NOREPLY_ADDRESS);
request.setFromAlias("");
request.setAddressType(1);
request.setReplyToAddress(false);
if (r.getSubject() != null && r.getSubject().length() != 0)
request.setSubject(r.getSubject());
if (r.getBody() != null && r.getBody().length() != 0)
request.setHtmlBody(r.getBody());
request.setToAddress(targetEmail);
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (Exception e) {
logger.error("sent email error : ", e);
}
}
//此方法的作用就是将100个邮件地址连接起来,每个邮件之间以逗号隔开,然后最后一个地址后面没有逗号也就是{[email protected],[email protected],[email protected]}
private String jointTargetEmail(List targetList) {
具体写法不能公布
}
//该方法采用二维集合的形式对于批量邮件每一百封邮件进行合并为一个集合
private List> divideGroup(List listAddress){
具体写法不能公布
}