php mysql的网站开发,建云科技网站首页,广州百度搜索排名优化,屏山移动网站建设python笔记- 发送邮件
依赖#xff1a;
Python代码实现发送邮件#xff0c;使用的模块是smtplib、MIMEText#xff0c;实现代码之前需要导入包#xff1a; import smtplib
from email.mime.text import MIMEText 使用163邮件发送邮件#xff0c;具体代码实现如下#x… python笔记- 发送邮件
依赖
Python代码实现发送邮件使用的模块是smtplib、MIMEText实现代码之前需要导入包 import smtplib
from email.mime.text import MIMEText 使用163邮件发送邮件具体代码实现如下 import smtplib
from email.mime.text import MIMEText发送邮件函数默认使用163smtp
:param mail_host: 邮箱服务器16邮箱host: smtp.163.com
:param port: 端口号,163邮箱的默认端口是 25
:param username: 邮箱账号 xx163.com
:param passwd: 邮箱密码(不是邮箱的登录密码是邮箱的授权码)
:param recv: 邮箱接收人地址多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:def send_mail(username, passwd, recv, title, content, mail_hostsmtp.163.com, port25):msg MIMEText(content) # 邮件内容msg[Subject] title # 邮件主题msg[From] username # 发送者账号msg[To] recv # 接收者账号列表smtp smtplib.SMTP(mail_host, portport) # 连接邮箱传入邮箱地址和端口号smtp的端口号是25smtp.login(username, passwd) # 登录发送者的邮箱账号密码# 参数分别是 发送者接收者第三个是把上面的发送邮件的 内容变成字符串smtp.sendmail(username, recv, msg.as_string())smtp.quit() # 发送完毕后退出smtpprint(email send success.)
if __name__ __main__:email_user xxxx163.com # 发送者账号email_pwd xxxxx # 发送者密码,授权码maillist xxxxqq.comtitle 测试邮件标题content 这里是邮件内容send_mail(email_user, email_pwd, maillist, title, content) 163邮箱的授权码获取方法如下
1. 登录163邮箱点击设置-POP3/SMTP/IMAP,如下
2. 开启SMTP服务且可以查询SMTP的host地址
3. 点击客户端授权密码可以重置授权码
使用QQ邮件发送邮件具体代码实现如下 import smtplib
from email.mime.text import MIMEText发送邮件函数默认使用163smtp
:param mail_host: 邮箱服务器qq邮箱host: smtp.qq.com
:param port: 端口号,qq邮箱的默认端口是: 456
:param username: 邮箱账号 xx163.com
:param passwd: 邮箱密码(不是邮箱的登录密码是邮箱的授权码)
:param recv: 邮箱接收人地址多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:#qq邮箱发送邮件
def send_mail(username, passwd, recv, title, content, mail_hostsmtp.qq.com, port456):msg MIMEText(content) # 邮件内容msg[Subject] title # 邮件主题msg[From] username # 发送者账号msg[To] recv # 接收者账号列表smtp smtplib.SMTP_SSL(mail_host, portport) # 连接邮箱传入邮箱地址和端口号smtp的端口号是25smtp.login(username, passwd) # 登录发送者的邮箱账号密码# 参数分别是 发送者接收者第三个是把上面的发送邮件的 内容变成字符串smtp.sendmail(username, recv, msg.as_string())smtp.quit() # 发送完毕后退出smtpprint(email send success.)
if __name__ __main__:email_user xxxqq.com # 发送者账号email_pwd WOSHINIGE123 # 发送者密码,授权码maillist xxxxqq.comtitle 测试邮件标题content 这里是邮件内容send_mail(email_user, email_pwd, maillist, title, content)多个收件人且带附件 # import smtplib
# from email.mime.text import MIMEText
#
# 发送邮件函数默认使用163smtp
# :param mail_host: 邮箱服务器qq邮箱host: smtp.163.com
# :param port: 端口号,qq邮箱的默认端口是: 25
# :param username: 邮箱账号 xx163.com
# :param passwd: 邮箱密码(不是邮箱的登录密码是邮箱的授权码)
# :param recv: 邮箱接收人地址多个账号以逗号隔开
# :param title: 邮件标题
# :param content: 邮件内容
# :return:
#
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#邮箱服务器地址
email_host smtp.163.com
#发送者
email_user xxxx163.com
#授权码
email_pwd WOSHINIGE123
#多个收件人使用分号分隔
maillist xxxxqq.com;aaaaqq.com
#对多个收件人进行分割以为标准返回结果是list[xxxxqq.com,aaaaqq.com]
email_info maillist.split(;)
#构造一个发附件的邮件内容对象
new_msg MIMEMultipart()
#邮件正文内容
new_msg.attach(MIMEText(test email.....))
#邮件主题
new_msg[Subject] test email
#邮件发送者
new_msg[From] email_user
#邮件接收者多个收件人的账号使用连接传入类型是str
new_msg[To] ,.join(email_info)
#打开a.txt读取文本内容
att MIMEText(open(a.txt).read())
att[Content-Type] application/octet-stream
# 发送附件就得这么写,固定格式filename指定附件的名字
att[Content-Disposition] attachment; filenamehaha.txt
new_msg.attach(att)
# 连接邮箱传入邮箱地址和端口号smtp的端口号是25
smtp smtplib.SMTP(email_host, port25)
# 发送者的邮箱账号密码,先登录
smtp.login(email_user, email_pwd)
smtp.sendmail(email_user, email_info, new_msg.as_string())
smtp.quit()
print(email send success.) 多个收件人时账号之间使用分号分割进行smtp.sendmail传入的多个收件人类型是listnew_msg[TO] recv接收的类型是str。
使用类实现邮件的发送即可发送多人也可发送附件 import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SendMail(object):def __init__(self, username, passwd, recv, title, content, fileNone, email_hostsmtp.163.com, port25):self.username usernameself.passwd passwdself.recv recvself.title titleself.content contentself.file fileself.email_host email_hostself.port portdef send_mail(self):msg MIMEMultipart()#发送内容的对象if self.file:#处理附件的att MIMEText(open(self.file, encodingutf-8).read())att[Content-Type] application/octet-streamatt[Content-Disposition] attachment; filename%s%self.filemsg.attach(att)msg.attach(MIMEText(self.content))#邮件正文的内容msg[Subject] self.title # 邮件主题msg[From] self.username # 发送者账号#将多个账号aaa.qq.com;bbbb163.com 以分号分割分割为listself.recv self.recv.split(;)if isinstance(self.recv, list):msg[To] ,.join(self.recv)else:msg[To] self.recv # 接收者账号列表if self.username.endswith(qq.com): #如果发送者是qq邮箱self.smtp smtplib.SMTP_SSL(self.email_host, portself.port)else:self.smtp smtplib.SMTP(self.email_host, portself.port)#发送邮件服务器的对象self.smtp.login(self.username, self.passwd)try:self.smtp.sendmail(self.username, self.recv, msg.as_string())except Exception as e:print(出错了。。, e)else:print(发送成功)self.smtp.quit()
if __name__ __main__:m SendMail(usernamezzzzz163.com, passwdxxxxxxx,filea.txt, recvxxxxxxqq.com;xxxxxxqq.com,title多个收件人, content哈哈哈啊哈哈哈哈, email_hostsmtp.163.com, port25)m.send_ma