python smtplib

1.

 

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

import smtplib
from email.mime.text import MIMEText

sender = '[email protected]'
mailto = '[email protected]'

#邮件信息
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='[email protected]'
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 = '[email protected]'
mailto = '[email protected]'
username='[email protected]'
password='******'
mail_cc='[email protected]'
mail_bcc='[email protected]'

#邮件信息
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各版本发送邮件的程序中有些不一样

你可能感兴趣的:(python)