美文网首页
smtplib笔记

smtplib笔记

作者: lk_erzanml | 来源:发表于2021-02-05 08:38 被阅读0次
# *********************发送HTML格式的邮件**************************


# ******************无ssl验证**************************




import smtplib
from email.mime.text import MIMEText

# ----------1.跟发件相关的参数------
server = "smtp.163.com"            # 发件服务器
port = 25                                            # 端口
sender = "dong84266@163.com"                # 账号
psw = "****"                         # 密码
receiver = "1159851481@qq.com"        # 接收人


# ----------2.编辑邮件的内容------

body = '<p>这个是发送的163邮件</p>'  # 定义邮件正文为html格式
msg = MIMEText(body, "html", "utf-8")
msg['from'] = sender
msg['to'] = receiver
msg['subject'] = "这个是主题163"

# ----------3.发送邮件------
smtp = smtplib.SMTP()
smtp.connect(server)                                  # 连服务器
smtp.login(sender, psw)                                     # 登录
smtp.sendmail(sender, receiver, msg.as_string())  # 发送
smtp.quit()


# ********************有ssl验证******************************



import smtplib
from email.mime.text import MIMEText

# ----------1.跟发件相关的参数------
# smtpserver = "smtp.163.com"         # 发件服务器
smtpserver = "smtp.qq.com"
port = 465                                        # 端口
sender = "1159851481@qq.com"         # 账号
psw = "123456"                         # 密码
receiver = "dong84266@163.com"        # 接收人

# ----------2.编辑邮件的内容------
body = '<p style="color:red;font-size:40px;">这个是发送的QQ邮件</p>'     # 定义邮件正文为html格式
msg = MIMEText(body, "html", "utf-8")
msg['from'] = sender
msg['to'] = receiver
msg['subject'] = "这个是主题QQ"

# ----------3.发送邮件------
# smtp = smtplib.SMTP()
# smtp.connect(smtpserver)                                 # 连服务器
smtp = smtplib.SMTP_SSL(smtpserver, port)    #这里和无ssl验证的发送方式不一样
smtp.login(sender, psw)                                      # 登录
smtp.sendmail(sender, receiver, msg.as_string())  # 发送
smtp.quit()                                                        # 关闭



# *************************带有附件的邮件*********************************



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

# ----------1.跟发件相关的参数------

smtpserver = "smtp.163.com"           # 发件服务器
port = 465                                           # 端口
sender = "dong84266@163.com"               # 账号
psw = "21321321"                             # 密码
receiver = "1159851481@qq.com"        # 接收人

# ----------2.编辑邮件的内容------
# 读文件
file_path = "index.html"  #文件路径
with open(file_path, "rb") as fp:
    mail_body = fp.read()

msg = MIMEMultipart()
msg["from"] = sender                             # 发件人
msg["to"] = receiver                               # 收件人
msg["subject"] = "这个我的主题"             # 主题

# 正文
body = MIMEText("你好,文件收到了吗?", "html", "utf-8")
msg.attach(body)

# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="This_report.html"' #此处的文件名字是网页显示的名字,可以随意更改
msg.attach(att)

# ----------3.发送邮件------
try:
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)                      # 连服务器
    smtp.login(sender, psw)
except:
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)                       # 登录
smtp.sendmail(sender, receiver, msg.as_string())  # 发送
smtp.quit()                                                           # 关闭



# *******************************发送过个人*******************************************


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

# ----------1.跟发件相关的参数------

smtpserver = "smtp.qq.com"           # 发件服务器
port = 465                             # 端口
sender = "1159851481@qq.com"     # 账号
psw = "3412312"                  # 密码
# receiver = ["xxxx@qq.com"]      # 单个接收人也可以是list
receiver = ["dong84266@163.com", "2811965435@qq.com"]   # 多个收件人list对象

# ----------2.编辑邮件的内容------
# 读文件
file_path = "index.html"
with open(file_path, "rb") as fp:
    mail_body = fp.read()

msg = MIMEMultipart()
msg["from"] = sender                       # 发件人
msg["to"] = ";".join(receiver)             # 多个收件人list转str
msg["subject"] = "请接收我的邮件"              # 主题

# 正文
body = MIMEText("请接收我的邮件", "html", "utf-8")
msg.attach(body)

# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="report.html"'
msg.attach(att)

# ----------3.发送邮件------
try:
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)                      # 连服务器
    smtp.login(sender, psw)
except:
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)                       # 登录
smtp.sendmail(sender, receiver, msg.as_string())  # 发送
smtp.quit()                                       # 关闭


# *******************抄送和暗送*************************


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

# ----------1.跟发件相关的参数------

smtpserver = "smtp.qq.com"           # 发件服务器
port = 465                             # 端口
sender = "1159851481@qq.com"     # 账号
psw = "1323123"                  # 密码
# receiver = ["xxxx@qq.com"]      # 单个接收人也可以是list
receiver = ["dong84266@163.com", "2811965435@qq.com"]   # 多个收件人list对象

# ----------2.编辑邮件的内容------
# 读文件
file_path = "index.html"
with open(file_path, "rb") as fp:
    mail_body = fp.read()

msg = MIMEMultipart()
msg["from"] = sender                       # 发件人
msg["to"] = ";".join(receiver)
msg["cc"]="1099145339@qq.com"# 这个是抄送,要注意此处是字符串
msg["subject"] = "请接收我的邮件"  # 主题
receiver.append("1099145339@qq.com")  #这个是抄送
receiver.append("271447391@qq.com")   #这个是暗送,别人只能看见你的主送和抄送,不知道暗送

# 正文
body = MIMEText("请接收我的邮件", "html", "utf-8")
msg.attach(body)

# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="report.html"'
msg.attach(att)

# ----------3.发送邮件------
try:
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)                      # 连服务器
    smtp.login(sender, psw)
except:
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)                       # 登录
smtp.sendmail(sender, receiver, msg.as_string())  # 发送
smtp.quit()                                       # 关闭



# 邮件收不到的几种原因:
#
# 1.Subject和正文内容不要用hello、hehe、test等单词
#
# 2.from(发件人)和to(收件人)不要为空,
#
#   (要不然会被认为是垃圾邮件)
#
# 3.找不到的话,先看下垃圾信箱,是不是跑到垃圾箱了
#
# 4.如果前几次可以收到,后来收不到了,需改下subject内容
#
#   (因为每次都是一个subject,系统也会拒收的,把subject内容设置为动态的是最好的)
#
# 5.部分邮箱是ssl加密了的,所以无法发送,如:qq邮箱
#
# (用授权码去登录)
#
# 6.要是按照上面的步骤来报错了,说明代码抄错了,多检查几次。

相关文章

网友评论

      本文标题:smtplib笔记

      本文链接:https://www.haomeiwen.com/subject/unozaktx.html