当前位置:   article > 正文

SpringBoot整合aliyun邮件发送

SpringBoot整合aliyun邮件发送

本文介绍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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.配置邮件参数(方便管理,增加代码复用性)

#邮箱服务器配置,获取方式参考aliyun邮件发送文档
ali.email.regionId=
ali.email.accessKeyId=
ali.email.secret=
ali.email.accountName=
ali.email.fromAlias=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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("垃圾回收");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/201282
推荐阅读
相关标签
  

闽ICP备14008679号