“名称或服务未知”正在发送电子邮件

2024-09-27 00:19:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想向excel表格文件中的电子邮件列表发送电子邮件

我正在使用VisualStudio代码和Linux操作系统

请在下面检查我的代码:

import pandas as pd
import smtplib

SenderAddress = "my@emailaddress"
password = "mypassword"

e = pd.read_excel("email.xlsx")
emails = e['Emails'].values
server = smtplib.SMTP("smtp.gmail.com ", 587)
server.starttls()
server.login(SenderAddress, password)
msg = "Hello this is a email form python"
subject = "Hello world"
body = "Subject: {}\n\n{}".format(subject, msg)
for email in emails:
    server.sendmail(SenderAddress, email, body)
server.quit()

运行代码时,出现以下错误:

line 9, in <module>
    server = smtplib.SMTP("smtp.gmail.com ", 587)
  File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.8/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

[Done] exited with code=1 in 0.683 seconds

Tags: 代码inpyselfhostserverportemail

热门问题