阿里云的发送邮件的jar包下载地址: http://aliyundm.oss-cn-hangzhou.aliyuncs.com/example/aliyun-java-sdk-dmV3.1.0.zip
手动安装
- 在解压的文件中可以找到 aliyun-java-sdk-dm-3.0.0-rc1.jar 和 aliyun-java-sdk-core-3.0.0.jar 包。
- 以 Eclipse 导入 JAR 包为例:在 Eclipse 上右键点击 Project,再单击 Properties > Java Build Path > Libraries > Add External JARs(netbeans 和 intellij 等其他 idea 用户请自行按照相应导入 JAR 包的方法导入)。
- 选择以上 JAR 包,点击 OK 按钮。
通过 maven 进行安装
- 添加 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>
- 添加 JAR 包
<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>
<dependencies>
发送邮件示例:
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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public void sample() {
// 如果是除杭州region外的其它region(如新加坡、澳洲Region),需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"。
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
// 如果是除杭州region外的其它region(如新加坡region), 需要做如下处理
//DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try {
//request.setVersion("2017-06-22");// 如果是除杭州region外的其它region(如新加坡region),必须指定为2017-06-22
request.setAccountName("控制台创建的发信地址")
request.setFromAlias("发信人昵称")
request.setAddressType(1)
request.setTagName("控制台创建的标签")
request.setReplyToAddress(true);
request.setToAddress("目标地址")
request.setSubject("邮件主题")
request.setHtmlBody("邮件正文")
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (ServerException e) {
e.printStackTrace()
}
catch (ClientException e) {
e.printStackTrace()
}
}
|