使用python发送简单的邮件

类别:软件工程 点击:0 评论:0 推荐:

前些时间,论坛上有人讨论怎么用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", "[email protected]", "[email protected]", "xxxx", "xxxxxxxxx")
    test.send("heheh\r\na test from python send mail")
        

本文地址:http://com.8s8s.com/it/it35264.htm