我正在尝试通过office365服务器发送电子邮件。电子邮件已正确发送,但邮件未附加
非常感谢协助
import smtplib
to = "me@gmail.com"
office365_user = 'announcement@somewhere.com'
office365_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.office365.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(office365_user,office365_pwd)
msg = "This is a test email \n"
smtpserver.sendmail(office365_user, to, msg)
smtpserver.close()
答案 0 :(得分:4)
您的邮件不是有效的邮件消息,其中包含标题和正文。尝试这样的事情:
msg = """From: <me@example.com>
To: <you@example.com>
Subject: foo
This is a test email
"""
答案 1 :(得分:1)
考虑以与Python documentation相同的方式构建消息。
from email.mime.text import MIMEText
msg = MIMEText("This is a test email")
msg['Subject'] = 'Email Subject'
msg['From'] = 'announcement@somewhere.com'
msg['To'] = 'me@gmail.com'
另外,我不确定使用smtpserver.close()
。似乎正确的方法是smtpserver.quit()
。