python 中无法使用 smtplib 发送邮件

Unable to send mail using smtplib in python

我运行在UbuntuOS中使用以下代码。当我在独立机器上 运行 这段代码时,我没有收到任何错误。 但是当我连接远程机器和 运行 相同的代码时,我收到以下错误消息。 我也修改了端口值并尝试了多次,但问题没有解决。我想知道我需要检查哪里才能解决问题。

import smtplib
message = ' '
sender  = ' '
receivers = ' '
SUBJECT  = ' '
TEXT     = ' '
sender = 'sender1@abc.com'
receivers = ['rec1@abc.com','rec2@abc.com']

# prepare message
SUBJECT = "Test mail "
TEXT    = """Message line1 \n
             Message line2 \n  """

message  = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

try:
    smtpObj = smtplib.SMTP(<Value>)
    smtpObj.sendmail(sender, receivers, message)
    smtpObj.close()

except smtplib.SMTPConnectError:
       print "Error: unable to send email"

except smtplib.SMTPException:
       print "Error: unable to send email"

错误信息:

Traceback (most recent call last):
  File "sampa.py", line 23, in <module>
    smtpObj = smtplib.SMTP(<Value>)
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused
try:
    smtpObj = smtplib.SMTP(smtp_server, port)
    smtpObj.ehlo()  # Can be omitted
    smtpObj.starttls(context=context)  # Secure the connection
    smtpObj.ehlo()  # Can be omitted
    smtpObj.login(sender, password)
    smtpObj.sendmail(sender, receivers, email_text)