使用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章