Python使用smtplib发送邮件
2014-08-29 17:34
836 查看
http://www.w3cschool.cc/python/python-email.htmlfrom
<pre name="code" class="python"># -*- coding: gb2312 -*- import smtplib from email.mime.text import MIMEText mailto_list=["284208276@qq.com"] mail_host="smtp.126.com" #设置服务器 mail_user="werm520" #用户名 mail_pass="******" #口令 mail_postfix="126.com" #发件箱的后缀 def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容 me="hello"+"<"+mail_user+"@"+mail_postfix+">" #这里的hello可以任意设置,收到信后,将按照设置显示 msg = MIMEText(content,_subtype='html',_charset='gb2312') #创建一个实例,这里设置为html格式邮件 msg['Subject'] = sub #设置主题 msg['From'] = me msg['To'] = ";".join(to_list) try: s = smtplib.SMTP() s.connect(mail_host) #连接smtp服务器 s.login(mail_user,mail_pass) #登陆服务器 s.sendmail(me, to_list, msg.as_string()) #发送邮件 s.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': if send_mail(mailto_list,"hello","<a href='http://http://blog.csdn.net/werm520'>测试文件</a>"): print "发送成功" else: print "发送失败"
或者你也可以在消息体中指定Content-type为text/html,如下实例:
# -*- coding: cp936 -*- import smtplib from email.mime.text import MIMEText print "debug" mailto_list=["284208276@qq.com"] mail_host="smtp.126.com" #设置服务器 mail_user="werm520" #用户名 mail_pass="----" #口令 mail_postfix="126.com" #发件箱的后缀 print "here" message = """From: From Person <from@fromdomain.com> To: To Person <to@todomain.com> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b> <h1>This is headline.</h1> """ print "try" try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(mail_user+'@'+mail_postfix, mailto_list, message) print "Successfully sent email" except Exception,e: print str(e) print "Error: unable to send email"
发送带附件的邮件:
# -*- coding: gb2312 -*- import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart mailto_list=["284208276@qq.com"] mail_host="smtp.126.com" #设置服务器 mail_user="werm520" #用户名 mail_pass="-----" #口令 mail_postfix="126.com" #发件箱的后缀 def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容 me="hello"+"<"+mail_user+"@"+mail_postfix+">" #这里的hello可以任意设置,收到信后,将按照设置显示 msg = MIMEMultipart() #创建一个带附件的实例 msg['Subject'] = sub #设置主题 msg['From'] = me msg['To'] = ";".join(to_list) att1 = MIMEText(open('f:\\激活码\\jihuoma.txt','rb').read(),'base64','gb2312') # att1["Content-Type"] = 'attachment; filename="test.txt"'<span style="white-space:pre"> </span># 这种重命名附件名称的方式不知道为什么不成功 att1.add_header('Content-Disposition','attachment', filename = '1.test') msg.attach(att1) body = MIMEText(content,_subtype='html',_charset='gb2312')<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">#创建一个实例,这里设置为html格式邮件</span><span style="white-space:pre"> </span> msg.attach(body) try: s = smtplib.SMTP() s.connect(mail_host) #连接smtp服务器 s.login(mail_user,mail_pass) #登陆服务器 s.sendmail(me, to_list, msg.as_string()) #发送邮件 s.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': if send_mail(mailto_list,"hello","<a href='http://http://blog.csdn.net/werm520'>测试文件</a>"): print "发送成功" else: print "发送失败"
更加详细的BLOG:
Python SMTP 发送带附件电子邮件
/article/8418960.html
相关文章推荐
- Python 3(13)使用 smtplib 模块发送 SMTP 邮件
- 用python的smtplib发送邮件python2,3都可以使用
- python - 如何通过GMail发送邮件:smtplib的使用
- python使用smtplib模块通过gmail实现邮件发送的方法
- 使用python smtplib 发送带附件的邮件
- Python3使用smtplib发送邮件
- python smtplib 使用代理发送邮件
- python - 如何通过GMail发送邮件:smtplib的使用
- python3使用smtplib发送邮件
- Python smtplib使用实例:网易邮箱向QQ邮箱发送邮件
- python使用smtplib模块通过gmail实现邮件发送的方法
- python3使用smtplib发送邮件
- python模块学习 ---- smtplib 邮件发送
- 用python的smtplib发送使用dkim签名的电子邮件
- python 发送邮件错误解决:smtplib.SMTPException SMTP AUTH extension
- python模块之smtplib: 用python发送SSL/TLS安全邮件
- python模块学习 ---- smtplib 邮件发送
- python模块学习 ---- smtplib 邮件发送(转)
- python发送邮件sendmail--smtplib【带附件】
- python模块学习 ---- smtplib 邮件发送