码迷,mamicode.com
首页 > 编程语言 > 详细

python邮件发送代码

时间:2014-08-30 22:59:10      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   代码   log   

1.通过socket

 1 In [1]: import socket
 2 
 3 In [2]: smtp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 4 
 5 In [3]: smtp.connect(("163mx00.mxmail.netease.com", 25))
 6 
 7 In [4]: smtp.send("Hello Mr He.\r\n")
 8 Out[4]: 14
 9 
10 In [5]: smtp.recv(1024)
11 Out[5]: 220 163.com Anti-spam GT for Coremail System (163com[20121016])\r\n502 Error: command not implemented\r\n
12 
13 In [6]: smtp.send("mail from:<xxxxx@qq.com>\r\n")
14 Out[6]: 26
15 
16 In [7]: smtp.recv(1024)
17 Out[7]: ‘‘
18 
19 In [8]: smtp.send("rcpt to:<oooo@163.com>\r\n")
20 Out[8]: 24
21 
22 In [9]: smtp.recv(1024)
23 Out[9]: ‘‘
24 
25 In [10]: smtp.send("What‘s your name?")
26 Out[10]: 17
27 
28 In [11]: smtp.recv(1024)
29 Out[11]: ‘‘

2.通过smtplib模块

 1 import smtplib
 2 import socket
 3 
 4 def sendmail(smtp_username, smtp_passwd,sender,receiver,content):
 5         try:
 6                 server = smtplib.SMTP("smtp.126.com", 25)
 7                 if smtp_username and smtp_passwd:
 8                         server.login(smtp_username, smtp_passwd)
 9                 server.sendmail(sender, receiver, content)
10         except smtplib.SMTPException,e:
11                 err_log.error("Error while sending email summary...")
12                 err_log.error(e)
13         except socket.error,e:
14                 err_log.error("Unable to connect to SMTP server...")
15                 err_log.error(e)
16         else:
17                 server.quit()
18 
19 sendmail("xiaobo05678", "我的密码",xiaobo05678@126.com, "1678216830@qq.com","Hello\r\n\r\n Mr He.\r\n\r\n" )   #注意要有2个\r\n,Hello是标题,Mr He.是内容

 

python邮件发送代码

标签:style   blog   color   io   ar   for   div   代码   log   

原文地址:http://www.cnblogs.com/goodhacker/p/3946978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!