python ------ 自动发送邮件

测试环境:

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( '发送失败')




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值