参考文档:
如何在 DNS 服务器上配置域名:https://help.aliyun.com/knowledge_detail/39397.html?spm=5176.2020520150.102.d6.12837528Fc1Cy5
开通邮件服务:https://www.aliyun.com/product/directmail?spm=a2c4g.11186623.2.13.62122b31hNdHdL
API:https://help.aliyun.com/document_detail/29435.html
1. 首先得有个域名
2. 开通 邮件服务后,进入管理台,新增 发件域名,在弹出框中输入 域名,例如我的是 memory-back.club,然后点击配置
3. 拷贝配置到 域名解析配置处:
具体操作步骤:
(1)在解析设置页面添加 4条记录
第一条: 类型为 MX 的:
对应 收信配置中的 MX验证,类型、记录值一定要对应
第二条:类型为 TXT 的:
对应 发件设置中的 所有权验证,类型、主机记录、记录值一定要对应
第三条:类型为 TXT的:
对应 发信配置 中的 spf验证,类型、记录值 一定要对应
第四条:类型为 CNAME 的:
对应 跟踪配置中 的CNAME验证
4. 返回,点击 验证,点击确定
除了 spf验证 要慢点,其他的几乎秒级验证
当看到 状态 不为 待验证 时,即可使用
5. 添加 发件地址:
同时添加一下回信地址(就是用户 回复邮件了,发到哪个地址,如果不配置,用户回复了就无法收到),添加完后,点击验证回信地址,即确保回信邮箱是正常的
然后去回信邮箱点击 验证链接 即可验证成功
6. 服务端开发:
如果其他模块已经引入
aliyun-java-sdk-core
的其他版本,建议 邮件服务放入单独的模块,因为 该依赖版本较低,如果放一起,会造成依赖冲突
(1)引入依赖:
<artifactId>amazingcenter-email</artifactId>
<packaging>jar</packaging>
<name>amazingcenter-email</name>
<url>http://maven.apache.org</url>
<dependencies>
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>com.team.amazingcenter</groupId>
<artifactId>amazingcenter-common</artifactId>
</dependency>
</dependencies>
(2)实体类
package com.team.amazingcenter.email.vo;
import lombok.Data;
import java.util.Date;
/**
* @version DataEmailVO.java v2.0
* @description:
* @author: wenze
* @create: 2020/9/10 17:47
**/
@Data
public class DataEmailVO {
private String toAddress;
private String subject;
private String textBody;
private String attachUrl;
private Byte emailBusinessType;
private Byte sended;
private Date sendTime;
private String ip;
private String regionId;
private Integer addressType;
private Byte replyToAddress;
private String htmlBody;
}
(3)EmailUtils:
package com.team.amazingcenter.email.utils;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.BatchSendMailRequest;
import com.aliyuncs.dm.model.v20151123.BatchSendMailResponse;
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.profile.DefaultProfile;
import com.team.amazingcenter.common.enumerate.AcceptLanguageEnum;
import com.team.amazingcenter.common.enumerate.locale.LocaleEnum;
import com.team.amazingcenter.common.utils.PropertiesUtil;
import com.team.amazingcenter.email.vo.DataEmailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import java.util.Objects;
/**
* @version EmailUtils.java v2.0
* @description: 邮箱服务
* @author: wenze
* @create: 2020/9/9 21:44
* doc: https://help.aliyun.com/document_detail/29435.html
**/
@Slf4j
public class EmailUtils {
/\*\*
\* key
\*/
private static final String ACCESS\_KEY\_ID = PropertiesUtil.getProperty("api\_access\_key\_id");
/\*\*
\* secret
\*/
private static final String ACCESS\_SECRET = PropertiesUtil.getProperty("api\_access\_key\_secret");
/\*\*
\* 管理控制台中配置的发信地址 - 单个
\*/
private static final String ACCOUNT\_NAME\_SINGLE = PropertiesUtil.getProperty("email\_account\_name\_single");
/\*\*
\* 管理控制台中配置的发信地址 - 批量
\*/
private static final String ACCOUNT\_NAME\_BATCH = PropertiesUtil.getProperty("email\_account\_name\_batch");
/\*\*
\* 区域id - 国内
\*/
private static final String EMAIL\_REGION\_ID\_CN = PropertiesUtil.getProperty("email\_region\_id\_cn", "");
/\*\*
\* 区域id - 新加坡
\*/
private static final String EMAIL\_REGION\_ID\_1 = PropertiesUtil.getProperty("email\_region\_id\_1");
/\*\*
\* 区域id - 澳大利亚
\*/
private static final String EMAIL\_REGION\_ID\_2 = PropertiesUtil.getProperty("email\_region\_id\_2");
/\*\*
\* 地址类型。取值:0:为随机账号 1:为发信地址
\*/
private static final Integer ADDRESS\_TYPE = 1;
/\*\*
\* 发送邮件
\*/
public static DataEmailVO singleSendMail(SingleSendMailRequest request, String key) {
DataEmailVO emailVO = new DataEmailVO();
String regionId = EMAIL\_REGION\_ID\_CN;
if (StringUtils.isNotBlank(key) && StringUtils.equals(key, AcceptLanguageEnum.LANGUAGE\_EN\_US.getLanguage())) {
regionId = EMAIL\_REGION\_ID\_1;
}
DefaultProfile profile = DefaultProfile.getProfile(regionId, ACCESS\_KEY\_ID, ACCESS\_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
request.setAccountName(ACCOUNT\_NAME\_SINGLE);
request.setAddressType(ADDRESS\_TYPE);
request.setRegionId(regionId);
BeanUtils.copyProperties(request, emailVO);
emailVO.setReplyToAddress(request.getReplyToAddress() ? (byte) 1 : (byte) 0);
emailVO.setSended((byte) 2);
try {
SingleSendMailResponse response = client.getAcsResponse(request);
if (Objects.isNull(response) || ObjectUtils.isEmpty(response)) {
emailVO.setSended((byte) 1);
}
} catch (ServerException e) {
log.info("ServerException,ErrCode:{},ErrMsg:{},RequestId:{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
return new DataEmailVO();
} catch (ClientException e) {
log.info("ClientException,ErrCode:{},ErrMsg:{},RequestId:{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
return new DataEmailVO();
} catch (Exception e) {
log.info("OtherException,ErrCode:{},ErrMsg:{},RequestId:{}", e);
return new DataEmailVO();
}
return emailVO;
}
/\*\*
\* 批量发送邮件
\*/
public void batchSendMail(BatchSendMailRequest request, String key) {
String regionId = EMAIL\_REGION\_ID\_CN;
if (StringUtils.isNotBlank(key) && StringUtils.equals(key, LocaleEnum.ENG.getKey())) {
regionId = EMAIL\_REGION\_ID\_1;
}
DefaultProfile profile = DefaultProfile.getProfile(regionId, ACCESS\_KEY\_ID, ACCESS\_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
request.setRegionId(regionId);
try {
BatchSendMailResponse response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
(4)PropertiesUtil
package com.team.amazingcenter.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取公共配置文件
*
* @author juluo
*
*/
public class PropertiesUtil {
/\*\* 默认的properties \*/
private static final Properties DEFAULT\_PROPERTIES;
static {
DEFAULT\_PROPERTIES = getPropertiesByFileName("common.properties");
}
/\*\*
\* 获取属性对应的值
\*
\* @param property
\* 属性
\* @param defaultValue
\* 没找到值返回的默认值
\* @return property 对应的值
\*/
public static String getProperty(String property, String defaultValue) {
return DEFAULT\_PROPERTIES.getProperty(property, defaultValue);
}
/\*\*
\* 获取属性对应的值
\*
\* @param property
\* 属性
\* @return property 对应的值 ,or {@code null}
\*/
public static String getProperty(String property) {
return DEFAULT\_PROPERTIES.getProperty(property);
}
/\*\*
\* 通过文件路径载入数据
\*
\* @param fileName
\* 文件路径
\* @return {@link Properties}
\*/
public static Properties getPropertiesByFileName(String fileName) {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
Properties p = new Properties();
if (inputStream == null) {
return p;
}
try {
p.load(inputStream);
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return p;
}
/\*\*
\* 通过属性获取指定文件的值
\*
\* @param fileName
\* 文件名
\* @param propertie
\* 属性
\* @param defaultValue
\* 默认值
\* @return 属性对应的值
\*/
public static String getProperty(String fileName, String propertie, String defaultValue) {
Properties p = getPropertiesByFileName(fileName);
return p.getProperty(propertie, defaultValue);
}
public static void main(String\[\] args) {
System.out.println(PropertiesUtil.getProperty("file\_oss\_storage\_path", ""));
}
}
其他模块需要使用时,直接通过 依赖引入的方式即可使用 邮箱服务
(5)业务层:
SingleSendMailRequest request = new SingleSendMailRequest();
request.setToAddress(email);
request.setSubject(I18nMessageUtil.getMessage(acceptLanguage, VerCodeTypeEnum.getDescByDesc(type), ""));
request.setTextBody(I18nMessageUtil.getMessage(acceptLanguage, I18nCodeEnum.PIN.getMsg(), "").replace("code", code));
request.setReplyToAddress(true);
DataEmailVO vo = EmailUtils.singleSendMail(request, acceptLanguage);
手机扫一扫
移动阅读更方便
你可能感兴趣的文章