Python 在代理服务器后面发送电子邮件

问题描述:

我想通过 hotmail smtp 使用 python 脚本发送电子邮件,但我已连接到代理服务器.

I want to send an email using python script via hotmail smtp but I'm connected to a proxy server.

有我的代码,当它直接连接到互联网时它可以工作,但是当它连接到代理服务器时他不起作用.

There is my code , it works when it's connected directely to internet but wen it's connected to a proxy server he doesn't work.

import smtplib

smtpserver = 'smtp.live.com'
AUTHREQUIRED = 1 
smtpuser = 'example@hotmail.fr'  
smtppass = 'mypassword'  

RECIPIENTS = 'mailto@gmail.com'
SENDER = 'example@hotmail.fr'
mssg = "test message"
s = mssg   

server = smtplib.SMTP(smtpserver,587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(smtpuser,smtppass)
server.set_debuglevel(1)
server.sendmail(SENDER, [RECIPIENTS], s)
server.quit()

您可以使用名为 SocksiPyPySocks,目前维护的分支:

You can accomplish this with a module called SocksiPy or PySocks, the currently maintained fork:

import smtplib
import socks

#socks.setdefaultproxy(TYPE, ADDR, PORT)
socks.setdefaultproxy(socks.SOCKS5, 'proxy.proxy.com', 8080)
socks.wrapmodule(smtplib)

smtpserver = 'smtp.live.com'
AUTHREQUIRED = 1 
smtpuser = 'example@hotmail.fr'  
smtppass = 'mypassword'  

RECIPIENTS = 'mailto@gmail.com'
SENDER = 'example@hotmail.fr'
mssg = "test message"
s = mssg   

server = smtplib.SMTP(smtpserver,587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(smtpuser,smtppass)
server.set_debuglevel(1)
server.sendmail(SENDER, [RECIPIENTS], s)
server.quit()