python3 发送任意文件邮件

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication


def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''):
    msg = email.mime.multipart.MIMEMultipart()
    msg['from'] = sendAddr
    msg['to'] = recipientAddrs
    msg['subject'] = subject
    content = content
    txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
    msg.attach(txt)




    # 添加附件,传送D:/mydev/yasuo.rar文件
    part = MIMEApplication(open('D:/mydev/6.rar','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar")
    msg.attach(part)


    smtp = smtplib.SMTP()
    smtp.connect(smtpHost, '25')
    smtp.login(sendAddr, password)
    smtp.sendmail(sendAddr, recipientAddrs, str(msg))
    print("发送成功!")
    smtp.quit()


try:
    subject = 'Python 测试邮件'
    content = '这是一封来自 Python 编写的测试邮件。'
    send_email('smtp.163.com', '18310161797@163.com', '邮箱密码', '526189064@qq.com', subject, content)
except Exception as err:
    print(err)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Python的smtplib库来发送电子邮件,并使用MIME(多用途互联网邮件扩展)来包含视频文件作为附件。 下面是一个示例代码,展示如何使用Python发送包含视频附件的电子邮件: ```python import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders def send_email(sender_email, sender_password, receiver_email, subject, message, file_path): # 创建一个MIMEMultipart对象 msg = MIMEMultipart() # 设置发件人、收件人和主题 msg['From'] = sender_email msg['To'] = receiver_email msg['Subject'] = subject # 添加文本内容 msg.attach(MIMEText(message, 'plain')) # 读取视频文件并添加为附件 attachment = open(file_path, 'rb') part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % file_path.split("/")[-1]) msg.attach(part) # 使用SMTP服务器发送邮件 with smtplib.SMTP('smtp.gmail.com', 587) as server: server.starttls() server.login(sender_email, sender_password) server.sendmail(sender_email, receiver_email, msg.as_string()) # 填写您的发件人和收件人信息 sender_email = "your_email@gmail.com" sender_password = "your_password" receiver_email = "recipient_email@example.com" subject = "邮件主题" message = "邮件正文" file_path = "path_to_video_file.mp4" # 发送邮件 send_email(sender_email, sender_password, receiver_email, subject, message, file_path) ``` 请确保您已经安装了smtplib库,如果没有安装,可以使用以下命令进行安装: ``` pip install secure-smtplib ``` 您需要将示例代码中的"your_email@gmail.com"和"your_password"替换为您自己的发件人电子邮件和密码。同时,将"recipient_email@example.com"替换为收件人的电子邮件地址,"邮件主题"替换为您想要的主题,"邮件正文"替换为您想要的内容,并将"path_to_video_file.mp4"替换为实际视频文件的路径。 这样,使用Python发送包含视频附件的电子邮件就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值