要使用AWS SES发送邮件,您需要验证用于发送邮件的电子邮件地址。验证完成后,您可以从验证的电子邮件地址发送邮件。以下是一个使用AWS SDK for Python(Boto3)的示例代码,演示如何发送电子邮件。
import boto3
# 创建SES客户端
client = boto3.client('ses')
# 发送邮件
response = client.send_email(
Source='your_verified_email@example.com', # 验证的电子邮件地址
Destination={
'ToAddresses': ['recipient@example.com'], # 收件人电子邮件地址
},
Message={
'Subject': {
'Data': 'Hello from AWS SES', # 邮件主题
},
'Body': {
'Text': {
'Data': 'This is the body of the email' # 邮件正文
}
}
}
)
print("Email sent! Message ID:"),
print(response['MessageId'])
在上述代码中,您需要将your_verified_email@example.com
替换为您已经验证过的电子邮件地址,并将recipient@example.com
替换为收件人的电子邮件地址。
请注意,使用AWS SES发送邮件可能会产生费用。在使用AWS SES时,请查看AWS SES定价页面以了解相关费用信息。此外,确保已正确配置AWS凭证以便访问SES服务。