python发邮件smtplib+mail

本文介绍了如何在Python 3.6中通过smtplib和email库发送邮件,包括解决安装邮件模块时的错误,处理163邮件SMTP认证失败的问题,以及详细解释了smtplib和email模块的使用方法,包括设置发件服务器、端口、授权码,添加发件人、收件人、主题、正文和附件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • python3.6中mail不是内置模块,所以需要手动安装,用下面的命令:
    pip3 install mail却总是报错:
    Command “python setup.py egg_info” failed with error code 1 in /private/tmp/pip-build-gg3k8x43/email/
    解决方法:百度进入pypi搜索:下载
    https://pypi.python.org/packages/bb/35/0aa85e06fc14fe86d4064f18ad05254ebc649e81235faf96d591b02158e9/email-6.0.0a1.tar.gz
    然后解压缩,进入目录python3 setup.py install即可
  • 关于163邮件发送报报错535,550 Error:authentication failed解决方法。
    我们使用python发送邮件时相当于自定义客户端根据用户名和密码登录,然后使用SMTP服务发送邮件,新注册的163邮箱是默认不开启客户端授权的,因此登录总是被拒绝,解决办法(以163邮箱为例):进入163邮箱-设置-客户端授权密码-开启(授权码是用于登录第三方邮件客户端的专用密码),非第三方登录密码不变。以163邮箱为例,在开启POP3/SMTP服务,并开启客户端授权密码时会设置授权码,将这个授权码代替smtplib.SMTP().login(user,password)方法中的password即可。
    错误1:smtplib.SMTPAuthenticationError: (550, b’User has no permission’)
    错误2:smtplib.SMTPAuthenticationError: (535, b’Error: authentication failed’)
    一些常用邮箱发件服务器及端口号
    邮箱 发件服务器 非SSL协议端口 SSL协议端口
    163 smtp.163.com 25 465
    qq smtp.qq.com 25 465
    xl mail.xl.com 25 465
    这个错误并不是所有邮箱都需要的;如我给公司内部用的邮箱就不需要。
  • python实现发邮件
    1&
### 使用 Python 接收邮件 `SMTP` 协议主要用于发送电子邮件,而接收邮件通常使用 `IMAP` 或者 `POP3` 协议。对于通过 Python 实现邮件的接收功能,并不是由 `smtplib` 完成而是借助于像 `imaplib` 这样的库来处理。 下面是一个简单的例子展示如何利用 `imaplib` 和 `email` 库读取 Gmail 邮箱中的最新一封未读邮件: ```python import imaplib import email from email.header import decode_header def check_email(username, password): mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login(username, password) # 选择邮箱内的文件夹,默认为 "INBOX" mail.select("inbox") status, messages = mail.search(None, 'UNSEEN') if not messages[0]: print("No new emails.") return latest_email_id = messages[0].split()[-1] res, msg = mail.fetch(latest_email_id, "(RFC822)") for response_part in msg: if isinstance(response_part, tuple): msg = email.message_from_bytes(response_part[1]) subject, encoding = decode_header(msg["Subject"])[0] if isinstance(subject, bytes): subject = subject.decode(encoding or 'utf-8') from_ = msg.get("From") print(f"主题: {subject}") print(f"来自: {from_}") if msg.is_multipart(): for part in msg.walk(): content_type = part.get_content_type() content_disposition = str(part.get("Content-Disposition")) try: body = part.get_payload(decode=True).decode() except Exception as e: pass if content_type == "text/plain" and "attachment" not in content_disposition: print(body) else: content_type = msg.get_content_type() try: body = msg.get_payload(decode=True).decode() except AttributeError: pass if content_type == "text/plain": print(body) if __name__ == "__main__": username = "<your-email>@gmail.com" app_password = "<app-specific-password>" check_email(username, app_password) ``` 此脚本会连接到指定账户并打印出最新的未读邮件的主题、发件人以及正文内容[^2]。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值