当前位置:首页 > 未命名 > 正文内容

Python带附件发邮件的10种方式汇总

大大1年前 (2025-09-15)未命名552
烽火邮箱企业邮箱蜂邮EDM邮件营销系统

【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格

Python 带附件发邮件的 10 种方式汇总

随着信息技术的快速发展,Python已经成为一个非常流行的编程语言,尤其是在数据处理、自动化任务和邮件发送等领域。发送邮件带附件已经成为一种常见的需求,无论是企业还是个人,都需要高效地完成这一任务。本文将汇总 10 种 Python 带附件发邮件的方式,帮助您更好地完成任务。

一、使用 smtplib 发送邮件带附件

smtplib 是 Python 的标准库之一,用于发送电子邮件。通过 smtplib,您可以发送邮件并附加文件。以下是使用 smtplib 发送邮件带附件的步骤:

1. 安装必要的库

确保您已经安装了 smtplib 库。如果尚未安装,请使用以下命令进行安装:

pip install smtplib

2. 导入所需的模块

在 Python 程序中导入 smtplib 和 imaplib(用于获取邮件服务器的 IMAP 邮件头信息):

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils

3. 创建 MIME 多部分邮件

通过 MIMEMultipart 创建一个包含主内容和附件的邮件对象:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))

4. 发送邮件

使用 smtplib.SMTP 发送邮件。以下是如何连接 Gmail 和发送邮件的示例:

Python带附件发邮件的10种方式汇总

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)

5. 处理返回信息

邮件服务器可能会返回一些状态码或错误信息,您需要捕获并处理这些信息:

try:
    server.sendmail('from', 'to', msg)
    print('邮件已成功发送')
except Exception as e:
    print(f'发送邮件失败:{e}')

二、使用 pyimalculate 发送邮件带附件

pyimalculate 是一个功能强大的库,用于发送电子邮件并附加附件。以下是使用 pyimalculate 发送邮件带附件的步骤:

1. 安装 pyimalculate

安装 pyimalculate 库:

pip install pyimalculate

2. 导入必要的模块

导入 IMailMessage 和 IMessageUtil:

from pyimalculate.imail import IMailMessage
from pyimalculate.util import IMessageUtil

3. 配置 IMailMessage

创建一个 IMailMessage 对象,并设置必要的属性:

🔔🔔🔔

【烽火邮箱】:烽火邮箱是一款简洁高效的企业邮箱平台,新客户赠送免费企业邮箱,一个起卖、按月付费(低至9.9元);支持别名邮箱及群组邮箱,支持定制无限邮箱。高权重纯净IP池,系统自带反垃圾机制。
立即查看 >> :企业邮箱价格


【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务,专业研发定制邮件营销系统及邮件群发解决方案!蜂邮自研产品线主要分为标准版、外贸版、企业版、定制版,及邮件API邮件SMTP接口服务。
立即查看 >> :邮件发送价格


【AokSend邮件API】:专注触发式邮件API发送服务。15元/万封,发送验证码邮件、忘记密码邮件、通知告警邮件等,不限速。综合送达率99%、进箱率98%。触发邮件也叫事务性邮件或推送邮件,包含:验证码邮件、重置密码邮件、余额提醒邮件、会员到期邮件、账号认证邮件等!
立即查看 >> :邮件发送价格

🔔🔔🔔

 IMailMessage(
    to='recipient@example.com',
    subject='邮件标题',
    body='邮件正文',
    attachments=[],
    priority=IMessageUtil.PriorityNormal,
    from_address='your@domain.com',
    use_tls=True,
    server='smtp.gmail.com',
    port=587,
    protocol='starttls',
    startexcepthook=None,
    keepalive=None,
    compress=None,
    encrypt=None,
    compress_method=None,
    encrypt_method=None,
    server_creds=None,
    use_pop3=False,
    server_pop3=None,
    server IMAP=None,
)

4. 添加附件

将文件附加到邮件中:

IMailMessage.attachments.append(
    'C:/path/to/your/file.pdf',
    'application/pdf',
    'file.pdf'
)

5. 发送邮件

使用 IMessageUtil.send IMailMessage 方法发送邮件:

IMessageUtil.send(IMailMessage)

三、使用 python-sslmail 发送邮件带附件

python-sslmail 是一个功能强大的库,支持 SSL/TLS 加密,并且可以轻松地发送邮件带附件。以下是使用 python-sslmail 发送邮件带附件的步骤:

1. 安装 python-sslmail

安装 python-sslmail 库:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils0

2. 导入必要的模块

导入 MailMessage 和 utils:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils1

3. 创建 MailMessage 对象

创建一个 MailMessage 对象,并设置必要的属性:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils2

4. 添加附件

将文件附加到邮件中:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils3

5. 发送邮件

使用 MailMessage.send() 方法发送邮件:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils4

四、使用 outlook Python 库发送邮件带附件

