上次更新时间:2021 年 2 月 18 日
我想要在 AWS Identity and Access Management (IAM) 中轮换我的 Amazon Simple Email Service (Amazon SES) 简单邮件传输协议 (SMTP) 凭证。我该如何创建与 Amazon SES 兼容的用户名和密码?
解决方法您在 IAM 控制台中为 SMTP 用户创建的访问密钥与 Amazon SES SMTP 接口不兼容。IAM 控制台中生成的密钥所采用的格式与 Amazon SES SMTP 服务器所需凭证的格式不同。
要为 Amazon SES SMTP 接口设置凭证,请执行以下任意一项操作:
创建新的 Amazon SES SMTP 凭证1.使用 Amazon SES 控制台创建新的 Amazon SES SMTP 凭证。
2.在取得新的凭证以后,您可以选择删除 IAM 中的现有 Amazon SES 凭证(如果您不再需要它们)。
将您的现有秘密访问密钥转换成 Amazon SES SMTP 格式注意:您必须通过以下步骤使用 Python 3 或更高版本。
1.更新现有的 IAM 用户策略,以便在最低程度上赋予 ses: SendRawEmail 权限。
2.复制 Python 代码,将秘密访问密钥转换成 Amazon SES SMTP 密码。
3.将 Python 代码粘贴到文本编辑器,然后将文件保存为 seskey.py。
4.要运行 Python 脚本,请使用以下命令:
对于 -secret,输入您的现有秘密访问密钥。然后输入一个空格以及您在使用 SMTP 密码时所在的 AWS 区域。
python3 seskey.py --secret YOURKEYrrpg/JHpyvtStUVcAV9177EAKKmDP37P us-east-1
5.该脚本会输出可与 Amazon SES 一起使用的新的秘密访问密钥:
#!/usr/bin/env python3
import hmac
import hashlib
import base64
import argparse
SMTP_REGIONS =
# These values are required to calculate the signature. Do not change them.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def calculate_key(secret_access_key, region):
if region not in SMTP_REGIONS:
raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")
signature = sign(("AWS4" + secret_access_key).encode('utf-8'), DATE)
signature = sign(signature, region)
signature = sign(signature, SERVICE)
signature = sign(signature, TERMINAL)
signature = sign(signature, MESSAGE)
signature_and_version = bytes() + signature
smtp_password = base64.b64encode(signature_and_version)
return smtp_password.decode('utf-8')
def main():
parser = argparse.ArgumentParser(
description='Convert a Secret Access Key for an IAM user to an SMTP password.')
parser.add_argument(
'secret', help='The Secret Access Key to convert.')
parser.add_argument(
'region',
help='The AWS Region where the SMTP password will be used.',
choices=SMTP_REGIONS)
args = parser.parse_args()
print(calculate_key(args.secret, args.region))
if __name__ == '__main__':
main()