Python SMTP socket.gaierror:[Errorno -2]找不到名称或服务

时间:2015-06-25 06:01:43

标签: python sockets email

我使用SMTP连接来连接和发送电子邮件,但我收到SMTP socket.gaierror:[Errorno -2]找不到名称或服务。这是我的代码

from smtplib import SMTP
import datetime

debuglevel = 0

smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('MY.MAIL.SERVER', 587)
smtp.login('USERNAME@DOMAIN', 'PASSWORD')

from_addr = "ann@abcsystems.com"
to_addr = "minna@abcsystems.com"

subj = "hello"

message_text = "Hello\nThis is a test mail from server\n\n"

msg = "From: %s\nTo: %s\nSubject: %s\n\n%s" 
        % ( from_addr, to_addr, subj, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

1 个答案:

答案 0 :(得分:0)

试试这个,

import socket
s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)
s.connect(('MY.MAIL.SERVER', 587))

如果再次抛出gai error,请尝试从本地计算机上ping MY.MAIL.SERVER