All Products
Search
Document Center

Direct Mail:CSharp SDK Manual(China)

Last Updated:Jun 14, 2023

This topic describes how to install and use the C# SDK.

Create an AccessKey

(Note: You can also use the AccessKey created by Alibaba Cloud RAM Service.)

  1. Log on to the AccessKey Management console.

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

  3. Read the API usage specifications and click Agree and Create.

(Recommend)Install the C# SDK

Development Environment

The C# SDK Direct Mail by Alibaba Cloud supports. Net Framework 2.0 or later.

Install the SDK

Install through dotnet

1. Add dependency package

You can obtain the new version number on the OpenAPI page and "SDK dependency information".

dotnet add package AlibabaCloud.SDK.Dm20151123 --version 1.0.x
Important

"version 1.0.x" is an example. Please enter the latest version obtained from openAPI page in actual use.

2. Jump to openAPI for debugging, select the development language, enter parameters, and download the automatically generated code (the parameter value will be included).

The debugging page does not need to fill in the Key value.

Warning

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

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

(Not recommend)The installation method of the old SDK.

Direct download: https://aliyundm.oss-cn-hangzhou.aliyuncs.com/example/aliyun-net-sdk-dmV3.1.0.zip.

Install the SDK

1. You can find the aliyun-net-sdk-core.dll and aliyun-net-sdk-dm.dll files in the decompressed file.

2. Right-click the project in Visual Studio, and then choose Add Reference > Browse. If you are using MonoDevelop, import the. dll file as required. Select the. dll file and click OK.

You can use Alibaba Cloud Direct Mail C# SDK in your project.

Example of Sending Emails

Example of how to call a single sender API (see SingleSendMail ). If you need to know the details of GetProfile and DefaultAcsClient in the sample code, you can view the source code:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dm.Model.V20151123;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a client used for initiating a request
			//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", Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendMailRequest request = new SingleSendMailRequest();
            try {
                //Version must set to "2017-06-22" when the regionId is not "cn-hangzhou"
                //request.Version = "2017-06-22";
                request.AccountName = "The sender address created in the console";
                request.FromAlias = "Sender's nickname";
                request.AddressType = 1;
                request.TagName = "The tag created in the console";
                request.ReplyToAddress = true;
                request.ToAddress = "Destination address";
                request.Subject = "Email subject" ;
                request.HtmlBody = "Email body" ;
                SingleSendMailResponse httpResponse = client.GetAcsResponse(request);
            } catch (ServerException e) {
                System.Console.WriteLine(e.ToString());
            }
            catch (ClientException e) {
                System.Console.WriteLine(e.ToString());
            }
        }
    }
}