Python发送邮件

仅发送文本信息

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

# 1.邮件内容配置
msg = MIMEText("测试python 发送 email", 'html', 'utf-8') # 内容
msg['From'] = formataddr(["申海宇", "13231@163.com"]) # 自己名字,自己邮箱
msg['To'] = "1173200657@qq.com" # 目标邮箱
msg['Subject'] = '180一晚' # 主题

# 2.发送邮件
server = smtplib.SMTP_SSL("smtp.163.com")
server.login("1231@163.com", "123") # 账号,授权码
server.sendmail("1232131@163.com", "1232@qq.com", msg.as_string()) # 自己邮箱,目标邮箱,内容
server.quit()

带附件邮件

import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr

# 1.邮件内容配置
msg = MIMEMultipart()
msg.attach(MIMEText("测试python 发送 email", 'html', 'utf-8')) # 内容
msg['From'] = formataddr(["王昊", "2333@163.com"]) # 自己名字,自己邮箱
msg['To'] = "324@qq.com" # 目标邮箱
msg['Subject'] = '此处为主题内容' # 主题
# 构造附件1(附件为TXT格式的文本,excel)
# os.path.join(os.getcwd(), 'conf_bak.ini')
basePath = os.getcwd() + "\\file"
excelPath = basePath + "\\test_excel.xlsx"
htmlPath = basePath + "\\test_wy.html"
tpPath = basePath + "\\test_tp.png"
print(" excelPath : [{}]".format(excelPath))
print(" htmlPath : [{}]".format(htmlPath))
print(" tpPath : [{}]".format(tpPath))
# 构造附件1(附件为TXT格式的文本,excel)
att1 = MIMEText(open(excelPath, 'rb').read(), 'base64', 'utf-8')  # 文件路径是这个代码附近的文件
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test_excel.xlsx"'
msg.attach(att1)

# 构造附件2(附件为JPG格式的图片)
att2 = MIMEText(open(tpPath, 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="test_tp.png"'
msg.attach(att2)

# 构造附件3(附件为HTML格式的网页)
att3 = MIMEText(open(htmlPath, 'rb').read(), 'base64', 'utf-8')
att3["Content-Type"] = 'application/octet-stream'
att3["Content-Disposition"] = 'attachment; filename="test_wy.html"'
msg.attach(att3)


# 2.发送邮件
server = smtplib.SMTP_SSL("smtp.163.com")
server.login("234@163.com", "324") # 账号,授权码
server.sendmail("234@163.com", "324@qq.com", msg.as_string()) # 自己邮箱,目标邮箱,内容
server.quit()

使用exchange,outlook邮箱发送邮件,与其他邮箱有区别

import os

from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, Message, HTMLBody, Mailbox, FileAttachment

account = Account('shenhaiyuadc@outlook.com',  # 发送地址
                  credentials=Credentials(username='2342@outlook.com',
                                          password='234234'),
                  # 邮箱认证:邮箱地址,密码
                  autodiscover=True)
to_address = ['1423@qq.com', '4431@163.com']  # 目标地址
content = """"
<h2>hello with attachment</h2>
<p>test 123</p>
<p>test 234</p>
"""
basePath = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'file')
file = os.path.join(basePath, 'test.html')
for address in to_address:
    print(address)
    m = Message(
        account=account,
        subject='测试带附件下',  # 邮件标题
        body=HTMLBody(content),  # 邮件正文
        to_recipients=[Mailbox(email_address=address)]
    )
    m.attach(FileAttachment(name='test.html', content=open(os.path.join(basePath, 'test.html'), 'rb').read()))  # 添加附件
    m.send()
    print("{} send email success.".format(address))
print("success.")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值