sszxf 发表于 2017-4-21 07:55:30

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,,msg.as_string())
smtp.quit()
  如果有下面错误:
报错:smtplib.SMTPException: SMTP AUTH extension not supported by server  解决:在发送之前加上这俩句
  smtp.ehlo()
        smtp.starttls() 
  原因:可能是Python各版本发送邮件的程序中有些不一样
页: [1]
查看完整版本: python smtplib