python无法正常发送邮件

import yagmail# pip3 install yagmail

yag = yagmail.SMTP(user='***@gmail.com', password='******',host='smtp.gmail.com')
body = "邮件正文"

yag.send(to='***@gmail.com',subject= '标题', contents=body)

print("已发送")

因为谷歌邮箱必须使用代理,电脑使用代理软件后,浏览器可以正常登陆邮箱
但是代码不能发送,会报错,全局代理之后也是这样

Traceback (most recent call last):
  File "C:/Users/wohao/OneDrive/INS/ins/tkinter/yagmail_test.py", line 7, in <module>
    yag.send(to='woiwo12138@gmail.com',subject= '11222', contents=body)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\site-packages\yagmail\sender.py", line 162, in send
    self.login()
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\site-packages\yagmail\sender.py", line 261, in login
    self._login(self.credentials)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\site-packages\yagmail\sender.py", line 207, in _login
    self.smtp = self.connection(self.host, self.port, **self.kwargs)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 1031, in __init__
    source_address)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 1037, in _get_socket
    self.source_address)
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\socket.py", line 728, in create_connection
    raise err
  File "C:\Users\wohao\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

尝试使用多种代理软件,都不可行,请问怎么解决呢?请大神指教

最佳答案

已经解决,开启上网软件之后,用python库自动抓取注册表中的ip地址,这样就可以正常使用了。

1年前 评论
shengdi 1个月前
讨论数量: 5

yagmail的这个库不是很了解,你可以借鉴或者采用一下SMTP使用HTTP代理的发信方法:

import smtplib
import socks

#'proxy_port' 代理端口必须使用整数
#'PROXY_TYPE_SOCKS4' 可以替换为HTTP 或 PROXY_TYPE_SOCKS5
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, proxy_host, proxy_port)
socks.wrapmodule(smtplib)

smtp = smtplib.SMTP()
...
1年前 评论

楼上的意会错了,别人说的是谷歌邮箱,无法正常使用代理的问题,怎么回答的都是qq邮箱发送的

1年前 评论

注意:还是得开启*墙软件(敏感词汇)

import smtplib

GMAIL_USERNAME = "LWYCAP@gmail.com"
GMAIL_PASSWORD = "密码"
email_subject = "Hello"
recipient = "1091761664@qq.com"
body_of_email = "hi"

# The below code never changes, though obviously those variables need values.
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

headers = "\r\n".join(["from: " + GMAIL_USERNAME,
                       "subject: " + email_subject,
                       "to: " + recipient,
                       "mime-version: 1.0",
                       "content-type: text/html"])

# body_of_email can be plaintext or html!
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient, content)

QQ邮箱:

file

谷歌邮箱:

file

1年前 评论

已经解决,开启上网软件之后,用python库自动抓取注册表中的ip地址,这样就可以正常使用了。

1年前 评论
shengdi 1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!