使用 Amazon SDK for .NET 的 Amazon SES 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 的 Amazon SES 示例

以下代码示例演示如何将 Amazon SDK for .NET 与 Amazon SES 结合使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何创建 Amazon SES 电子邮件模板。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Create an email template. /// </summary> /// <param name="name">Name of the template.</param> /// <param name="subject">Email subject.</param> /// <param name="text">Email body text.</param> /// <param name="html">Email HTML body text.</param> /// <returns>True if successful.</returns> public async Task<bool> CreateEmailTemplateAsync(string name, string subject, string text, string html) { var success = false; try { var response = await _amazonSimpleEmailService.CreateTemplateAsync( new CreateTemplateRequest { Template = new Template { TemplateName = name, SubjectPart = subject, TextPart = text, HtmlPart = html } }); success = response.HttpStatusCode == HttpStatusCode.OK; } catch (Exception ex) { Console.WriteLine("CreateEmailTemplateAsync failed with exception: " + ex.Message); } return success; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CreateTemplate中的。

以下代码示例演示了如何删除 Amazon SES 电子邮件模板。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Delete an email template. /// </summary> /// <param name="templateName">Name of the template.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteEmailTemplateAsync(string templateName) { var success = false; try { var response = await _amazonSimpleEmailService.DeleteTemplateAsync( new DeleteTemplateRequest { TemplateName = templateName }); success = response.HttpStatusCode == HttpStatusCode.OK; } catch (Exception ex) { Console.WriteLine("DeleteEmailTemplateAsync failed with exception: " + ex.Message); } return success; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteTemplate中的。

以下代码示例演示了如何删除 Amazon SES 身份。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Delete an email identity. /// </summary> /// <param name="identityEmail">The identity email to delete.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteIdentityAsync(string identityEmail) { var success = false; try { var response = await _amazonSimpleEmailService.DeleteIdentityAsync( new DeleteIdentityRequest { Identity = identityEmail }); success = response.HttpStatusCode == HttpStatusCode.OK; } catch (Exception ex) { Console.WriteLine("DeleteIdentityAsync failed with exception: " + ex.Message); } return success; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteIdentity中的。

以下代码示例显示如何获取 Amazon SES 发送限制。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Get information on the current account's send quota. /// </summary> /// <returns>The send quota response data.</returns> public async Task<GetSendQuotaResponse> GetSendQuotaAsync() { var result = new GetSendQuotaResponse(); try { var response = await _amazonSimpleEmailService.GetSendQuotaAsync( new GetSendQuotaRequest()); result = response; } catch (Exception ex) { Console.WriteLine("GetSendQuotaAsync failed with exception: " + ex.Message); } return result; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetSendQuota中的。

以下代码示例显示如何获取 Amazon SES 身份的状态。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Get identity verification status for an email. /// </summary> /// <returns>The verification status of the email.</returns> public async Task<VerificationStatus> GetIdentityStatusAsync(string email) { var result = VerificationStatus.TemporaryFailure; try { var response = await _amazonSimpleEmailService.GetIdentityVerificationAttributesAsync( new GetIdentityVerificationAttributesRequest { Identities = new List<string> { email } }); if (response.VerificationAttributes.ContainsKey(email)) result = response.VerificationAttributes[email].VerificationStatus; } catch (Exception ex) { Console.WriteLine("GetIdentityStatusAsync failed with exception: " + ex.Message); } return result; }

以下代码示例演示了如何列出 Amazon SES 电子邮件模板。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// List email templates for the current account. /// </summary> /// <returns>A list of template metadata.</returns> public async Task<List<TemplateMetadata>> ListEmailTemplatesAsync() { var result = new List<TemplateMetadata>(); try { var response = await _amazonSimpleEmailService.ListTemplatesAsync( new ListTemplatesRequest()); result = response.TemplatesMetadata; } catch (Exception ex) { Console.WriteLine("ListEmailTemplatesAsync failed with exception: " + ex.Message); } return result; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListTemplates中的。

