Create an AccessKey
Log on to the
AccessKey console.
Click Create AccessKey at the upper-right corner of the page.
Read API Terms of Use, and then click Agree and Create.
Note: You can use the AccessKey created with Alibaba Cloud Resource Access Management service.
Install the Java SDK
Development environment
Alibaba Cloud Java SDK supports J2SE Development Kit (JDK) 1.6 or later.
Download the SDK
Click here to download the SDK directly.
Search and download the latest version of SDK.
Install the SDK
Install manually
Find the files “aliyun-java-sdk-dm-3.0.0-rc1.jar” and “aliyun-java-sdk-core-3.0.0.jar” in the downloaded package.
Add JARS.Take importing the JAR package in Eclipse as an example:
On Eclipse, right-click Project, and click Properties > Java Build Path > Libraries > Add External JARs (other idea users including “netbeans” and “intellij” must follow the appropriate method to import the JAR package).
Select the JAR packages and click OK.
Now you can use the Alibaba Cloud Java SDK in the project.
Install using maven
Add the maven library.
<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>
Add a JAR package.
<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>
Email sending example
Take calling the API to send a single email as an example (for how to call the interface, see SingleSendMail):
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 Sample {
public void singleSendMail() {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");//ap-southeast-1,ap-southeast-2
//Encrypted connections using HTTPS
//profile.getHttpClientConfig().setProtocolType(com.aliyuncs.http.ProtocolType.HTTPS);
// Other region except China east 1 (hangzhou), such as Singapore region, need to do the following treatment.
//try {
//DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
//} catch (ClientException e) {
//e.printStackTrace();
//}
IAcsClient client = new DefaultAcsClient(profile);
SingleSendMailRequest request = new SingleSendMailRequest();
try {
//request.setVersion("2017-06-22");// Other region except China east 1 (hangzhou), such as Singapore region, need to be specified for the 2017-06-22.
request.setAccountName("Mail address created in the console");
request.setFromAlias("Sender nickname");
request.setAddressType(1);
request.setTagName("Tag created in the console");
request.setReplyToAddress(true);
request.setToAddress("Destination address");
request.setSubject("Subject");
request.setHtmlBody("Body");
request.setMethod(MethodType.POST);//If the size of Textbody, htmlbody or content is uncertain, it is recommended to submit it by post to avoid URI is not valid exception
//request.setClickTrace("0");//0 off, 1 on ClickTrace
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();
}
}
}
Click here to get more tips for using Java SDKs.