smtp+ mail写邮件(爬虫准备)
# coding:utf-8
import smtplib
from email.mime.text import MIMEText as mtext
from email.header import Header
mail_host = "smtp.sohu.com"
host_user = "[email protected]"
host_password = "************"
form_address = host_user#( 发件人地址)
email_subject = "test"#(主题,相当于title)
address = "[email protected]" #(收件人地址)
massage = "新年好" #(内容,支持富文本)
def sendTo(address, message):
msg = mtext(message, _subtype='plain')
msg['Subject'] = email_subject
msg['Form'] = Header(host_user)
msg['To'] = address
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(host_user, host_password)
server.sendmail(host_user, address, msg.as_string())
server.close()
return True
except Exception as e:
print(str(e))
return False
if __name__ == '__main__':
sendTo(address, massage)
点击运行