用SDK于 Rust SES API 的 Amazon v2 示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

用SDK于 Rust SES API 的 Amazon v2 示例

以下代码示例向您展示了如何在 Amazon SES API v2 中使用 for Rust 来执行操作和实现常见场景。 AWS SDK

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

场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。

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

操作

以下代码示例演示如何使用 CreateContact

SDK对于 Rust
注意

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

async fn add_contact(client: &Client, list: &str, email: &str) -> Result<(), Error> { client .create_contact() .contact_list_name(list) .email_address(email) .send() .await?; println!("Created contact"); Ok(()) }
  • 有关API详细信息,请参见CreateContact中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 CreateContactList

SDK对于 Rust
注意

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

async fn make_list(client: &Client, contact_list: &str) -> Result<(), Error> { client .create_contact_list() .contact_list_name(contact_list) .send() .await?; println!("Created contact list."); Ok(()) }
  • 有关API详细信息,请参见CreateContactList中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 CreateEmailIdentity

SDK对于 Rust
注意

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

match self .client .create_email_identity() .email_identity(self.verified_email.clone()) .send() .await { Ok(_) => writeln!(self.stdout, "Email identity created successfully.")?, Err(e) => match e.into_service_error() { CreateEmailIdentityError::AlreadyExistsException(_) => { writeln!( self.stdout, "Email identity already exists, skipping creation." )?; } e => return Err(anyhow!("Error creating email identity: {}", e)), }, }

以下代码示例演示如何使用 CreateEmailTemplate

SDK对于 Rust
注意

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

let template_html = std::fs::read_to_string("../resources/newsletter/coupon-newsletter.html") .unwrap_or_else(|_| "Missing coupon-newsletter.html".to_string()); let template_text = std::fs::read_to_string("../resources/newsletter/coupon-newsletter.txt") .unwrap_or_else(|_| "Missing coupon-newsletter.txt".to_string()); // Create the email template let template_content = EmailTemplateContent::builder() .subject("Weekly Coupons Newsletter") .html(template_html) .text(template_text) .build(); match self .client .create_email_template() .template_name(TEMPLATE_NAME) .template_content(template_content) .send() .await { Ok(_) => writeln!(self.stdout, "Email template created successfully.")?, Err(e) => match e.into_service_error() { CreateEmailTemplateError::AlreadyExistsException(_) => { writeln!( self.stdout, "Email template already exists, skipping creation." )?; } e => return Err(anyhow!("Error creating email template: {}", e)), }, }

以下代码示例演示如何使用 DeleteContactList

SDK对于 Rust
注意

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

match self .client .delete_contact_list() .contact_list_name(CONTACT_LIST_NAME) .send() .await { Ok(_) => writeln!(self.stdout, "Contact list deleted successfully.")?, Err(e) => return Err(anyhow!("Error deleting contact list: {e}")), }
  • 有关API详细信息,请参见DeleteContactList中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 DeleteEmailIdentity

SDK对于 Rust
注意

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

match self .client .delete_email_identity() .email_identity(self.verified_email.clone()) .send() .await { Ok(_) => writeln!(self.stdout, "Email identity deleted successfully.")?, Err(e) => { return Err(anyhow!("Error deleting email identity: {}", e)); } }

以下代码示例演示如何使用 DeleteEmailTemplate

SDK对于 Rust
注意

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

match self .client .delete_email_template() .template_name(TEMPLATE_NAME) .send() .await { Ok(_) => writeln!(self.stdout, "Email template deleted successfully.")?, Err(e) => { return Err(anyhow!("Error deleting email template: {e}")); } }

以下代码示例演示如何使用 GetEmailIdentity

SDK对于 Rust
注意

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

确定电子邮件地址是否已验证。

async fn is_verified(client: &Client, email: &str) -> Result<(), Error> { let resp = client .get_email_identity() .email_identity(email) .send() .await?; if resp.verified_for_sending_status() { println!("The address is verified"); } else { println!("The address is not verified"); } Ok(()) }
  • 有关API详细信息,请参见GetEmailIdentity中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 ListContactLists

SDK对于 Rust
注意

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

async fn show_lists(client: &Client) -> Result<(), Error> { let resp = client.list_contact_lists().send().await?; println!("Contact lists:"); for list in resp.contact_lists() { println!(" {}", list.contact_list_name().unwrap_or_default()); } Ok(()) }
  • 有关API详细信息,请参见ListContactLists中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 ListContacts

SDK对于 Rust
注意

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

async fn show_contacts(client: &Client, list: &str) -> Result<(), Error> { let resp = client .list_contacts() .contact_list_name(list) .send() .await?; println!("Contacts:"); for contact in resp.contacts() { println!(" {}", contact.email_address().unwrap_or_default()); } Ok(()) }
  • 有关API详细信息,请参见ListContacts中的 Rust AWS SDK API 参考

以下代码示例演示如何使用 SendEmail

SDK对于 Rust
注意

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

向联系人列表中的所有成员发送消息。

