此错误通常表示您的邮件主题、正文或收件人地址中使用了不允许的字符。要解决此错误,请确保您的邮件主题、正文和收件人地址仅包含允许的字符,如字母、数字、句点、下划线和短划线。
以下是使用 Python 发送电子邮件时的示例代码,演示如何避免此错误:
import boto3
from botocore.exceptions import ClientError
SENDER = "sender@example.com"
RECIPIENT = "recipient@example.com"
AWS_REGION = "us-west-2"
SUBJECT = "Test email"
BODY_TEXT = "This is the message body in text format."
CHARSET = "UTF-8"
client = boto3.client('ses',region_name=AWS_REGION)
try:
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
请注意,这里的主题和正文变量使用了允许的字符,并使用了指定的字符集。