Java SDK tutorial

Last Updated: Sep 26, 2017

Create an AccessKey

  1. Log on to the Access Key console.
  2. Click Create Access Key at the upper right corner of the page.
  3. 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

Install the SDK

Install manually

  1. 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.

  2. 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).

  3. Select the JAR packages and click OK.

Now you can use the Alibaba Cloud Java SDK in the project.

Install using maven

  1. Add the maven library.
    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. Add a JAR package.
    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>

Email sending example

Take calling the API to send a single email as an example (For how to call the interface, see SingleSendMail ):

  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. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
  11. // Other region except China east 1 (hangzhou), such as Singapore region, need to do the following treatment.
  12. //try {
  13. //DefaultProfile.addEndpoint("dm.ap-southeast-1.aliyuncs.com", "ap-southeast-1", "Dm", "dm.ap-southeast-1.aliyuncs.com");
  14. //} catch (ClientException e) {
  15. //e.printStackTrace();
  16. //}
  17. IAcsClient client = new DefaultAcsClient(profile);
  18. SingleSendMailRequest request = new SingleSendMailRequest();
  19. try {
  20. //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.
  21. request.setAccountName("Mail address created in the console");
  22. request.setFromAlias("Sender nickname");
  23. request.setAddressType(1);
  24. request.setTagName("Tag created in the console");
  25. request.setReplyToAddress(true);
  26. request.setToAddress("Destination address");
  27. request.setSubject("Subject");
  28. request.setHtmlBody("Body");
  29. SingleSendMailResponse httpResponse = client.getAcsResponse(request);
  30. } catch (ServerException e) {
  31. e.printStackTrace();
  32. }
  33. catch (ClientException e) {
  34. e.printStackTrace();
  35. }
  36. }

Click here to get more tips for using Java SDKs.

Thank you! We've received your feedback.