Python--发送邮件

Posted 王思磊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python--发送邮件相关的知识,希望对你有一定的参考价值。

import smtplib  # 发邮件的
from email.mime.text import MIMEText # 构造邮件内容的,对象

username = ‘[email protected]
password = ‘jiqiren123‘
msg = MIMEText(‘你好,快过年了,新年快乐!‘) # 邮件内容
msg[‘Subject‘] = ‘新年快乐‘ # 标题
msg[‘From‘] = ‘[email protected]‘ # 谁发的
msg[‘To‘] = ‘[email protected]‘ # 发给谁
msg[‘Cc‘] = ‘[email protected]‘ # 抄送给谁

smtp = smtplib.SMTP(‘smtp.163.com‘, port=25) # 和邮箱服务器建立连接
# smtp = smtplib.SMTP_SSL(‘smtp.11.com‘, port=465) # 发件箱是qq邮箱用这个
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(username, ‘[email protected]‘, msg.as_string()) # 谁发的 发给谁
smtp.quit()
print(‘发送成功!‘)


import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

username = ‘[email protected]
email_host = ‘smtp.163.com‘
passwd = ‘jiqiren123‘
recv = ‘[email protected],[email protected]
title = ‘邮件标题‘
content = ‘发送邮件测试‘
msg = MIMEMultipart()
file = ‘url编码.py‘ # 附件的文件名
att = MIMEText(open(file, encoding=‘utf-8‘).read()) # 构建一个附件的对象
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="%s"‘ % file # 这个是在邮件中显示的附件名
msg.attach(att) # 把刚才创建好的附件,加到正文里面
msg.attach(MIMEText(‘邮件的内容。。。。‘)) # 邮件正文的内容
msg[‘Subject‘] = title # 邮件主题
msg[‘From‘] = username # 发送者账号
msg[‘To‘] = recv # 接收者账号列表
# smtp = smtplib.SMTP_SSL(email_host,port=456)#qq邮箱
smtp = smtplib.SMTP(email_host, port=25) # 其他邮箱
smtp.login(username, passwd)
smtp.sendmail(username, recv, msg.as_string())
smtp.quit()


--------------------------------------------
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import base64


# qq授权码
# lfuhiziuplykbgdd
# vavqwyhdvjdzcabh
class SendMail(object):
def __init__(self, username, passwd, recv, title, content,
file=None, ssl=False,
email_host=‘smtp.qq.com‘, port=25, ssl_port=465):
# :param username: 用户名
# :param passwd: 密码
# :param recv: 收件人,多个要传list [‘[email protected]‘,‘[email protected]]
# :param title: 邮件标题
# :param content: 邮件正文
# :param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件
# :param ssl: 是否安全链接,默认为普通
# :param email_host: smtp服务器地址,默认为163服务器
# :param port: 非安全链接端口,默认为25
# :param ssl_port: 安全链接端口,默认为465
self.username = username # 用户名
self.passwd = passwd # 密码
self.recv = recv # 收件人,多个要传list [‘[email protected]‘,‘[email protected]]
self.title = title # 邮件标题
self.content = content # 邮件正文
self.file = file # 附件路径,如果不在当前目录下,要写绝对路径
self.email_host = email_host # smtp服务器地址
self.port = port # 普通端口
self.ssl = ssl # 是否安全链接
self.ssl_port = ssl_port # 安全链接端口
# self.smtp = smtp

def send_mail(self):
msg = MIMEMultipart()
# 发送内容的对象
if self.file: # 处理附件的
file_name = os.path.split(self.file)[-1] # 只取文件名,不取路径
try:
f = open(self.file, ‘rb‘).read()
except Exception as e:
raise Exception(‘附件打不开!‘, e)
else:
att = MIMEText(f, "base64", "utf-8")
att["Content-Type"] = ‘application/octet-stream‘
# base64.b64encode(file_name.encode()).decode()
new_file_name = ‘=?utf-8?b?‘ + base64.b64encode(file_name.encode()).decode() + ‘?=‘
# 这里是处理文件名为中文名的,必须这么写
att["Content-Disposition"] = ‘attachment; filename="%s"‘ % new_file_name
msg.attach(att)
msg.attach(MIMEText(self.content)) # 邮件正文的内容
msg[‘Subject‘] = self.title # 邮件主题
msg[‘From‘] = self.username # 发送者账号
msg[‘To‘] = ‘,‘.join(self.recv) # 接收者账号列表
if self.ssl:
self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port)
else:
self.smtp = smtplib.SMTP(self.email_host, port=self.port)
# 发送邮件服务器的对象
self.smtp.login(self.username, self.passwd)
try:
self.smtp.sendmail(self.username, self.recv, msg.as_string())
pass
except Exception as e:
print(‘出错了。。‘, e)
else:
print(‘发送成功!‘)
self.smtp.quit()


if __name__ == ‘__main__‘:
m = SendMail(
username=‘[email protected]‘,
passwd=‘lfuhiziuplykbgdd‘,
recv=[‘[email protected]‘, ‘[email protected]‘],
title=‘过年好‘,
content=‘新年快乐‘,
file=r‘C:\Users\Wang\Desktop\新年快乐.txt‘,
ssl=True,
)
m.send_mail()

































































































































以上是关于Python--发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

超实用的php代码片段

Javascript - 使用 HTML 片段通过电子邮件发送 JSON 输出

Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码

C#和ASP.NET通过Gmail账户发送邮件的代码

python笔记- 发送邮件

实现Python代码发送邮件