首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python发邮件报错Cannot assign requested address?

python发邮件报错Cannot assign requested address?

提问于 2020-04-21 08:40:17
回答 2关注 0查看 1.4K

在腾讯云服务器利用python连接gmail发送邮件,为什么一直显示如下错误,之前还能发送的,本地也可以发。也根据网上说的设置了 net.ipv4.ip_nonlocal_bind = 1 还是有问题。有没有大佬解答下。

代码语言:js
AI代码解释
复制
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

回答 2

FesonX

精选回答
回答已采纳

修改于 2020-04-23 13:13:10

------2020-04-23

你的原代码没有传入邮件服务器端口,而是直接传入一个写好的url导致找不到地址,传入端口就可以了,另外记得打开调试模式啊,这样容易判断问题

smtp = smtplib.SMTP_SSL(self.smtp_server, self.port)

# 开启 Debug

smtp.set_debuglevel(True)

代码语言:python
运行
AI代码解释
复制
# 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

配置贴一下,之前遇到类似的问题是邮件服务器端口错误导致

广埠屯第一暖男

提问者

发布于 2020-04-23 02:23:57

邮件发送的类

代码语言:python
运行
AI代码解释
复制
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("邮件发送成功")
和开发者交流更多问题细节吧,去 写回答
相关文章
TIME_WAIT引起Cannot assign requested address报错
有时候用redis客户端(php或者java客户端)连接Redis服务器,报错:“Cannot assign requested address。”
黄规速
2022/04/14
2K0
异常解决——Tomcat启动异常:Cannot assign requested address
tomcat启动的时候报错,提示无法使用8005端口,因为使用的默认端口,tomcat的8005端口是用来停止服务的。
执笔记忆的空白
2022/01/05
1.7K0
这个 TCP 问题你得懂:Cannot assign requested address
原文链接: 这个 TCP 问题你得懂:Cannot assign requested address
AlwaysBeta
2021/09/07
5.1K3
BindException: Cannot assign requested address (Bind failed)解决办法
百度了BindException: Cannot assign requested address (Bind failed),网友们提供的方法不能解决我的遇到的问题。 最后意外发现时我的配置文件zoo.cfg配置错误:
程裕强
2022/05/06
1.6K0
tomcat启动报java.net.BindException: Cannot assign requested address
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
奋飛
2019/08/15
2.7K0
k8s上部署zookeeper集群报错java.net.BindException: Cannot assign requested address (Bind failed)
1.配置3个POD部署zookeeper节点, 2.分别使用clusterIP暴露2888,3888端口 3.配置环境变量 ZOO_SERVERS=“server.1=zookeeper-cluster-1.test.svc.cluster.local:2888:3888;2181 server.2=zookeeper-cluster-2.test.svc.cluster.local:2888:3888;2181 server.3=zookeeper-cluster-3.test.svc.cluster.local:2888:3888;2181” MY_ZOO_ID=1
路过君
2021/12/07
1.6K0
socket服务部署到服务端后启动失败Cannot assign requested address: bind 的总结
https://blog.csdn.net/asd1098626303/article/details/79141315
算法与编程之美
2019/07/17
10.9K0
socket服务部署到服务端后启动失败Cannot assign requested address: bind 的总结
React报错之Cannot assign to 'current' because a read-only property
原文链接:https://bobbyhadz.com/blog/react-cannot-assign-to-current-because-read-only-property[1]
chuckQu
2022/08/19
1.1K0
React报错之Cannot assign to 'current' because a read-only property
Python报错:OSError: cannot open resource
今天借助Python第三方库写了一个简单的生成词云的编程,但在使用wordcloud生成词云过程中,出现了OSError: cannot open resource错误,通过断点调试并查看了一些网上的解决方法 找到了原因:字体属性font_path的设置与系统提供的字体不一致。 在本地电脑没有所写的字体,或是字体名称后缀不一致,因此只需查看本地是否有对应的字体,将其改为本地对应文件夹下已有的字体文件即可。
全栈程序员站长
2022/10/03
1.8K0
Python报错:OSError: cannot open resource
Glassfish4.1安装及配置[通俗易懂]
上述下载的GlassFish为开源版本,文档见GlassFish Server Documentation。
全栈程序员站长
2022/09/23
1.6K0
Glassfish4.1安装及配置[通俗易懂]
Consul 的部署与使用
Consul是一种网络工具,可提供功能齐全的服务网格和服务发现。在本地尝试领事。这句话引用与官网
java攻城狮
2021/01/18
1.6K0
Uncaught TypeError: Cannot assign to read only property 'exports' of object
查了资料 “在webpack打包的时候,可以在js文件中混用require和export。但是不能混用import 以及module.exports ” 但是项目并没有混用 import 与 module.exports
切图仔
2022/09/08
8840
Zabbix技术问答特辑-25期
zabbix server服务器的agent监控报错:get value from agent faild:bind() faild:[99] cannot assign requested address。数据一直在正常采集,zabbix_get 能获取到数据,页面测试也能获取到数据。
Zabbix
2021/11/02
1.1K1
IDEA 报错Disconnected from the target VM, address:
今天写了一个接口,运行Idea报错Disconnected from the target VM, address: '127.0.0.1:59995', transport: 'sock)
赵哥窟
2020/05/21
5.1K1
公网环境数据上报sdk接口报错率达到40%
2.通过nginx日志可以看到大量Cannot assign requested address 错误,同时被压测机网络连接TIME_WAIT数量特别大
林洽榆-悦智
2022/03/10
4610
Python 发邮件
普通邮件 [root@localhost checksalt]# cat python_email.py  #!/usr/bin/python # -*- coding: utf-8 -*- import sys def smtp(title,file):     import smtplib     from email.mime.text import MIMEText     from email.header import Header           with open(file, 'r') 
py3study
2020/01/15
1.8K0
Python发邮件
代码 1、mail.py #-*-coding:utf-8-*- #!/bin/pyton import sys import smtplib import logging from email.mime.text import MIMEText def send_mail(to_list, cc_list, html, sub): me = mail_user msg = MIMEText(html, _subtype='html', _charset='utf-8') # 格式化
week
2020/05/12
1.9K0
docker push报错:denied: requested access to the resource is denied
当把镜像推送到自己的docker.io是报错如下: denied: requested access to the resource is denied 解决方法: docker login 登录后在推。
院长技术
2021/02/19
1.1K0
zabbix报错The server requested authentication method unknown to the client
The server requested authentication method unknown to the client
大大刺猬
2023/07/27
8800
zabbix报错The server requested authentication method unknown to the client
使用python发邮件
# -*- coding: UTF-8 -*- import smtplib import traceback from email.mime.text import MIMEText from email.utils import formataddr class SendMail(): def mail(self,subject,text): #self.my_sender='****@163.com' # 发件人邮箱账号 #self.my_pass
JQ实验室
2022/01/11
1.2K0

相似问题

小程序报错Cannot read property 'skey' of null?

22K

云函数报错Runtime.ImportModuleError:Cannot find module?

0267

API网关签权问题,报错“HMAC signature cannot be verifie”?

24.2K

centos8 运行yum update -y 报错Cannot find a .....?

1510

实时音视频呼叫的时候,报错Cannot read properties of undefined?

1502
相关问答用户
自由工作者
CVM专项擅长1个领域
腾讯科技(深圳)有限公司 | 高级工程师
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档