如果Amazon SES在发送电子邮件时返回SMTP代码250错误,则可能是由于代码错误或Amazon SES配置问题导致的。您可以使用下面的代码示例解决此问题:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
msg = MIMEMultipart()
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Test Email'
text = MIMEText('This is a test email')
msg.attach(text)
pdf_file = 'example.pdf'
pdf_attachment = MIMEApplication(open(pdf_file, 'rb').read(), _subtype = 'pdf')
pdf_attachment.add_header('Content-Disposition', 'attachment', filename = pdf_file)
msg.attach(pdf_attachment)
try:
ses = smtplib.SMTP('email-smtp.us-west-2.amazonaws.com', 587)
ses.starttls()
ses.login('username', 'password')
ses.sendmail('sender@example.com', 'recipient@example.com', msg.as_string())
ses.quit()
print('Email sent!')
except Exception as e:
print('Error: ', e)
请注意,您需要将代码中的“username”和“password”替换为您AWS账户中IAM用户的凭据。此外,确保为空间的端点与代码中使用的端点匹配。最后,确认AWS账户可以发送邮件。