为什么我无法使用AWS SES接收电子邮件。净

问题描述 投票:1回答:1

我使用AWS网页上的示例来检查我是否可以使用AWS SES SDK接收电子邮件。

它显示电子邮件已成功发送,但在我查看我的电子邮件帐户时未收到任何电子邮件。

发件人的电子邮件已经过验证。

代码与给定的简单相同。只有电子邮件地址不同。

当我在VS 2017中粘贴代码时,Client.SendEmail()会引发错误。

我将其修改为推荐给Client.SendEmailAsync()。不知道问题云在哪里。

using Amazon;
using System;
using System.Collections.Generic;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

namespace AmazonSESSample 
{
    class Program
    {

        static readonly string senderAddress = "*****";

        static readonly string receiverAddress = "******";


        static readonly string configSet = "ConfigSet";


        static readonly string subject = "Amazon SES test (AWS SDK for .NET)";


        static readonly string textBody = "Amazon SES Test (.NET)\r\n" 
                                        + "This email was sent through Amazon SES "
                                        + "using the AWS SDK for .NET.";


        static readonly string htmlBody = @"<html>
<head></head>
<body>
  <h1>Amazon SES Test (AWS SDK for .NET)</h1>
  <p>This email was sent with
    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
    <a href='https://aws.amazon.com/sdk-for-net/'>
      AWS SDK for .NET</a>.</p>
</body>
</html>";

        static void Main(string[] args)
        {

          using (
var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1)) 
            {
                var sendRequest = new SendEmailRequest
                {
                    Source = senderAddress,
                    Destination = new Destination
                    {
                        ToAddresses =
                        new List<string> { receiverAddress }
                    },
                    Message = new Message
                    {
                        Subject = new Content(subject),
                        Body = new Body
                        {
                            Html = new Content
                            {
                                Charset = "UTF-8",
                                Data = htmlBody
                            },
                            Text = new Content
                            {
                                Charset = "UTF-8",
                                Data = textBody
                            }
                        }
                    },

                    ConfigurationSetName = configSet
                };
                try
                {
                    Console.WriteLine("Sending email using Amazon SES...");
                    var response = client.SendEmailAsync(sendRequest);
                    Console.WriteLine("The email was sent successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The email was not sent.");
                    Console.WriteLine("Error message: " + ex.Message);

                }
            }

            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }
}
c# .net amazon-web-services amazon-ses
1个回答
3
投票

Bug

Main方法在发送电子邮件之前结束,因为我们不等待Task response完成。

SendEmailAsync返回Task,意思是这两行代码是相同的:

  1. var response = client.SendEmailAsync(sendRequest);
  2. Task response = client.SendEmailAsync(sendRequest);

Solutions

如果您安装了最新版本的Visual Studio并启用了C#7.1,则可以利用async Task Main并使用await关键字,该关键字将告诉代码在不同的线程上运行SendEmailAsyncMain将不会结束,直到SendEmail完成。

如果您使用的是较旧版本的Visual Studio,则可以添加.GetAwaiter().GetResult(),这也将确保MainSendEmail完成之前不会结束,但SendEmailAsync将锁定当前线程。

C#7.1及以上(首选)

static async Task Main(string[] args)
{

    using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1)) 
    {
        var sendRequest = new SendEmailRequest
        {
            Source = senderAddress,
            Destination = new Destination
            {
                ToAddresses =
                new List<string> { receiverAddress }
            },
            Message = new Message
            {
                Subject = new Content(subject),
                Body = new Body
                {
                    Html = new Content
                    {
                        Charset = "UTF-8",
                        Data = htmlBody
                    },
                    Text = new Content
                    {
                        Charset = "UTF-8",
                        Data = textBody
                    }
                }
            },

            ConfigurationSetName = configSet
        };
        try
        {
            Console.WriteLine("Sending email using Amazon SES...");
            var response = await client.SendEmailAsync(sendRequest);
            Console.WriteLine("The email was sent successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("The email was not sent.");
            Console.WriteLine("Error message: " + ex.Message);

        }
    }

    Console.Write("Press any key to continue...");
    Console.ReadKey();
}

C#7.0及以下

static void Main(string[] args)
{

    using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1)) 
    {
        var sendRequest = new SendEmailRequest
        {
            Source = senderAddress,
            Destination = new Destination
            {
                ToAddresses =
                new List<string> { receiverAddress }
            },
            Message = new Message
            {
                Subject = new Content(subject),
                Body = new Body
                {
                    Html = new Content
                    {
                        Charset = "UTF-8",
                        Data = htmlBody
                    },
                    Text = new Content
                    {
                        Charset = "UTF-8",
                        Data = textBody
                    }
                }
            },

            ConfigurationSetName = configSet
        };
        try
        {
            Console.WriteLine("Sending email using Amazon SES...");
            var response = client.SendEmailAsync(sendRequest).GetAwaiter().GetResult();
            Console.WriteLine("The email was sent successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("The email was not sent.");
            Console.WriteLine("Error message: " + ex.Message);

        }
    }

    Console.Write("Press any key to continue...");
    Console.ReadKey();
}
© www.soinside.com 2019 - 2024. All rights reserved.