以下代码示例演示了如何列出 Amazon SES 身份。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Get the identities of a specified type for the current account. /// </summary> /// <param name="identityType">IdentityType to list.</param> /// <returns>The list of identities.</returns> public async Task<List<string>> ListIdentitiesAsync(IdentityType identityType) { var result = new List<string>(); try { var response = await _amazonSimpleEmailService.ListIdentitiesAsync( new ListIdentitiesRequest { IdentityType = identityType }); result = response.Identities; } catch (Exception ex) { Console.WriteLine("ListIdentitiesAsync failed with exception: " + ex.Message); } return result; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListIdentities中的。

以下代码示例演示了如何通过 Amazon SES 发送电子邮件。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Send an email by using Amazon SES. /// </summary> /// <param name="toAddresses">List of recipients.</param> /// <param name="ccAddresses">List of cc recipients.</param> /// <param name="bccAddresses">List of bcc recipients.</param> /// <param name="bodyHtml">Body of the email in HTML.</param> /// <param name="bodyText">Body of the email in plain text.</param> /// <param name="subject">Subject line of the email.</param> /// <param name="senderAddress">From address.</param> /// <returns>The messageId of the email.</returns> public async Task<string> SendEmailAsync(List<string> toAddresses, List<string> ccAddresses, List<string> bccAddresses, string bodyHtml, string bodyText, string subject, string senderAddress) { var messageId = ""; try { var response = await _amazonSimpleEmailService.SendEmailAsync( new SendEmailRequest { Destination = new Destination { BccAddresses = bccAddresses, CcAddresses = ccAddresses, ToAddresses = toAddresses }, Message = new Message { Body = new Body { Html = new Content { Charset = "UTF-8", Data = bodyHtml }, Text = new Content { Charset = "UTF-8", Data = bodyText } }, Subject = new Content { Charset = "UTF-8", Data = subject } }, Source = senderAddress }); messageId = response.MessageId; } catch (Exception ex) { Console.WriteLine("SendEmailAsync failed with exception: " + ex.Message); } return messageId; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考SendEmail中的。

以下代码示例演示了如何通过 Amazon SES 发送模板化电子邮件。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Send an email using a template. /// </summary> /// <param name="sender">Address of the sender.</param> /// <param name="recipients">Addresses of the recipients.</param> /// <param name="templateName">Name of the email template.</param> /// <param name="templateDataObject">Data for the email template.</param> /// <returns>The messageId of the email.</returns> public async Task<string> SendTemplateEmailAsync(string sender, List<string> recipients, string templateName, object templateDataObject) { var messageId = ""; try { // Template data should be serialized JSON from either a class or a dynamic object. var templateData = JsonSerializer.Serialize(templateDataObject); var response = await _amazonSimpleEmailService.SendTemplatedEmailAsync( new SendTemplatedEmailRequest { Source = sender, Destination = new Destination { ToAddresses = recipients }, Template = templateName, TemplateData = templateData }); messageId = response.MessageId; } catch (Exception ex) { Console.WriteLine("SendTemplateEmailAsync failed with exception: " + ex.Message); } return messageId; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考SendTemplatedEmail中的。

以下代码示例演示了如何通过 Amazon SES 验证电子邮件身份。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Starts verification of an email identity. This request sends an email /// from Amazon SES to the specified email address. To complete /// verification, follow the instructions in the email. /// </summary> /// <param name="recipientEmailAddress">Email address to verify.</param> /// <returns>True if successful.</returns> public async Task<bool> VerifyEmailIdentityAsync(string recipientEmailAddress) { var success = false; try { var response = await _amazonSimpleEmailService.VerifyEmailIdentityAsync( new VerifyEmailIdentityRequest { EmailAddress = recipientEmailAddress }); success = response.HttpStatusCode == HttpStatusCode.OK; } catch (Exception ex) { Console.WriteLine("VerifyEmailIdentityAsync failed with exception: " + ex.Message); } return success; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考VerifyEmailIdentity中的。