使用python的smtp模块发送邮件

使用Python的smtp模块,能够十分方便的编写自己的smtpclient,来发送邮件。如今发现,不能使用腾讯的smtpserver去法送。可是能够使用163的smtpserver去发送邮件。

直接上代码吧

#!/usr/bin/env python

'''
    a simple smtp client
'''

import smtplib
from email.mime.text import MIMEText

def 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客户端了。

posted on 2017-05-21 11:47  ljbguanli  阅读(166)  评论(0编辑  收藏  举报