我試圖使用python將文件附加到郵件。 代碼:socket.gaierror:[Errno 11001] getaddrinfo失敗
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from smtplib import SMTPException
def send_Email():
file1="abc.txt"
message = "Test mail"
msg = MIMEMultipart()
msg.attach(MIMEText(file(file1).read()))
try:
smtpObj = smtplib.SMTP('smtp server name',port)
smtpObj.sendmail(sender, EmailId, message, msg.as_string())
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
的Bt我得到的錯誤:socket.gaierror:[錯誤11001]的getaddrinfo失敗
完整的錯誤信息:
File "C:\Python27\lib\smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
看起來像socket lib無法解析SMTP服務器名稱。 – Denis
此SMTP服務器是否有A記錄 –