Amazon SES 示例使用 AWS SDK for .NET - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon SES 示例使用 AWS SDK for .NET

下列程式碼範例說明如何透過 AWS SDK for .NET 與 Amazon SES 搭配使用來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用CreateTemplate

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考CreateTemplate中的。

下列程式碼範例會示範如何使用DeleteIdentity

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考DeleteIdentity中的。

下列程式碼範例會示範如何使用DeleteTemplate

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考DeleteTemplate中的。

下列程式碼範例會示範如何使用GetIdentityVerificationAttributes

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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; }

下列程式碼範例會示範如何使用GetSendQuota

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考GetSendQuota中的。

下列程式碼範例會示範如何使用ListIdentities

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考ListIdentities中的。

下列程式碼範例會示範如何使用ListTemplates

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考ListTemplates中的。

下列程式碼範例會示範如何使用SendEmail

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考SendEmail中的。

下列程式碼範例會示範如何使用SendTemplatedEmail

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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 詳細資訊,請參閱 AWS SDK for .NET API 參考SendTemplatedEmail中的。

下列程式碼範例會示範如何使用VerifyEmailIdentity

AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

/// <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; }