python - 由于不支持地址系列错误,无法从 Python 发送电子邮件

标签 python python-3.x email

我在尝试从 python 发送自动电子邮件时遇到错误。

我收到的错误是“[Errno 97]协议(protocol)不支持地址族”

# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

# create message object instance
msg = MIMEMultipart()
password = "password"
msg['From'] = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c6ddc7dff4d3d9d5ddd89ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>"
msg['To'] = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9ded8d4dcf9ded4d8d0d597dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>"
msg['Subject'] = "Photos"

# attach image to message body


server = smtplib.SMTP('smtp.gmail.com: 587')

server.starttls()

# Login Credentials for sending the mail
server.login(msg['From'], password)

server.sendmail(msg['From'], msg['To'], msg.as_string())

如何解决这个问题。如有任何帮助,我们将不胜感激。

最佳答案

我不确定您是否已经解决了这个问题,所以我发布了对我有用的代码。

from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib

msg = MIMEMultipart()
password = "password"
msg['From'] = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99ebf0eaf2d9fef4f8f0f5b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>"
msg['To'] = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0562646860456268646c692b666a68" rel="noreferrer noopener nofollow">[email protected]</a>"
msg['Subject'] = "Photos"

text = "Here are the photos.\nCheers,\nLife is complex"
msg_text = MIMEText(text, 'plain')
msg.attach (msg_text)

fp = open('image.png', 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)

server = smtplib.SMTP('smtp.gmail.com: 587')

server.starttls()

# Login Credentials for sending the mail
server.login(msg['From'], password)

server.sendmail(msg['From'], msg['To'], msg.as_string())

BTW have you enabled the Gmail security setting that allows "less secure application access' to your Gmail account? This setting allows your python script to interact with your account.

Gmail Security Setting

关于python - 由于不支持地址系列错误,无法从 Python 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55760995/

相关文章:

javascript - JQuery对象通过ajax向PHP发送邮件()

python - 获取 python 列表列表中的所有字符串

python - Django 获得独特的多种功能

python - 使用 python 2.7 在 mac os x 10.9 中安装 hg 时出错

python - 在 matplotlib 中缩放轴 3d

python - Tkinter 模块 - 程序将只运行然后停止并且不会打开窗口

email - 发送带附件的电子邮件

python - 如何在 cython 中实现更好的循环速度性能?

python - 我如何使用正则表达式 python 提取引号内的值?

c# - 如何在 asp.net mvc 中发送确认电子邮件