Python发电子邮件

有时候项目中会涉及发送电子邮件,因此,先记录一下

import smtplib
from email.mime.text import MIMEText
#邮件接收者邮箱
email='2201128470@qq.com'
import easygui
receiver=email
subject='这是邮件主题'
content='这是邮件内容'
#邮件代理服务器,我使用的是163邮箱,其他邮箱也可以
host='smtp.163.com'
#邮件发送者的登录账号
user_name='18258685895'
#这里填邮箱授权码,不是账号的密码,如何获得邮箱授权码,请自行百度解决
pwd='###'
#邮件发送者邮箱账号
sender='WXB_0101@163.com'

message=MIMEText(content,'plain','utf-8')
message['From']=sender
message['to']=receiver
message['Subject']=subject
def sendMail():
    try:
        smtp_obj=smtplib.SMTP(host,25)
        smtp_obj.login(user_name,pwd)
        smtp_obj.sendmail(sender,receiver,message.as_string())
        easygui.msgbox('邮件发送成功')
    except smtplib.SMTPException as error:
        easygui.msgbox('邮件发送失败')
def main():
    sendMail()
if __name__ == '__main__':
    main()

posted @ 2020-06-18 16:26  极限之旅  阅读(164)  评论(0编辑  收藏  举报