测试环境:
python3.4
发件服务器 网易yeah
收件服务器 移动139邮箱
注意:一定要在发件的邮箱里面设置POP3/SMTP/IMAP为开启状态,要不然会报错:
535 Error:authentication failed
通过本程序,可以自动给自己的手机邮箱发送邮件,在手机邮箱中把发件地址设置为白名单,就可以在手机收到短信通知
测试结果如图:
import smtplib
import email
from email.mime.text import MIMEText
#配置信息
send_mail_host = "smtp.yeah.net" # 发送的smtp
send_mail_user = "*******" #发件人邮箱的用户名
send_mail_user_name = "李啊涌" #发件人姓名
send_mail_pswd = "********" #发件邮箱设置的授权码
send_mail_postfix = "yeah.net" # 发邮件的域名
get_mail_user = "***********" #收件用户名
get_mail_postfix = "139.com" #收件人邮箱服务器
get_mail_host = "pop.139.com" #收件人pop3服务器
def semd_mail(sub, content):
send_mail_address = send_mail_user_name + "<" + send_mail_user + "@" + send_mail_postfix + ">"
msg = email.mime.text.MIMEText(content)
msg['Subject'] = sub
msg['From'] = send_mail_address
msg['to'] = to_adress = "139SMSserver<" + get_mail_user + "@" + get_mail_postfix + ">"
try:
stp = smtplib.SMTP()
stp.connect(send_mail_host)
stp.login(send_mail_user, send_mail_pswd)
stp.sendmail(send_mail_address, to_adress, msg.as_string())
stp.close()
return True
except Exception as e:
print( e)
return False
if __name__ == '__main__':
sub="测试" #主题
content="python如此强大!" #发件内容
if semd_mail(sub, content):
print ("发送成功")
else:
print( '发送失败')