搭ListIdentities配 AWS 開發套件或 CLI 使用 - Amazon Simple Email Service

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

ListIdentities配 AWS 開發套件或 CLI 使用

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

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
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中的。

C++
適用於 C++ 的 SDK
注意

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

//! List the identities associated with this account. /*! \param identityType: The identity type enum. "NOT_SET" is a valid option. \param identities; A vector to receive the retrieved identities. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::listIdentities(Aws::SES::Model::IdentityType identityType, Aws::Vector<Aws::String> &identities, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::ListIdentitiesRequest listIdentitiesRequest; if (identityType != Aws::SES::Model::IdentityType::NOT_SET) { listIdentitiesRequest.SetIdentityType(identityType); } Aws::String nextToken; // Used for paginated results. do { if (!nextToken.empty()) { listIdentitiesRequest.SetNextToken(nextToken); } Aws::SES::Model::ListIdentitiesOutcome outcome = sesClient.ListIdentities( listIdentitiesRequest); if (outcome.IsSuccess()) { const auto &retrievedIdentities = outcome.GetResult().GetIdentities(); if (!retrievedIdentities.empty()) { identities.insert(identities.cend(), retrievedIdentities.cbegin(), retrievedIdentities.cend()); } nextToken = outcome.GetResult().GetNextToken(); } else { std::cout << "Error listing identities. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); return true; }
  • 如需 API 詳細資訊,請參閱 AWS SDK for C++ API 參考ListIdentities中的。

CLI
AWS CLI

列出特定 AWS 帳戶的所有身分 (電子郵件地址和網域)

下列範例使用 list-identities 命令來列出已提交給 Amazon SES 驗證的所有身分:

aws ses list-identities

輸出:

{ "Identities": [ "user@example.com", "example.com" ] }

傳回的清單包含所有身分,無論驗證狀態為何 (已驗證、等待驗證、失敗等)。

在此範例中,因為我們未指定 identity-type 參數,所以會傳回電子郵件地址 網域。

如需驗證詳細資訊,請參閱《Amazon Simple Email Service 開發人員指南》中的「在 Amazon SES 中驗證電子郵件地址和網域」。

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考ListIdentities中的。

Java
適用於 Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ses.SesClient; import software.amazon.awssdk.services.ses.model.ListIdentitiesResponse; import software.amazon.awssdk.services.ses.model.SesException; import java.io.IOException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListIdentities { public static void main(String[] args) throws IOException { Region region = Region.US_WEST_2; SesClient client = SesClient.builder() .region(region) .build(); listSESIdentities(client); } public static void listSESIdentities(SesClient client) { try { ListIdentitiesResponse identitiesResponse = client.listIdentities(); List<String> identities = identitiesResponse.identities(); for (String identity : identities) { System.out.println("The identity is " + identity); } } catch (SesException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考ListIdentities中的。

JavaScript
適用於 JavaScript (v3) 的開發套件
注意

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

import { ListIdentitiesCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createListIdentitiesCommand = () => new ListIdentitiesCommand({ IdentityType: "EmailAddress", MaxItems: 10 }); const run = async () => { const listIdentitiesCommand = createListIdentitiesCommand(); try { return await sesClient.send(listIdentitiesCommand); } catch (err) { console.log("Failed to list identities.", err); return err; } };
  • 如需 API 詳細資訊,請參閱 AWS SDK for JavaScript API 參考ListIdentities中的。

PowerShell
適用的工具 PowerShell

範例 1:無論驗證狀態為何,此命令都會傳回包含特定 AWS 帳戶的所有身分 (電子郵件地址和網域) 的清單。

Get-SESIdentity
  • 如需 API 詳細資訊,請參閱AWS Tools for PowerShell 指令程ListIdentities式參考中的。

Python
適用於 Python (Boto3) 的 SDK
注意

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

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def list_identities(self, identity_type, max_items): """ Gets the identities of the specified type for the current account. :param identity_type: The type of identity to retrieve, such as EmailAddress. :param max_items: The maximum number of identities to retrieve. :return: The list of retrieved identities. """ try: response = self.ses_client.list_identities( IdentityType=identity_type, MaxItems=max_items ) identities = response["Identities"] logger.info("Got %s identities for the current account.", len(identities)) except ClientError: logger.exception("Couldn't list identities for the current account.") raise else: return identities
  • 如需 API 的詳細資訊,請參閱AWS 開發套件ListIdentities中的 Python (博托 3) API 參考。

Ruby
適用於 Ruby 的開發套件
注意

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

require "aws-sdk-ses" # v2: require 'aws-sdk' # Create client in us-west-2 region # Replace us-west-2 with the AWS Region you're using for Amazon SES. client = Aws::SES::Client.new(region: "us-west-2") # Get up to 1000 identities ids = client.list_identities({ identity_type: "EmailAddress" }) ids.identities.each do |email| attrs = client.get_identity_verification_attributes({ identities: [email] }) status = attrs.verification_attributes[email].verification_status # Display email addresses that have been verified if status == "Success" puts email end end
  • 如需 API 詳細資訊,請參閱 AWS SDK for Ruby API 參考ListIdentities中的。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS 開發套件使用 Amazon SES。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。