这个错误消息表示 AWS SES 客户端无法连接到代理 URL。以下是一个可能的解决方案,其中包含一个 Python 代码示例:
import boto3
from botocore.exceptions import NoCredentialsError, EndpointConnectionError
def send_email(subject, body, sender, recipient):
# 创建 AWS SES 客户端
ses_client = boto3.client('ses')
# 设置代理 URL
ses_client.meta.events._unique_id_handlers['request-created'].insert(0,
lambda request, **kwargs: request.register_hook('before-send',
lambda r, **kwargs: setattr(r.context, 'proxies', {'http': 'http://your_proxy_url', 'https': 'https://your_proxy_url'}))))
try:
# 发送邮件
response = ses_client.send_email(
Source=sender,
Destination={
'ToAddresses': [
recipient
]
},
Message={
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
)
print("Email sent successfully!")
except NoCredentialsError:
print("AWS credentials not found")
except EndpointConnectionError:
print("Unable to connect to proxy URL")
except Exception as e:
print("An error occurred:", str(e))
# 发送邮件示例
send_email("Hello", "This is a test email", "sender@example.com", "recipient@example.com")
在上面的代码中,我们使用 boto3
库创建了一个 AWS SES 客户端。然后,我们通过修改客户端对象的 meta
属性来设置代理 URL。注意替换 'http://your_proxy_url'
和 'https://your_proxy_url'
中的 your_proxy_url
为实际的代理 URL。
如果 AWS SES 客户端无法连接到代理 URL,将会抛出 EndpointConnectionError
异常。在代码示例中,我们使用 try-except
块来捕获并处理这个异常。