package com.hys.app.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
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.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class AliEmailUtil {
    private static String  accountName ="136560217@qq.com";

    public static void sendEmail() {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                    .accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                    .build());

            AsyncClient client = AsyncClient.builder()
                    .region("cn-hangzhou")
                    .credentialsProvider(provider)
                    .overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride("dm.aliyuncs.com"))
                    .build();
            SingleSendMailRequest singleSendMailRequest = SingleSendMailRequest
                    .builder()
                    .accountName(accountName)
                    .tagName("test")
                    .replyToAddress(false)
                    .toAddress("136560217@qq.com")
                    .subject("测试邮件")
                    .htmlBody("测试邮件")
                    .addressType(1)
                    .addressType(1)
                    .build();

            CompletableFuture<SingleSendMailResponse> response = client.singleSendMail(singleSendMailRequest);
            SingleSendMailResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