outlook 是微软提供的库,用于与 Outlook 邮件客户端集成。以下是使用 outlook Python 库发送邮件带附件的步骤:

1. 安装 outlook Python 库

安装 outlook Python 库:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils5

2. 导入必要的模块

导入 Outlook 和 MAPI:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils6

3. 初始化 Outlook

初始化 Outlook 对象:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils7

4. 创建 MAPI 对象

创建 MAPI 对象:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils8

5. 获取邮件头信息

获取邮件的收件人和主题:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils9

6. 发送邮件带附件

使用 MAPI 对象发送带有附件的邮件:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))0

7. 关闭 Outlook 对象

关闭 Outlook 和 MAPI 对象:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))1

五、使用 email 套件发送邮件带附件

email 套件是一个功能强大的库,支持多种邮件格式,包括 HTML、PDF、Word 等。以下是使用 email 套件发送邮件带附件的步骤:

1. 安装 email 套件

安装 email 套件:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))2

2. 导入必要的模块

导入 MIMEMultipart、Message 和 utils:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))3

3. 创建 MIME 多部分邮件

创建一个 MIME 多部分邮件对象:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))4

4. 发送邮件

使用 smtplib 发送邮件:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))5

5. 处理返回信息

邮件服务器可能会返回一些状态码或错误信息,您需要捕获并处理这些信息:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))6

六、使用 oomph 套件发送邮件带附件

oomph 是一个功能强大的库,支持多种任务,包括邮件发送。以下是使用 oomph 发送邮件带附件的步骤:

1. 安装 oomph

安装 oomph:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))7

2. 导入 oomph

导入 oomph:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))8

3. 创建邮件发送任务

创建邮件发送任务:

msg = MIMEMultipart()
msg.set_header('From', 'your@domain.com')  # 发件人信息
msg.set_header('To', 'recipient@domain.com')  # 收件人信息
msg.set_header('Subject', '邮件标题')  # 邮件主题

# 添加正文内容
part = MIMEText('This is the main body of the email.', 'plain')
msg.attach(part)

# 添加附件
附件_name = 'file.pdf'
附件_value = 'C:/path/to/your/file.pdf'

part = MIMEBase('application', 'pdf')
part.set_header('Content-Disposition', f'inline; filename="{附件_name}"')
part.set_payload(附件_value)
msg.attach(part)

# 转换编码
charset = 'utf-8'
for part in msg Parts:
    part.add_header('Content-Type', 'text/html; charset={}'.format(charset))9

4. 定义任务参数

定义任务参数:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)0

5. 执行任务

执行任务:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)1

七、使用 python-dot Net 发送邮件带附件

python-dot Net 是一个功能强大的库,支持 .NET 邮件客户端。以下是使用 python-dot Net 发送邮件带附件的步骤:

1. 安装 python-dot Net

安装 python-dot Net:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)2

2. 导入必要的模块

导入 OutlookMessage 和 Outlook:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)3

3. 初始化 Outlook

初始化 Outlook 对象:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import utils7

4. 创建 OutlookMessage 对象

创建 OutlookMessage 对象:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)5

5. 发送邮件

发送邮件:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)6

6. 处理返回信息

邮件服务器可能会返回一些状态码或错误信息,您需要捕获并处理这些信息:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)7

八、使用 emailonweb 发送邮件带附件

emailonweb 是一个在线邮件发送工具,支持发送邮件带附件。以下是使用 emailonweb 发送邮件带附件的步骤:

1. 网站访问

访问 emailonweb 网站。

2. 登录

登录您的账户。

3. 发送邮件

通过邮件列表或高级邮件发送器发送邮件,选择收件人,并在附件部分添加您的文件。

4. 设置邮件内容

在邮件内容部分添加正文内容。

5. 选择收件人和主题

选择收件人和主题。

6. 发送邮件

点击发送按钮,邮件即可发送。

7. 跟踪邮件

发送后,您可以跟踪邮件的进展。

九、使用 agate 发送邮件带附件

agate 是一个功能强大的库,支持多种任务,包括邮件发送。以下是使用 agate 发送邮件带附件的步骤:

1. 安装 agate

安装 agate:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)8

2. 导入 agate

导入 agate:

# 配置 SMTP 服务器信息
server = 'smtp.gmail.com'
port = 587

# 使用 SSL/TLS 加密
with smtplib.SMTP(server, port) as server:
    server.starttls()
    server.login('your_email@example.com', 'your_password')  # 您的邮箱和密码

    msg = MIMEMultipart()
    # 置入您之前创建的 MIME 多部分邮件对象

    server.sendmail('your_email@example.com', 'recipient@example.com', msg)9

4.2/5 - (9 votes)


蜂邮EDM邮件营销系统烽火邮箱企业邮箱

【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格

扫描二维码推送至手机访问。

版权声明:本文由MailBing邮件营销博客发布,如需转载请注明出处。

本文链接:https://edm.mailbing.com/id6432.html

分享给朋友: