This topic describes how to install and use the Java SDK.
Create an Access Key
(Note: You can also use the Access Key created by Alibaba Cloud RAM Service.)
Log on to the Access Key Management console.
Click Create Access Key in the upper-right corner of the page. The Create Access Key dialog box appears.
Read the API usage specifications and click Agree and Create.
Install the Java SDK
Development Environment
Alibaba Cloud Java SDK supports J2 SE Development Kit (JDK) 1.6 or later.
(Recommend)Install the SDK
Install through Maven
1. Add the Maven library.
You can obtain the new version number on the OpenAPI page and "SDK dependency information".
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dm20151123</artifactId>
<version>1.0.x</version>
</dependency>
2. Jump to openAPI for debugging, select the development language, enter parameters, and download the automatically generated code (the parameter value will be included).
You do not need to enter the Key value on the debugging page.
Do not hard coding the "accessKeyId" and "accessKeySecret" values in the code to avoid disclosure.
The Alibaba Cloud SDK supports defining defining the values of ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET in the environment variables of the operating system, after obtaining the values from the environment variables in the code, authentication can be performed.
Debugging
(Not recommend)The installation method of the old SDK.
SDK download
Direct download: https://aliyundm.oss-cn-hangzhou.aliyuncs.com/example/aliyun-java-sdk-dmV3.1.0.zip.
Use the following address to query and download the SDK.
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-umdatacloud</artifactId> <version>ali-jdk-8.2.4</version> </dependency> or: https://oss.sonatype.org/#nexus-search;gav~com.aliyun~aliyun-java-sdk-*~~~
Install the SDK
Manual installation
You can find the aliyun-java-sdk-dm-3.1.0.jar and aliyun-java-sdk-core-3.0.0.jar packages in the decompressed file.
Take Eclipse to import a JAR package as an example: Right-click the Project on Eclipse, and then click Properties > Java Build Path > Libraries > Add External JARs. For other idea users such as netbeans and intellij, please import the JAR package according to the corresponding method.
Select the preceding JAR package and click OK.
After the above steps, you can use Aliyun Java SDK in the project.
Install through Maven
Add a 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>
Example of sending email
For an example of how to call a single sender API, see the 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() {
//If you are not in the China (Hangzhou) region, replace cn-hangzhou in the following code with your region ID. For example, if you are in the Singapore (Singapore) , specify ap-southeast-1.
//Please configure in the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID,ALIBABA_CLOUD_ACCESS_KEY_SECRET.
//Reference documents:https://www.alibabacloud.com/help/en/directmail/latest/configure-authentication-accesskey-in-environment-variables
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
//If you are in a region other than China (Hangzhou), perform the following operation:
//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");// If it is a region other than the Hangzhou region, such as the Singapore region, it must be set to 2017-06-22.
request.setAccountName("The sender address created in the console");
request.setFromAlias("The sender's nickname");// The sender's nickname. The name must be less than 15 characters in length.
request.setAddressType(1);//0: is a random account 1: is a sender address.
request.setTagName("The tag created in the console");
request.setReplyToAddress(true);// Specifies whether to enable the return address configured in the management console (the status must be verified). The value range is true or false.
request.setToAddress("Destination address");
// You can send emails to multiple recipients. The recipients are separated by commas. BatchSendMailRequest method is recommended for batch sending.
// request.setToAddress("Email 1, Email 2");
request.setSubject("Email subject");
// If the byte[].toString method is used, make sure that it is finally converted into the utf-8 format and then put into the htmlbody and textbody. If the encoding is inconsistent, it will be regarded as spam.
// Note: The size of text mail is limited to 3M. Excessive text will cause connection timeout or 413 errors
request.setHtmlBody("Email body");
// The SDK uses the HTTP protocol to send messages. The default is the GET method, which has a certain length limit.
// If the size of the textBody, htmlBody, or content is uncertain, we recommend that you submit it in POST mode to avoid uri is not valid exceptions.
request.setMethod(MethodType.POST);
// Enable ICP filing, 0 disable, 1 enable
//request.setClickTrace("0");
// If the call is successful, httpResponse is returned. If the call fails, an exception is thrown. You must catch the error code in the exception. For more information about the error code, see the API documentation.
SingleSendMailResponse httpResponse = client.getAcsResponse(request);
} catch (ServerException e) {
// Capture the error exception code.
System.out.println("ErrCode : " + e.getErrCode());
e.printStackTrace();
}
catch (ClientException e) {
// Capture the error exception code.
System.out.println("ErrCode : " + e.getErrCode());
e.printStackTrace();
}
}
}
Click here for more tips about how to use the SDK for Java.
Also call the template batch to send letters, please use the BatchSendMailRequest, related modifications:
request.setTemplateName("test template");
request.setReceiversName("Test-test");
Where "test template" is the template name; "test-test" is the name of the recipient list.