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