阿里云的发送邮件

时间:2022-09-24 12:57:34

阿里云的发送邮件的jar包下载地址: http://aliyundm.oss-cn-hangzhou.aliyuncs.com/example/aliyun-java-sdk-dmV3.1.0.zip

手动安装

  1. 在解压的文件中可以找到 aliyun-java-sdk-dm-3.0.0-rc1.jar 和 aliyun-java-sdk-core-3.0.0.jar 包。
  2. 以 Eclipse 导入 JAR 包为例:在 Eclipse 上右键点击 Project,再单击 Properties > Java Build Path > Libraries > Add External JARs(netbeans 和 intellij 等其他 idea 用户请自行按照相应导入 JAR 包的方法导入)。
  3. 选择以上 JAR 包,点击 OK 按钮。

通过 maven 进行安装

  1. 添加 maven 库
     
      
    1. <repositories>
    2. <repository>
    3. <id>sonatype-nexus-staging</id>
    4. <name>Sonatype Nexus Staging</name>
    5. <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    6. <releases>
    7. <enabled>true</enabled>
    8. </releases>
    9. <snapshots>
    10. <enabled>true</enabled>
    11. </snapshots>
    12. </repository>
    13. </repositories>
  2. 添加 JAR 包
     
      
    1. <dependencies>
    2. <dependency>
    3. <groupId>com.aliyun</groupId>
    4. <artifactId>aliyun-java-sdk-core</artifactId>
    5. <version>3.0.0</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>com.aliyun</groupId>
    9. <artifactId>aliyun-java-sdk-dm</artifactId>
    10. <version>3.1.0</version>
    11. </dependency>
    12. <dependencies>

发送邮件示例:

  1. import com.aliyuncs.DefaultAcsClient;
  2. import com.aliyuncs.IAcsClient;
  3. import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
  4. import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.exceptions.ServerException;
  7. import com.aliyuncs.profile.DefaultProfile;
  8. import com.aliyuncs.profile.IClientProfile;
  9. public void sample() {
  10. // 如果是除杭州region外的其它region(如新加坡、澳洲Region),需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"
  11. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
  12. // 如果是除杭州region外的其它region(如新加坡region), 需要做如下处理
  13. //try {
  14. //DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
  15. //} catch (ClientException e) {
  16. //e.printStackTrace();
  17. //}
  18. IAcsClient client = new DefaultAcsClient(profile);
  19. SingleSendMailRequest request = new SingleSendMailRequest();
  20. try {
  21. //request.setVersion("2017-06-22");// 如果是除杭州region外的其它region(如新加坡region),必须指定为2017-06-22
  22. request.setAccountName("控制台创建的发信地址");
  23. request.setFromAlias("发信人昵称");
  24. request.setAddressType(1);
  25. request.setTagName("控制台创建的标签");
  26. request.setReplyToAddress(true);
  27. request.setToAddress("目标地址");
  28. request.setSubject("邮件主题");
  29. request.setHtmlBody("邮件正文");
  30. SingleSendMailResponse httpResponse = client.getAcsResponse(request);
  31. } catch (ServerException e) {
  32. e.printStackTrace();
  33. }
  34. catch (ClientException e) {
  35. e.printStackTrace();
  36. }
  37. }