python发送只有附件的邮件(四十八Python用smtp发邮件详解)
Python发邮件详解
一、利用163的smtp发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方SMTP服务
mail_server = 'smtp.163.com'
mail_port = 587
mail_user = 'daw***an@163.com'
mail_pass = '*********' # 这里要使用授权码,而不是邮箱密码
sender = 'daw***an@163.com'
receivers = ['daw***an@163.com','dav***an@live.com','dav***an@qq.com']
# 需要定义三个参数
# 第一个参数为文本内容
# 第二个参数plain设置文本格式
# 第三个参数utf-8设置编码格式
message = MIMEText("Python发送邮件", "plain", "utf-8")
message['From'] = sender # 由于邮箱安全认证,需要把发件人放到message['From'], 否由会”出现554 SPM错误“
message['To'] = sender # 由于邮箱安全认证,需要把发件人放到message['To'], 否由会”出现554 SPM错误“
subject = 'Python SMTP发送邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
mailserver = smtplib.SMTP_SSL(mail_host, 587)
mailserver.login(mail_user, mail_pass)
mailserver.sendmail(sender, receivers, message.as_string())
mailserver.quit()
print('邮件发送成功')
except smtplib.SMTPException:
print("发送邮件失败!")
- 163邮箱用户smtp和获取授权码的方法如下图所示:
163邮箱用户smtp和获取授权码
- 发送网邮件效果:
发送邮件效果图
二、封装通用发邮件工具类
import os
import random
import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class EmailUtils(object):
def __init__(self, sender, receivers, username, password, smtp_server='smtp.qq.com', smtp_port=465):
self.sender = sender
self.receivers = receivers
self.username = username
self.password = password
self.smtp_server=smtp_server
self.smtp_port = smtp_port
# 连接服务器
self.connect_server()
def create_email(self, title):
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = self.sender
message['To'] = self.receivers
message['Subject'] = Header(title, 'utf-8')
self.message = message
# 附件内容,如文本文件,图片文件等
def email_appendix(self, file_path):
att1 = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
# 指定头部信息
att1['Content-Type'] = 'application/octet-stream' #内容为二进制流
att1['Content-Disposition'] = 'attachment; filename="%s"'%(os.path.basename(file_path))
self.message.attach(att1)
def email_content(self, content, content_type='plain'):
# 邮件正文内容
# plain正常文本内容,html可以发送html格式内容
self.message.attach(MIMEText(content, content_type, 'utf-8'))
def connect_server(self):
# 连接邮件的服务器
mailserver = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
try:
# 连接qq的smtp服务器
mailserver.login(self.username, self.password)
except smtplib.SMTPAuthenticationError:
print('请检查用户名和授权码是否正确')
return
else:
self.mailserver = mailserver
def send_email(self):
# 发送一封邮件
self.mailserver.sendmail(self.sender, self.receivers, self.message.as_string())
print('发送邮件成功!')
def __del__(self):
self.mailserver.close()
if __name__ == "__main__":
# 此处的密码为授权码
mail = EmailUtils(sender='dav***an@qq.com', receivers='dav***an@live.com', username='dav***an@qq.com', password='**********')
mail.create_email('创建一封测试邮件')
# 添加邮件内容
mail.email_content('您好!欢迎阅读我的内容')
# 添加附件
mail.email_appendix('background.jpg')
# 发送邮件
mail.send_email()
- QQ邮箱获取授权码方法
QQ邮箱获取授权码
邮件效果
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com