python脚本发送SMTP邮件例程
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender="XXXXXXXXXX@sina.cn"
receiver="XXXXXXXXXX@qq.com"
severname="smtp.sina.cn"
mailpass="XXXXXXXXXX"
message=MIMEText('python 邮件发送测试','plain','utf-8')
message['From']="{}".format(sender)
message['To']=",".join(receiver)
message['Subject']='python test'
try:
sm=smtplib.SMTP()
sm.connect(severname,25)
sm.login(sender,mailpass)
sm.sendmail(sender,receiver,message.as_string())
print("you have sended this email sucessfully")
except smtplib.SMTPException:
print("error,无法发送此邮件")
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender="XXXXXXXXXX@sina.cn"
receiver="XXXXXXXXXX@qq.com"
severname="smtp.sina.cn"
mailpass="XXXXXXXXXX"
mailmessage="""
<p>python test</p>
<p><a href="http://www.runoob.com">这是一个链接</a>
"""
message=MIMEText(mailmessage,'html','utf-8')
message['From']=Header(sender)
message['To']=Header(receiver)
message['Subject']='python学习'
try:
st=smtplib.SMTP()
st.connect(severname,25)
st.login(sender,mailpass)
st.sendmail(sender,receiver,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("邮件发送失败")
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender="XXXXXXXXXX@sina.cn"
receiver="XXXXXXXXXX@qq.com"
severname="smtp.sina.cn"
mailpass="XXXXXXXXXX"
message=MIMEMultipart()
message['From']=Header(sender)
message['To']=Header(receiver)
message['Subject']='python学习'
message.attach(MIMEText('这个是测试','plain','utf-8'))
att1=MIMEText(open('test.txt','rb').read(),'base64','utf-8')
att1["Content-Type"]='application/octet-stream'
att1["Content-Disposition"]='attachment;filename="test.txt"'
message.attach(att1)
try:
sm=smtplib.SMTP()
sm.connect(severname,25)
sm.login(sender,mailpass)
sm.sendmail(sender,receiver,message.as_string())
print("you have sended this email sucessfully")
except smtplib.SMTPException:
print("error,无法发送此邮件")