All Products
Search
Document Center

Direct Mail:Csharp SDK Manual(Australia and Singapore Region)

Last Updated:Jan 23, 2024

Create An AccessKey

  1. Log on to the AccessKey Console.

  2. Click Create AccessKey at the upper-right corner of the page.

  3. Read API Terms of Use, and click Agree and Create.

Note: You can use the AccessKey of ram users granted with system policy “AliyunDirectMailFullAccess”. Ram users can be created with Alibaba Cloud Resource Access Management service.

(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 code the "accessKeyId" and "accessKeySecret" values in the code to avoid disclosure.

The Alibaba Cloud SDK supports 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 Recommended) Install the C# SDK

Requirements

The Alibaba Cloud SDK for .NET requires:

    .NET Framework 4.6.1 or later
    .NET Standard 2.0 or later

Install the SDK

You must install the SDK core Nuget Package for any SDK you use.

Use Package Manager

    Install-Package aliyun-net-sdk-core
    Install-Package aliyun-net-sdk-dm-overseas -Version 4.7.2

You can also use the Visual Studio to install for .NET Framework Application referring to https://docs.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio with searching our SDK name “aliyun-net-sdk-dm-overseas” in the package browser.

Email Sending Example

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

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {      
            //Region-Id: ap-southeast-1 for Singapore Region and ap-southeast-2 for Australlia Region
            string regionId = "ap-southeast-1";

            //Region-Host: dm.ap-southeast-1.aliyuncs.com for Singapore Region and dm.ap-southeast-2.aliyuncs.com for Australlia Region
            string regionHost = "dm.ap-southeast-1.aliyuncs.com";

			//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(regionId, Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            IAcsClient client = new DefaultAcsClient(profile);


            DefaultProfile.AddEndpoint(regionHost, regionId, "Dm", regionHost);
            SingleSendMailRequest request = new SingleSendMailRequest();
            try {
                request.AccountName = "Sender address created in the console";
                request.FromAlias = "Sender nickname";
                request.AddressType = 1;
                request.TagName = "Tag created in the console";
                request.ReplyToAddress = true;
                request.ToAddress = "Destination address";
                request.Subject = "Subject";
                request.HtmlBody = "Body";
                SingleSendMailResponse httpResponse = client.GetAcsResponse(request);
                System.Console.WriteLine(httpResponse.RequestId);
            } catch (ServerException e) {
                System.Console.WriteLine(e.ToString());
            }
            catch (ClientException e) {
                System.Console.WriteLine(e.ToString());
            }
        }
    }
}