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

时间:2018-04-15 16:53:19

标签: c# .net amazon-web-services amazon-ses

我使用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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

错误

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

SendEmailAsync返回Task,这意味着这两行代码相同:

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

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

  3. 解决方案

    如果您安装了最新版本的Visual Studio并启用了C#7.1,则可以利用async Task Main并使用await关键字来指示代码运行{{1}在另一个帖子上SendEmailAsync赢了,直到Main完成。

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

    C#7.1及以上(首选)
    SendEmailAsync

    C#7.0及以下

    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();
    }