使用python smtp发邮件代码

按照上面的代码执行之后,提示我

Traceback (most recent call last):
  File "/usr/local b/python2.6/threading.py", line 525, in __bootstrap_inner
  File "/home/houjw/monitor/DbMonitorThread.py", line 117, in run
  File "/home/houjw/monitor/Alarm.py", line 15, in report
  File "/home/houjw/monitor/myemail.py", line 23, in send_mail
  File "/usr/local b/python2.6/email/mime/text.py", line 30, in __init__
  File "/usr/local b/python2.6/email/message.py", line 224, in set_payload
  File "/usr/local b/python2.6/email/message.py", line 264, in set_charset
  File "/usr/local b/python2.6/email/encoders.py", line 73, in encode_7or8bit
LookupError: unknown encoding: ascii

在同事的指导下在执行代码前添加
reload(sys)
sys.setdefaultencoding('utf8')
就好了


email.mime.text. MIMEText
class email.mime.text.MIMEText(_text[, _subtype[, _charset]])
Module: email.mime.text

    A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as a parameter to the MIMENonMultipart constructor; it defaults to us-ascii. If _text is unicode, it is encoded using the output_charset of _charset, otherwise it is used as-is

推荐使用下面这种
msg = email.MIMEText.MIMEText(info, _subtype="html", _charset="utf-8")
http://suzhouclark.itpub.net/post/7184/484998


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值