微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使 AWS SMTP 电子邮件正常工作的问题

如何解决使 AWS SMTP 电子邮件正常工作的问题

我正在尝试让下面的代码与 AWS SES 一起使用(我用我的凭证替换了诸如“aaa”/“bbb”等的占位符):

import smtplib  
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

SENDER = 'aaa@bbb.com'  
SENDERNAME = 'aaa'

RECIPIENT  = 'ccc@ddd.com'
USERNAME_SMTP = 'xxxxx'
PASSWORD_SMTP = 'yyyyy'
HOST = 'email-smtp.us-east-1.amazonaws.com'
PORT = 587
SUBJECT = 'Amazon SES Test (Python smtplib)'
BODY_TEXT = ("Amazon SES Test\r\n"
             "This email was sent through the Amazon SES SMTP "
             "Interface using the Python smtplib package."
            )
BODY_HTML = """<html>
<head></head>
<body>
  <h1>Amazon SES SMTP Email Test</h1>
  <p>This email was sent with Amazon SES using the
    <a href='https://www.python.org/'>Python</a>
    <a href='https://docs.python.org/3/library/smtplib.html'>
    smtplib</a> library.</p>
</body>
</html>
            """
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = email.utils.formataddr((SENDERNAME,SENDER))
msg['To'] = RECIPIENT

part1 = MIMEText(BODY_TEXT,'plain')
part2 = MIMEText(BODY_HTML,'html')

msg.attach(part1)
msg.attach(part2)

try:  
    server = smtplib.SMTP(HOST,PORT)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(USERNAME_SMTP,PASSWORD_SMTP)
    server.sendmail(SENDER,RECIPIENT,msg.as_string())
    server.close()

except Exception as e:
    print ("Error: ",e)
else:
    print ("Email sent!")

当我在 Python 控制台上运行代码时,代码在“登录”语句中退出(脚本本身没有给出任何错误代码登录时才退出)。我已验证我的 UN/PW 是正确的,发送电子邮件已验证等,并且我的帐户已获准发送电子邮件。如果我使用下面的 PowerShell 实用程序,我也可以使用 AWS SES 发送 SMTP 电子邮件(因此 AWS 端已正确设置/启用):

$AWS_ACCESS_KEY = "aaa"
$AWS_SECRET_KEY = "bbb"

$SECURE_KEY = $(ConvertTo-securestring -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY,$SECURE_KEY))

$from = "me@me.com"
$to = "to@to.com"
$subject = "test" 
$body = "This is the body"

Write-Host "Sending Email via AmazonSES"
Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer email-smtp.us-east-1.amazonaws.com -Credential $creds -UseSsl -Port 587
Write-Host "Sent"

关于为什么我的 Python 不起作用的任何想法?代码本身来自 AWS。不确定是否需要添加任何内容。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。