【问题标题】:How to add AWS SES (Simple Email Service) to .net 6 program.cs如何将 AWS SES(简单电子邮件服务)添加到 .net 6 program.cs
【发布时间】:2022-11-09 01:54:29
【问题描述】:

我需要使用 Amazon SES 在 .net 6 中发送电子邮件。

在 .net 5 中以及在您需要将其添加到 startup.cs 之前

    // Amazon SES
    services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
    services.AddAWSService<IAmazonSimpleEmailService>();
    services.AddSingleton<IEmailService, CSharpAwsSesServiceHelper.EmailService.AwsEmailService>();
    services.Configure<AwsEmailServiceOptions>(Configuration.GetSection(nameof(AwsEmailServiceOptions)))
        .AddSingleton(x => x.GetRequiredService<IOptions<AwsEmailServiceOptions>>().Value);

在 .net 6 中没有 Startup.cs,它已在 program.cs 中被替换和简化。

但是,在 program.cs 中默认没有配置对象,就像在 Startup.cs 中一样,这不起作用(错误'配置不包含 GetAWSOptions())

我搜索了一个示例项目或指导,例如 https://aws.amazon.com/blogs/developer/net-6-on-aws/

https://github.com/aws-samples/aws-net-guides/tree/master/RuntimeSupport/dotnet6

https://jasonwatmore.com/post/2020/11/28/net-core-c-aws-ses-send-email-via-smtp-with-aws-simple-email-service

但找不到此代码的 .net 6 版本。

有人知道这里有什么最佳实践/如何使这项工作有效吗?

谢谢。

更新

我已将所有 using 语句从 Startup.cs 复制到 Program.cs - 这不是问题。

然后,我复制了服务构建器(将服务更改为 builder.Services。)由于 program.cs 中缺少配置对象,我收到上面列出的错误。

【问题讨论】:

  • “这不起作用”什么是“这个”?最明显的解决方案是添加缺少的using 语句
  • 您必须在 Program.cs 中配置服务,对吧?它与 Startup 中的服务构建器相同。
  • 我已经更新了问题以澄清这些观点。

标签: c# amazon-ses .net-6.0


【解决方案1】:

我想通了 - 你只需要添加构建器。到配置的开始 - 即

// Amazon SES
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonSimpleEmailService>();
builder.Services.AddSingleton<IEmailService, CSharpAwsSesServiceHelper.EmailService.AwsEmailService>();
builder.Services.Configure<AwsEmailServiceOptions>(builder.Configuration.GetSection(nameof(AwsEmailServiceOptions)))
    .AddSingleton(x => x.GetRequiredService<IOptions<AwsEmailServiceOptions>>().Value);

【讨论】:

  • 1000 次观看,但有一个无法解释的反对票 - 对这个问题的任何赞成票都是很好的,谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 2022-11-22
  • 2018-09-18
  • 1970-01-01
  • 2014-10-30
相关资源
最近更新 更多