All Products
Search
Document Center

Direct Mail:Java SDK manual

Last Updated:Nov 22, 2022

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

  1. Log on to the Access Key Management console.

  2. Click Create Access Key in the upper-right corner of the page. The Create Access Key dialog box appears.

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

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. When you use it in other environments, enter the corresponding "accessKeyId" and "accessKeySecret" values in the code.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

image

The installation method of the old SDK.

SDK download

Install the SDK

Manual installation

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

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

  3. Select the preceding JAR package and click OK.

After the above steps, you can use Aliyun Java SDK in the project.

Install through Maven

  1. 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>
  2. 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) or Australia (Sydney) region, specify ap-southeast-1 or ap-southeast-2. 
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
        //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.