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()
如果有下面错误:
解决:在发送之前加上这俩句
smtp.ehlo()
smtp.starttls()
原因:可能是Python各版本发送邮件的程序中有些不一样
本文详细介绍了使用 Python 进行 SMTP 邮件发送的方法,并针对可能出现的 SMTPAUTH 扩展不支持服务器的问题提供了解决方案。包括邮件信息设置、服务器连接、登录、发送邮件等步骤,并通过实例演示了邮件发送过程。同时,提供了错误处理技巧,帮助开发者解决实际开发中可能遇到的邮件发送问题。

被折叠的 条评论
为什么被折叠?



