使用python的smtp模块发送邮件

来源:互联网 发布:社工库数据下载 编辑:程序博客网 时间:2024/05/16 01:26

使用Python的smtp模块,可以十分方便的编写自己的smtp客户端,来发送邮件。现在发现,不能使用腾讯的smtp服务器去法送,但是可以使用163的smtp服务器去发送邮件。

直接上代码吧

#!/usr/bin/env python'''    a simple smtp client'''import smtplibfrom email.mime.text import MIMETextdef sendMail(user,pwd,to,subject,text):    msg=MIMEText(text)    msg['From']=user    msg['To']=to    msg['Subject']=subject    try:        #smtpServer=smtplib.SMTP('smtp.qq.com',587)#Configure 1        smtpServer=smtplib.SMTP('smtp.163.com',25)#Configure 2        print "[+] Connecting To Mail Server"        smtpServer.ehlo()        print "[+] Starting Encrypted Session"        smtpServer.starttls()        smtpServer.ehlo()        print "[+] Logging Into Mail Server"        smtpServer.login(user,pwd)        print "[+] Logging successfully"        print "[+] Sendding Mail"        smtpServer.sendmail(user,to,msg.as_string())        smtpServer.close()        print "[+] Mail send Successfully"    except:        print "[+] Mail send failed"def main():    'Configure 1: from qq mail to 163 mail'    #user=""#type in your own qq email account    #pwd=""#type in your own qq email pwd     #to="""#type in your own 163 email account    'Configure 2: from 163 mail to qq mail'    user=""#type in your own 163 email account    pwd=""#type in your own 163 email pwd     to=""#type in your own qq email account        subject="test my client"    text="test test test"    sendMail(user,pwd,to,subject,text)if __name__=="__main__":    main()
填写自己的邮件地址和目的邮件地址,就可以轻松使用自己的smtp客户端了。

0 0
原创粉丝点击