Python发送电子邮件范例

jopen 12年前

利用python可以很方便地发送电子邮件。以下是范例:

    #coding=utf-8        import smtplib        from email.mime.text import MIMEText        def main():            MAIL_FROM = 'sdsd@163.com'            MAIL_TO = ['sdsd@qq.com']            msg = MIMEText('我是内容',_charset='UTF-8')            msg['Subject'] = 'python测试!'.decode("utf-8")            msg['From'] = MAIL_FROM                    try:                smtp = smtplib.SMTP()                smtp.connect('smtp.163.com')                smtp.login('sdsd', '****')#用户名和密码                smtp.sendmail(MAIL_FROM, MAIL_TO, msg.as_string())                print 'email sent.suss'                smtp.close()            except Exception, e:                print e                if __name__ == '__main__':            main()  

文件本身的编码要是utf-8的.

本人测试通过.环境是:py2.6  windows xp