Issue with email send

Nov 21, 2011 at 1:42 PM

Hi, I am tring to write a email class,but when I write it based on tutorial from official, I got error in PTVS, the souce code is as list below:

 

 

#!/usr/bin/env python3
# -*- coding: utf-8 -*- 

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email import utils



class MailSender:
    _smtpSvr="smtp.qq.com"
    _port=25
    _user=''
    _pwd=''
    _from=""
    _to=""
    _subject=""
    html=""
    text=""

    def __init__(self):
        self.smtp = smtplib.SMTP()
        self.smtp.connect(self._smtpSvr,self._port)
        self.smtp.login(self._user,self._pwd)
    
    def setSubject(self,sub):
        self._subject=Header(sub,'utf-8')
         
    def send(self):
        msg = MIMEMultipart('alternative')
        msg['Subject'] = self._subject
        msg['From'] = self._from
        msg['To'] = self._to
        try:
            if self.text!=None:
                part1 = MIMEText(self.text.encode(),'plain',_charset='utf-8')
                msg.attach(part1)
            if self.html!=None:
                part2 = MIMEText(self.html.encode(),'html',_charset='utf-8')
                msg.attach(part2)
            self.smtp.sendmail(self._from,self._to,msg.as_string())
        except Exception as e:
            print(e)
        else:
            print("success")
        self.smtp.quit()


zz=MailSender()
zz.setSubject("zzz")
zz.html="zzz"
zz.send()

I got warning shows:

>	_maxlengths in header line 445	Python
 	feed in header line 422	Python
 	encode in header line 335	Python
 	_write_headers in generator line 179	Python
 	_write in generator line 144	Python
 	flatten in generator line 91	Python
 	as_string in message line 168	Python
 	send in send_email line 44	Python
 	send_email module line 55	Python



builtins.GeneratorExit
Traceback (most recent call last):
  File "C:\Python32\lib\email\header.py", line 445, in _maxlengths
    yield self._maxlen - len(self._current_line)
NoneType

 

If I select "continue",I can still run this program without warning , and email successful. Anyone know why this happen? How can I avoid this? Thanks!

 

BTW:I can run it without problem in CPython..