前端

img zhaoweikid

使用python发送简单的邮件

发表于2004/10/6 10:38:00  1711人阅读

分类: Python网络编程

前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeBSD在telnet到163的的smtp服务器,分析了一下,就用python写个了发送邮件的程序,感觉有点粗糙,但还算能工作。
import smtplib, base64

class SimpleSendMail:
    def __init__(self, smtp_server, from_addr, to_addr, user, passwd):
        self.from_addr = from_addr
        self.to_addr  = to_addr
        self.username = base64.encodestring(user)
        self.password = base64.encodestring(passwd)
        self.mailserver = smtp_server
   
   
    def send(self, msg):
        server = smtplib.SMTP(self.mailserver)
        server.set_debuglevel(1)
        server.docmd("EHLO server")
        server.docmd("AUTH LOGIN")
        server.send(self.username)
        server.getreply()
        server.send(self.password)
        server.getreply()
        server.docmd("MAIL FROM:" + self.from_addr)
        server.docmd("RCPT TO:" + self.to_addr)
        server.docmd("DATA")
        server.send(msg)
        server.send("/r/n./r/n")
        server.getreply()
        #server.reset()
        server.quit()
       
if __name__ == '__main__':
    test = SimpleSendMail("smtp.163.com", "xxxx@163.com", "xxxx@sina.com", "xxxx", "xxxxxxxxx")
    test.send("heheh/r/na test from python send mail")
        

阅读全文
0 0

相关文章推荐

img
取 消
img