在腾讯云服务器利用python连接gmail发送邮件,为什么一直显示如下错误,之前还能发送的,本地也可以发。也根据网上说的设置了 net.ipv4.ip_nonlocal_bind = 1 还是有问题。有没有大佬解答下。
Traceback (most recent call last):
File "GetConvertibleBondInfo.py", line 185, in <module>
GetConvertibleBondInfo().start()
File "GetConvertibleBondInfo.py", line 177, in start
files=None
File "/home/FuckingShares/GetDailyConvertibleBondInfo/utils/SendEMail.py", line 59, in send_mail
smtp = smtplib.SMTP(self.smtp_server)
File "/home/software/anaconda3/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/home/software/anaconda3/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/home/software/anaconda3/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/home/software/anaconda3/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/home/software/anaconda3/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
------2020-04-23
你的原代码没有传入邮件服务器端口,而是直接传入一个写好的url导致找不到地址,传入端口就可以了,另外记得打开调试模式啊,这样容易判断问题
smtp = smtplib.SMTP_SSL(self.smtp_server, self.port)
# 开启 Debug
smtp.set_debuglevel(True)
# coding=utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtpd import COMMASPACE
class SendEmailByGoogleMail:
def __init__(self, subject, username, password, receivers:list):
# 初始化账号信息
self.user_account = {'username': username, 'password': password}
# 初始化邮件主题
self.subject = subject
# 设置邮箱服务器地址
self.smtp_server = 'smtp.gmail.com'
# TLS 容易挂,用SSL端口
self.port = 465
# 初始化发件人姓名
self.sender = ''
# 初始化收件人邮箱
self.receivers = receivers
def send_mail(self, way, content, files):
msg_root = MIMEMultipart()
# 构造附件列表
if files is not None:
for file in files:
file_name = file.split("/")[-1]
att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att["Content-Disposition"] = 'attachment; filename=%s' % file_name
msg_root.attach(att)
# 邮件主题
msg_root['Subject'] = self.subject
# 接收者的昵称,其实这里也可以随便设置,不一定要是邮箱
msg_root['To'] = ';'.join(self.receivers)
# 邮件正文
if way == 'common':
msg_root.attach(MIMEText(content, 'plain', 'utf-8'))
elif way == 'html':
msg_root.attach(MIMEText(content, 'html', 'utf-8'))
smtp = smtplib.SMTP_SSL(self.smtp_server, self.port)
# 开启 Debug
smtp.set_debuglevel(True)
smtp.ehlo()
# smtp.starttls()
smtp.login(self.user_account['username'], self.user_account['password'], initial_response_ok=False)
smtp.auth_plain()
smtp.sendmail(self.sender, self.receivers, msg_root.as_string())
print("邮件发送成功")
if __name__ == '__main__':
sebg = SendEmailByGoogleMail('Hello', username='***', password='***',
receivers=['***'])
sebg.send_mail('common', content='hello world', files=None)
------2020-04-22
配置贴一下,之前遇到类似的问题是邮件服务器端口错误导致
邮件发送的类
class SendEmailByGoogleMail:
def __init__(self, subject, username, password, receivers):
# 初始化账号信息
self.user_account = {'username': username, 'password': password}
# 初始化邮件主题
self.subject = subject
# 设置邮箱服务器地址
self.smtp_server = 'smtp.gmail.com:587'
# 初始化发件人姓名
self.sender = ''
# 初始化收件人邮箱
self.receivers = receivers
def send_mail(self, way, content, files):
msg_root = MIMEMultipart()
# 构造附件列表
if files is not None:
for file in files:
file_name = file.split("/")[-1]
att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att["Content-Disposition"] = 'attachment; filename=%s' % file_name
msg_root.attach(att)
# 邮件主题
msg_root['Subject'] = self.subject
# 接收者的昵称,其实这里也可以随便设置,不一定要是邮箱
msg_root['To'] = COMMASPACE.join(self.receivers)
# 邮件正文
if way == 'common':
msg_root.attach(MIMEText(content, 'plain', 'utf-8'))
elif way == 'html':
msg_root.attach(MIMEText(content, 'html', 'utf-8'))
smtp = smtplib.SMTP(self.smtp_server)
smtp.ehlo()
smtp.starttls()
smtp.login(self.user_account['username'], self.user_account['password'])
smtp.sendmail(self.sender, self.receivers, msg_root.as_string())
print("邮件发送成功")
相似问题