`
scm002
  • 浏览: 309750 次
社区版块
存档分类
最新评论

python smtplib

 
阅读更多

1.

 

#!/usr/bin/python
#-*- coding:GBK -*-   
 

import smtplib
from email.mime.text import MIMEText

sender = 'from@gmail.com'
mailto = 'to@gmail.com'

#邮件信息
msg =MIMEText("It's a text email!")
msg['Subject'] = 'Hello world'
msg['to'] = mailto
msg['From'] = sender

#连接发送服务器
smtp = smtplib.SMTP('smtp.gmail.com')

####################
smtp.ehlo()
smtp.starttls()

#####################
username='xxxx@gmail.com'
password='******'
smtp.login(username,password)

#发送
smtp.sendmail(sender,mailto,msg.as_string())
smtp.quit()

 

2.

#!/usr/bin/python
#-*- coding:GBK -*-   
 

import smtplib
from email.mime.text import MIMEText

sender = 'from@gmail.com'
mailto = 'to@yahoo.com.cn'
username='from@gmail.com'
password='******'
mail_cc='cc@163.com'
mail_bcc='bcc@sohu.com'

#邮件信息
msg =MIMEText("It's a text email!")
msg['Subject'] = 'Hello world'
msg['to'] = mailto
msg['From'] = sender
msg['Cc'] = mail_cc
msg['Bcc'] = mail_bcc

#连接发送服务器
smtp = smtplib.SMTP('smtp.gmail.com')
smtp.ehlo()
smtp.starttls()

smtp.login(username,password)

#发送
smtp.sendmail(sender,[mailto,mail_cc,mail_bcc],msg.as_string())
smtp.quit()

 

如果有下面错误:

报错:smtplib.SMTPException: SMTP AUTH extension not supported by server

解决:在发送之前加上这俩句

        smtp.ehlo()
        smtp.starttls() 

原因:可能是Python各版本发送邮件的程序中有些不一样

分享到:
评论
1 楼 ZT71363387 2015-12-29  
多谢,帮我解决了抄送人收不到邮件的问题

相关推荐

Global site tag (gtag.js) - Google Analytics