[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Serverless technologies](index.md) > Sending emails in Yandex Cloud Postbox using the AWS SDK > .NET Core

# Sending emails using the AWS SDK for .NET Core

In this tutorial, you will learn how to send emails via Yandex Cloud Postbox using the [AWS SDK for .Net Core](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-sdk-features.html).

To start sending emails:

1. [Get your cloud ready](#before-begin).
1. [Configure a directory for authentication data](#auth).
1. [Create and run the application](#app).
1. [Check the result](#check-result).

If you no longer need the resources you created, [delete them](#clear-out).


## Get your cloud ready {#before-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).


### Required paid resources {#paid-resources}

The infrastructure support costs include:

* Fee for using Yandex Cloud Postbox (see [Yandex Cloud Postbox pricing](../../postbox/pricing.md)).
* Fee for public DNS queries and [DNS zones](../../dns/concepts/dns-zone.md), if you are creating a resource record in Cloud DNS (see [Cloud DNS pricing](../../dns/pricing.md)).


### Set up resources {#infrastructure}

1. [Create](../../iam/operations/sa/create.md) a service account.
1. [Assign](../../iam/operations/sa/assign-role-for-sa.md) the `postbox.sender` [role](../../postbox/security/index.md#postbox-sender) to the service account.
1. [Create](../../iam/operations/authentication/manage-access-keys.md) a static access key for the service account. Save the ID and secret key.
1. Create an [address](../../postbox/operations/create-address.md).
1. [Pass](../../postbox/operations/check-domain.md) a domain ownership check.


## Configure a directory for authentication data {#auth}

1. Create a directory to store the authentication data in and navigate to it: 

    For macOS and Linux:

    ```bash
    mkdir ~/.aws/
    ```

    For Windows:

    ```bash
    mkdir C:\Users\<user_name>\.aws\
    ```

1. In the `.aws` directory, create a file named `credentials`, copy the credentials you got when [creating a static access key](#infrastructure), and paste them into it:

    ```text
    [default]
    aws_access_key_id = <static_key_ID>
    aws_secret_access_key = <secret_key>
    ```

1. Create a file named `config` with the default region settings and copy the following information to it:

    ```text
    [default]
    region = ru-central1
    endpoint_url = https://postbox.cloud.yandex.net
    ```


### Using environment variables {#variables}

By default, the AWS SDK uses authentication data from environment variables if they are set. These variables have priority over authentication data from the `.aws/credentials` file.

The following environment variables are supported:

* `AWS_ACCESS_KEY_ID`: Static key ID.
* `AWS_SECRET_ACCESS_KEY`: Secret key.

To set environment variables, depending on your operating system, follow these steps:

{% list tabs group=operating_system %}

- Linux/macOS

    In the terminal, run this command:

    ```bash
    export AWS_ACCESS_KEY_ID=<static_key_ID>
    export AWS_SECRET_ACCESS_KEY=<secret_key>
    ```

- Windows

    In PowerShell, run:

    ```powershell
    $Env:AWS_ACCESS_KEY_ID=<static_key_ID>
    $Env:AWS_SECRET_ACCESS_KEY=<secret_key>
    ```

{% endlist %}


## Create and run the application {#app}

1. Get the application code:

    {% list tabs %}

    - Repository

      1. Clone the repository:
         
         ```bash
         git clone https://github.com/yandex-cloud-examples/yc-postbox-examples
         ```
      1. Navigate to the folder in the cloned `csharp/Postbox/Postbox/` repository.
      1. In the `Program.cs` file, specify the following:
         
         * Recipient's email address in the `ToAddresses` field, e.g., `receiver@yourdomain.com`. You will need access to this email address for the next step.
         * Sender's email address in the `FromEmailAddress` field.
         
             The sender's email domain must match the one specified in the Yandex Cloud Postbox address you created when [getting started](#infrastructure). For example, if your verified domain is `yourdomain.com`, you can use addresses like `noreply@yourdomain.com` or `admin@yourdomain.com` but not `user@mail.yourdomain.com`.

    - Manually

      1. Create a directory named `postbox-csharp` and open it.
      1. Create a file named `Program.cs` and paste this code into it:

          ```csharp
          using Amazon.Runtime;
          using Amazon.SimpleEmailV2;
          using Amazon.SimpleEmailV2.Model;

          var client = new AmazonSimpleEmailServiceV2Client(
              new AmazonSimpleEmailServiceV2Config
              {
                  ServiceURL = "https://postbox.cloud.yandex.net",
                  SignatureMethod = SigningAlgorithm.HmacSHA256,
                  SignatureVersion = "4",
                  AuthenticationRegion = "ru-central1",
              }
          );

          try
          {
              var response = await client.SendEmailAsync(
                  new SendEmailRequest
                  {
                      Destination = new Destination
                      {
                          ToAddresses = ["<recipient_address>"]
                      },
                      Content = new EmailContent
                      {
                          Simple = new Message
                          {
                              Body = new Body
                              {
                                  Text = new Content
                                  {
                                      Charset = "UTF-8",
                                      Data = "Hello, world!"
                                  }
                              },
                              Subject = new Content
                              {
                                  Charset = "UTF-8",
                                  Data = "Test email"
                              }
                          }
                      },
                      FromEmailAddress = "<sender_address>"
                  });

              Console.Write(response.MessageId);
          }
          catch (Exception ex)
          {
              // Logging exceptions in JSON format
              Console.WriteLine(ex);
          }
          ```

      1. In the `Program.cs` file, specify the following:
         
         * Recipient's email address in the `ToAddresses` field, e.g., `receiver@yourdomain.com`. You will need access to this email address for the next step.
         * Sender's email address in the `FromEmailAddress` field.
         
             The sender's email domain must match the one specified in the Yandex Cloud Postbox address you created when [getting started](#infrastructure). For example, if your verified domain is `yourdomain.com`, you can use addresses like `noreply@yourdomain.com` or `admin@yourdomain.com` but not `user@mail.yourdomain.com`.
      1. Create a file named `Postbox.csproj` and paste this code into it:

          ```xml
          <Project Sdk="Microsoft.NET.Sdk">

              <PropertyGroup>
                  <OutputType>Exe</OutputType>
                  <TargetFramework>net8.0</TargetFramework>
                  <ImplicitUsings>enable</ImplicitUsings>
                  <Nullable>enable</Nullable>
              </PropertyGroup>

              <ItemGroup>
                <PackageReference Include="AWSSDK.SimpleEmailV2" Version="3.7.408.17" />
              </ItemGroup>

          </Project>
          ```

      1. Create a file named `Postbox.sln` and paste this code into it:

          ```text
          Microsoft Visual Studio Solution File, Format Version 12.00
          Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Postbox", "Postbox\Postbox.csproj", "{51272D55-8BA2-4072-9445-889E627E605D}"
          EndProject
          Global
            GlobalSection(SolutionConfigurationPlatforms) = preSolution
              Debug|Any CPU = Debug|Any CPU
              Release|Any CPU = Release|Any CPU
            EndGlobalSection
            GlobalSection(ProjectConfigurationPlatforms) = postSolution
              {51272D55-8BA2-4072-9445-889E627E605D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
              {51272D55-8BA2-4072-9445-889E627E605D}.Debug|Any CPU.Build.0 = Debug|Any CPU
              {51272D55-8BA2-4072-9445-889E627E605D}.Release|Any CPU.ActiveCfg = Release|Any CPU
              {51272D55-8BA2-4072-9445-889E627E605D}.Release|Any CPU.Build.0 = Release|Any CPU
            EndGlobalSection
          EndGlobal
          ```

    {% endlist %}

  1. Run the application:

      ```bash
      dotnet run
      ```

      Result:

      ```text
      DB42OLB6KLNJ.7VDE********@ingress2-klg
      ```


## Check the result {#check-result}

Make sure the recipient specified in the file named `Program.cs` in the `ToAddresses` field has received an email with the specified parameters.


## How to delete the resources you created {#clear-out}

To stop paying for the resources you created:

1. Delete the [address](../../postbox/concepts/glossary.md#adress).
1. [Delete](../../dns/operations/zone-delete.md) the DNS zone if you created a resource record in it.