首先,我(无论如何)都是Python的新手,并且从程序员的角度浏览电子邮件的复杂性。因此,如果我省略任何重要信息或滥用任何术语,请不要犹豫让我澄清一下!
我最近学习了如何通过Python 3.7(在Windows 10操作系统上)从家庭Internet网络中毫无问题地生成和发送电子邮件。但是,当我在公司的Internet网络上时,无法正确连接Python以生成和发送电子邮件。我将在下面显示我尝试过的两种不同方法(均来自先前的论坛主题),以及后续的错误消息:
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("aaron.dadas.85", "mypassword")
server.sendmail(
"aaron.dadas.85@gmail.com",
"aaron.dadas.85@gmail.com",
"this message is from python")
server.quit()
Traceback (most recent call last):
File "C:\Users\320045759\Desktop\emailtest.py", line 3, in <module>
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 1031, in __init__
source_address)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 1037, in _get_socket
self.source_address)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
这是第二次尝试,使用我公司的Internet代理:
import smtplib
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, 'http://user:password@199.168.151.10:80', 8080)
socks.wrapmodule(smtplib)
smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 1
smtpuser = 'aaron.dadas.85'
smtppass = 'mypassword'
RECIPIENTS = 'aaron.dadas.85@gmail.com'
SENDER = 'aaron.dadas.85@gmail.com'
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()
Traceback (most recent call last):
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Scripts\socks.py", line 823, in connect
super(socksocket, self).connect(proxy_addr)
socket.gaierror: [Errno 11003] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\320045759\Desktop\emailtest.py", line 18, in <module>
server = smtplib.SMTP(smtpserver,587)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Lib\socket.py", line 716, in create_connection
sock.connect(sa)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Scripts\socks.py", line 94, in wrapper
return function(*args, **kwargs)
File "C:\Users\320045759\Desktop\Softwares\Python\Python37\Scripts\socks.py", line 836, in connect
raise ProxyConnectionError(msg, error)
socks.ProxyConnectionError: Error connecting to SOCKS5 proxy http://user:password@199.168.151.10:80:8080: [Errno 11003] getaddrinfo failed
我曾尝试导航其他线程以寻求解决方案,但是其中许多都由用户禁用防火墙来解决(通常在用户收到“ [WinError 10061]无法连接,因为目标计算机处于活动状态时无法建立连接的情况下)拒绝”错误-在这种情况下,我也多次遇到此错误)。我不能禁用防火墙。我无权更改公司设置,因此必须绕过公司防火墙。
说实话,这可能根本不是问题。我觉得好像只是在处理应该如何使用网络的代理地址一样。一个重要的提示是,当我从命令行使用 python -m pip install [package] 时,它每次都会失败。但是,当我使用 python -m pip --proxy http://user:password@199.168.151.10:80安装[package] 时,它可以工作。如果我正确地理解了这一点,则意味着可以使用一种代理地址来访问在线资源...在生成/发送电子邮件的情况下,我只是没有正确地使用代理地址。
我希望此介绍内容能为有人提供足够的帮助!我将积极监视答复,以在需要时提供更多信息。谢谢!
编辑01/07/2019:
使用以下脚本,我可以在公司Internet上提取HTML代码(使用代理设置):
import requests
proxies = {'http': 'http://user:password@199.168.151.10:80','https': 'http://user:password@199.168.151.10:80'}
r = requests.get('https://www.google.com/', proxies=proxies)
print(r.text)
[输出的是Google主页HTML代码]
这是朝正确方向迈出的一步,因为它表明了通过此代理地址成功连接到Internet。但是我仍然无法解决这个难题:在我之前的尝试中,什么缺失或语法上不正确?我是否不正确地声明了代理地址?