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

转载于:https://www.cnblogs.com/ljbguanli/p/6884384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值