python:smtp和TLS不传递消息
我正试图通过office365服务器发送电子邮件。电子邮件已正确发送,但未附加邮件 非常感谢您的帮助python:smtp和TLS不传递消息,python,ssl,smtplib,Python,Ssl,Smtplib,我正试图通过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
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()
您的邮件不是有效的邮件,它由标题和正文组成。试着这样做:
msg = """From: <me@example.com>
To: <you@example.com>
Subject: foo
This is a test email
"""
msg=”“”来自:
致:
主题:富
这是一封测试邮件
"""
考虑以与消息相同的方式构造消息
另外,我不确定是否要使用smtpserver.close()
。似乎正确的方法是smtpserver.quit()
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'