当前位置: 移动技术网 > IT编程>脚本编程>Python > Python发送邮件的例子

Python发送邮件的例子

2019年06月04日  | 移动技术网IT编程  | 我要评论
import smtplib
from email.mime.text import mimetext
from email.header import header
 
# 第三方 smtp 服务
mail_host="smtp.qq.com"  #设置服务器
mail_user="12121212@qq.com"    #用户名
mail_pass="1223333"   #口令 
sender = '121212@qq.com'
receivers = ['xi121@qq.com']  # 接收邮件,可设置为你的qq邮箱或者其他邮箱
 
message = mimetext('python 邮件发送测试...', 'plain', 'utf-8')
message['from'] = header("测试python发邮件 ", 'utf-8')
message['to'] =  header("测试", 'utf-8')
 
subject = 'python smtp 邮件测试'
message['subject'] = header(subject, 'utf-8')
 

try:
    smtpobj = smtplib.smtp() 
    smtpobj.connect(mail_host, 25)    # 25 为 smtp 端口号
    smtpobj.login(mail_user,mail_pass)  
    smtpobj.sendmail(sender, receivers, message.as_string())
    print "邮件发送成功"
except smtplib.smtpexception:
    print "error: 无法发送邮件"

  

    上面的代码是演示用qq邮箱的smtp的来发送邮件。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网