python使用代理进行邮件发送

本文介绍如何使用SMTP类连接代理服务器,并通过实例演示如何登录邮箱并发送邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 import sys,os
 2 import smtplib, httplib, socket
 3 
 4  class SMTP(smtplib.SMTP, httplib.HTTPConnection):
 5     def connect(self, host='localhost', port = 0, proxy=None):
 6         if proxy is None:
 7             return smtplib.SMTP.connect(self, host, port)
 8         else:
 9             if not port and (host.find(':') == host.rfind(':')):
10                 i = host.rfind(':')
11                 if i >= 0:
12                     host, port = host[:i], host[i+1:]
13                     try: port = int(port)
14                     except ValueError:
15                         raise socket.error, "nonnumeric port"
16             if not port:
17                 port = self.default_port
18 
19             if self.debuglevel > 0:
20             print >>sys.stderr, 'connect:', (host, port)
21 
22             proxy_host, proxy_port = proxy
23             conn = httplib.HTTPConnection(proxy_host, proxy_port)
24             conn.connect()
25             conn.send("CONNECT %s:%d HTTP/1.0\r\n\r\n" % (host, port))
26             response = conn.response_class(conn.sock, strict=conn.strict,
27 method=conn._method)
28             (version, code, message) = response._read_status()
29             if code != 200:
30                 conn.close()
31                 raise socket.error, "Proxy connection failed: %d %s" %
32 (code, message.strip())
33             while True:
34                 line = response.fp.readline()
35                 if line == '\r\n':
36                     break
37             self.sock = conn.sock
38             (code, msg) = self.getreply()
39             if self.debuglevel > 0:
40                 print >>sys.stderr, "connect:", msg
41             return (code, msg)
42 
43  if __name__ == '__main__':
44     smtp = SMTP()
45     smtp.debuglevel = 1
46     smtp.connect('smtp.qq.com', 25, proxy=('127.0.0.1', 8123))
47     smtp.login('1234...@qq.com', '12345678')
48     smtp.close()

 

转载于:https://www.cnblogs.com/ontseason/articles/4220516.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值