首页 > 代码库 > python邮件发送代码

python邮件发送代码

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\n12 13 In [6]: smtp.send("mail from:<xxxxx@qq.com>\r\n")14 Out[6]: 2615 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]: 2421 22 In [9]: smtp.recv(1024)23 Out[9]: ‘‘24 25 In [10]: smtp.send("What‘s your name?")26 Out[10]: 1727 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邮件发送代码