打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python3.3 邮件发送 含附件(各种类型文件)

1、廖雪峰教程代码会出现此种错误:
smtplib.SMTPServerDisconnected: please run connect() first,弃用

2、TypeError: getsockaddrarg: AF_INET6 address must be tuple, not str
服务器地址类型设定为元组
3、UnicodeEncodeError: 'utf-8’ codec can’t encode character '\udcc9’ in position 0: surrogates not allowed
有些采用读取附件内容然后再发送附件,读取附件中中文内容错误,utf-8 和 gbk均无效


可用程序代码:

#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibimport email.mime.multipartimport email.mime.textfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationdef 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:/软件/yasuo.rar文件    part = MIMEApplication(open('D:/软件/yasuo.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('xxsmtp.xxx.com.cn', 'fish@xxx.com.cn', '123456', 'fish@xxx.com.cn', subject, content)except Exception as err:    print(err)
  • //////////////////////////////

  • #!/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:/软件/yasuo.rar文件

  •     part = MIMEApplication(open('D:/1.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', 'hd***004@163.com', 'z***1115', 'hdzgx2004@sohu.com', subject, content)

  • except Exception as err:

  •     print(err)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python3 SMTP发送邮件 | 菜鸟教程
python笔记3
从零开始学Python(九):搭建一个基于SMTP的简单邮件预警系统
python发送邮件
python使用smtplib和MIMEText发送邮件
【Python】Python使用smtplib发送简单文本邮件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服