async fn send_message( client: &Client, list: &str, from: &str, subject: &str, message: &str, ) -> Result<(), Error> { // Get list of email addresses from contact list. let resp = client .list_contacts() .contact_list_name(list) .send() .await?; let contacts = resp.contacts(); let cs: Vec<String> = contacts .iter() .map(|i| i.email_address().unwrap_or_default().to_string()) .collect(); let mut dest: Destination = Destination::builder().build(); dest.to_addresses = Some(cs); let subject_content = Content::builder() .data(subject) .charset("UTF-8") .build() .expect("building Content"); let body_content = Content::builder() .data(message) .charset("UTF-8") .build() .expect("building Content"); let body = Body::builder().text(body_content).build(); let msg = Message::builder() .subject(subject_content) .body(body) .build(); let email_content = EmailContent::builder().simple(msg).build(); client .send_email() .from_email_address(from) .destination(dest) .content(email_content) .send() .await?; println!("Email sent to list"); Ok(()) }

使用模板向联系人列表中的所有成员发送消息。

let coupons = std::fs::read_to_string("../resources/newsletter/sample_coupons.json") .unwrap_or_else(|_| r#"{"coupons":[]}"#.to_string()); let email_content = EmailContent::builder() .template( Template::builder() .template_name(TEMPLATE_NAME) .template_data(coupons) .build(), ) .build(); match self .client .send_email() .from_email_address(self.verified_email.clone()) .destination(Destination::builder().to_addresses(email.clone()).build()) .content(email_content) .list_management_options( ListManagementOptions::builder() .contact_list_name(CONTACT_LIST_NAME) .build()?, ) .send() .await { Ok(output) => { if let Some(message_id) = output.message_id { writeln!( self.stdout, "Newsletter sent to {} with message ID {}", email, message_id )?; } else { writeln!(self.stdout, "Newsletter sent to {}", email)?; } } Err(e) => return Err(anyhow!("Error sending newsletter to {}: {}", email, e)), }
  • 有关API详细信息,请参见SendEmail中的 Rust AWS SDK API 参考

场景

以下代码示例显示了如何运行 Amazon SES API v2 新闻简报工作流程。

SDK对于 Rust
注意

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

match self .client .create_contact_list() .contact_list_name(CONTACT_LIST_NAME) .send() .await { Ok(_) => writeln!(self.stdout, "Contact list created successfully.")?, Err(e) => match e.into_service_error() { CreateContactListError::AlreadyExistsException(_) => { writeln!( self.stdout, "Contact list already exists, skipping creation." )?; } e => return Err(anyhow!("Error creating contact list: {}", e)), }, } match self .client .create_contact() .contact_list_name(CONTACT_LIST_NAME) .email_address(email.clone()) .send() .await { Ok(_) => writeln!(self.stdout, "Contact created for {}", email)?, Err(e) => match e.into_service_error() { CreateContactError::AlreadyExistsException(_) => writeln!( self.stdout, "Contact already exists for {}, skipping creation.", email )?, e => return Err(anyhow!("Error creating contact for {}: {}", email, e)), }, } let contacts: Vec<Contact> = match self .client .list_contacts() .contact_list_name(CONTACT_LIST_NAME) .send() .await { Ok(list_contacts_output) => { list_contacts_output.contacts.unwrap().into_iter().collect() } Err(e) => { return Err(anyhow!( "Error retrieving contact list {}: {}", CONTACT_LIST_NAME, e )) } }; let coupons = std::fs::read_to_string("../resources/newsletter/sample_coupons.json") .unwrap_or_else(|_| r#"{"coupons":[]}"#.to_string()); let email_content = EmailContent::builder() .template( Template::builder() .template_name(TEMPLATE_NAME) .template_data(coupons) .build(), ) .build(); match self .client .send_email() .from_email_address(self.verified_email.clone()) .destination(Destination::builder().to_addresses(email.clone()).build()) .content(email_content) .list_management_options( ListManagementOptions::builder() .contact_list_name(CONTACT_LIST_NAME) .build()?, ) .send() .await { Ok(output) => { if let Some(message_id) = output.message_id { writeln!( self.stdout, "Newsletter sent to {} with message ID {}", email, message_id )?; } else { writeln!(self.stdout, "Newsletter sent to {}", email)?; } } Err(e) => return Err(anyhow!("Error sending newsletter to {}: {}", email, e)), } match self .client .create_email_identity() .email_identity(self.verified_email.clone()) .send() .await { Ok(_) => writeln!(self.stdout, "Email identity created successfully.")?, Err(e) => match e.into_service_error() { CreateEmailIdentityError::AlreadyExistsException(_) => { writeln!( self.stdout, "Email identity already exists, skipping creation." )?; } e => return Err(anyhow!("Error creating email identity: {}", e)), }, } let template_html = std::fs::read_to_string("../resources/newsletter/coupon-newsletter.html") .unwrap_or_else(|_| "Missing coupon-newsletter.html".to_string()); let template_text = std::fs::read_to_string("../resources/newsletter/coupon-newsletter.txt") .unwrap_or_else(|_| "Missing coupon-newsletter.txt".to_string()); // Create the email template let template_content = EmailTemplateContent::builder() .subject("Weekly Coupons Newsletter") .html(template_html) .text(template_text) .build(); match self .client .create_email_template() .template_name(TEMPLATE_NAME) .template_content(template_content) .send() .await { Ok(_) => writeln!(self.stdout, "Email template created successfully.")?, Err(e) => match e.into_service_error() { CreateEmailTemplateError::AlreadyExistsException(_) => { writeln!( self.stdout, "Email template already exists, skipping creation." )?; } e => return Err(anyhow!("Error creating email template: {}", e)), }, } match self .client .delete_contact_list() .contact_list_name(CONTACT_LIST_NAME) .send() .await { Ok(_) => writeln!(self.stdout, "Contact list deleted successfully.")?, Err(e) => return Err(anyhow!("Error deleting contact list: {e}")), } match self .client .delete_email_identity() .email_identity(self.verified_email.clone()) .send() .await { Ok(_) => writeln!(self.stdout, "Email identity deleted successfully.")?, Err(e) => { return Err(anyhow!("Error deleting email identity: {}", e)); } } match self .client .delete_email_template() .template_name(TEMPLATE_NAME) .send() .await { Ok(_) => writeln!(self.stdout, "Email template deleted successfully.")?, Err(e) => { return Err(anyhow!("Error deleting email template: {e}")); } }