AWS SES不提供SMTP设置按钮。相反,您需要通过使用您的凭据与您的SMTP客户端进行身份验证来发送电子邮件。下面是使用Python的示例代码,它使用Pythons smtplib
模块和AWS SES的SMTP终结点来发送电子邮件。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
SENDER = 'sender@example.com'
RECIPIENT = 'recipient@example.com'
PASSWORD = 'your-smtp-password' # AWS SES SMTP credentials password
REGION = 'us-east-1'
# Create the message
msg = MIMEMultipart()
msg['Subject'] = 'Sample email with attachment'
msg['From'] = SENDER
msg['To'] = RECIPIENT
# Add body text to the email
body_text = 'Hello,\n\nPlease see attachment for more details.\n\nBest regards,'
body = MIMEText(body_text, 'plain')
msg.attach(body)
# Add attachment to the email
attachment_filename = 'attachment.txt'
with open(attachment_filename, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=attachment_filename)
msg.attach(attachment)
# Send email using AWS SES SMTP endpoint
with smtplib.SMTP(f'email-smtp.{REGION}.amazonaws.com', 587) as server:
server.starttls()
server.login(SENDER, PASSWORD)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
在上面的例子中,您需要:
-将SENDER
和RECIPIENT
变量替换为您的电子邮件地址
-将PASSWORD
变量替换为您的SMTP凭据密码
-如果您不在us-east-1
区域,请将REGION
变量替换为您的区域
-添加适当的主体文本和/或附件,以便满足您的要求
-使用`server.send