user_email = "asdf@email.com"
content = "text"
mail_host = "smtp.exmail.qq.com"
mail_user = "system@system.com"
mail_pass = "password"
sender = mail_user
receivers = [user_email]
message = MIMEText(content, 'html', 'utf-8')
message['From'] = Header("this is header", 'utf-8')
subject = 'this is title'
message['Subject'] = Header(subject, 'utf-8')
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
上面这个写法总是超时,肯定是防火墙的原因。简单的解决方法就是绕开25端口。
改写如下:
user_email = "asdf@email.com"
content = "text"
mail_host = "smtp.exmail.qq.com"
mail_user = "system@system.com"
mail_pass = "password"
sender = mail_user
receivers = [user_email]
message = MIMEText(content, 'html', 'utf-8')
message['From'] = Header("this is header", 'utf-8')
subject = 'this is title'
message['Subject'] = Header(subject, 'utf-8')
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())