- 添加 Maven 依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dm20151123</artifactId>
<version>1.0.5</version>
</dependency>
- springboot 配置
package cn.devcxl.mail.push.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @author devcxl
*/
@Data
@ConfigurationProperties(prefix = "spring.alibaba.mail")
public class PushMailConfigProperties {
/**
* your-accessKeyId
*/
private String accessKeyId;
/**
* your-accessKeySecret
*/
private String accessKeySecret;
/**
* 区域
*/
private String region;
/**
* 请求域名
*/
private String endpointOverride;
/**
* 连接超时时间 单位秒
*/
private Integer connectTimeOut;
}
- 配置 EmailSender
package cn.devcxl.mail.push.config;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dm20151123.AsyncClient;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
/**
* @author devcxl
*/
@Configuration
@EnableConfigurationProperties(PushMailConfigProperties.class)
public class EmailSenderConfiguration {
@Bean
public AsyncClient mailAsyncClient(PushMailConfigProperties pushMailConfigProperties){
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(pushMailConfigProperties.getAccessKeyId())
.accessKeySecret(pushMailConfigProperties.getAccessKeySecret())
//.securityToken("<your-token>") // use STS token
.build());
// Configure the Client
return AsyncClient.builder()
.region(pushMailConfigProperties.getRegion())
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride(pushMailConfigProperties.getEndpointOverride())
.setConnectTimeout(Duration.ofSeconds(pushMailConfigProperties.getConnectTimeOut()))
)
.build();
}
}
- 创建 EmailSender
package cn.devcxl.mail.push.components;
import cn.devcxl.cryptopay.common.constant.MqConstant;
import cn.devcxl.mail.push.domain.EmailContent;
import com.aliyun.sdk.service.dm20151123.AsyncClient;
import com.aliyun.sdk.service.dm20151123.models.SingleSendMailRequest;
import com.aliyun.sdk.service.dm20151123.models.SingleSendMailResponse;
import com.aliyun.sdk.service.dm20151123.models.SingleSendMailResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
/**
* @author orange
*/
@Slf4j
@Component
public class EmailSender {
private AsyncClient mailAsyncClient;
public EmailSender(AsyncClient mailAsyncClient) {
this.mailAsyncClient = mailAsyncClient;
}
public void send(EmailContent content) {
// Parameter settings for API request
SingleSendMailRequest singleSendMailRequest = SingleSendMailRequest.builder()
.accountName(content.getAccountName())
.addressType(content.getAddressType())
.tagName(content.getTagName())
.replyToAddress(content.isReplyToAddress())
.toAddress(String.join(",", content.getToAddress()))
.subject(content.getSubject())
.htmlBody(content.getHtmlBody())
.textBody(content.getTextBody())
.fromAlias(content.getFromAlias())
.replyAddress(content.getReplyAddress())
.replyAddressAlias(content.getReplyAddressAlias())
.clickTrace(content.getClickTrace().toString())
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<SingleSendMailResponse> response = mailAsyncClient.singleSendMail(singleSendMailRequest);
// 完成进度
response.thenAccept(result -> {
SingleSendMailResponseBody body = result.getBody();
String requestId = body.getRequestId();
String envId = body.getEnvId();
log.info("邮件发送成功requestId:{},envId:{}",requestId, envId);
});
}
}