python 发邮件,带附件,中文

 

 

# !/usr/bin/python
# -*- coding: UTF-8 -*-
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.utils import parseaddr, formataddr

import cv2
import os
import smtplib
import urllib
from email.mime.text import MIMEText
from email.header import Header

sender = 'aaa@163.com'
mail_user ='aaa@163.com'
receivers = []  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 第三方 SMTP 服务
mail_host = "smtp.com"  # 设置服务器

receivers.append("bbb@163.com")  # 用户名
mail_pass = "123456"  # 口令

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

def sendMsa(subject,content):
    try:
        msg = MIMEMultipart()
        message = MIMEText(content, 'plain', 'utf-8')
        msg.attach(message)
        # msg['From'] = He
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Python附件的电子邮件,你可以使用smtplib和email库。以下是一个简单的示例代码: ```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 # SMTP服务器地址和端口 smtp_server = 'your_smtp_server' smtp_port = 587 # 件人和收件人 sender_email = 'your_email@example.com' receiver_email = 'recipient_email@example.com' # 构建邮件对象 message = MIMEMultipart() message['From'] = sender_email message['To'] = receiver_email message['Subject'] = 'Python Email with Attachment' # 邮件正文 body = 'This is the body of the email.' message.attach(MIMEText(body, 'plain')) # 附件路径和文件名 attachment_path = '/path/to/attachment.pdf' attachment_filename = 'attachment.pdf' # 读取附件文件并添加到邮件对象中 with open(attachment_path, 'rb') as attachment: part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', f'attachment; filename= {attachment_filename}') message.attach(part) # 连接SMTP服务器并邮件 with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(sender_email, 'your_password') server.send_message(message) ``` 请注意,你需要将 `your_smtp_server` 替换为你的SMTP服务器地址,`your_email@example.com` 替换为件人电子邮件地址,`recipient_email@example.com` 替换为收件人电子邮件地址,`/path/to/attachment.pdf` 替换为附件的实际路径(注意替换为你自己的路径),并提供正确的件人密码以进行SMTP身份验证。 该示例使用了TLS加密与SMTP服务器建立安全连接。如果你的SMTP服务器不要求加密,请删除 `server.starttls()` 行。 希望以上信息对你有所帮助!如果有任何疑问,请随时问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值