使用Python smtplib发送电子邮件似乎不起作用?

时间:2015-11-06 11:16:43

标签: python email smtp smtplib

我正在尝试使用Python smtplib发送电子邮件。我有一个带有gmail地址的工作设置,但我现在想要使用自定义电子邮件地址(noreply@mydomain.com)。所以我创建了所需的电子邮件地址,并使用以下设置在Thunderbird中对其进行了测试:

Servername: securemail.hostingxs.nl
Port: 578
Username: noreplymydomaincom
Authentication method: Normal password
Connection security: STARTTLS

所以我在命令行中尝试了以下内容:

>>> import smtplib
>>> from email import Encoders
>>> smtpServer = smtplib.SMTP('securemail.hostingxs.nl', 587)
>>> smtpServer.ehlo()
(250, 'securemail.hostingxs.nl Hello ec2-52-22-82-231.eu-central-1.compute.amazonaws.com [52.22.82.231], pleased to meet you\nENHANCEDSTATUSCODES\nPIPELINING\nEXPN\nVERB\n8BITMIME\nSIZE\nDSN\nAUTH LOGIN
PLAIN DIGEST-MD5 CRAM-MD5\nSTARTTLS\nDELIVERBY\nHELP')
>>> smtpServer.starttls()
(220, '2.0.0 Ready to start TLS')
>>> smtpServer.login('noreplymydomaincom', 'mypassword')  # I triple checked the password and copy pasted it between the command line and Thunderbird
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 622, in login
    raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (535, '5.7.0 authentication failed')
>>>

有人知道我在这里可能做错了什么吗?欢迎所有提示!

[编辑]

我尝试使用不同的服务器名称,使用smtp.mydomain.com时我会更进一步,但我无法发送实际的电子邮件:

>>> smtpServer = smtplib.SMTP('smtp.mydomain.com', 587)
>>> smtpServer.ehlo()
(250, 'shared3.hostingxs.nl Hello ec2-52-28-82-231.eu-central-1.compute.amazonaws.com [52.28.82.231],
 pleased to meet you\nENHANCEDSTATUSCODES\nPIPELINING\nEXPN\nVERB\n8BITMIME\nSIZE\nDSN\nAUTH LOGIN PL
AIN\nSTARTTLS\nDELIVERBY\nHELP')
>>> smtpServer.starttls()
(220, '2.0.0 Ready to start TLS')
>>> smtpServer.login('noreplymydomaincom', 'ourpassword')
(235, '2.0.0 OK Authenticated')
>>> smtpServer.ehlo()
(250, 'shared3.hostingxs.nl Hello ec2-52-28-82-231.eu-central-1.compute.amazonaws.com [52.28.82.231],
 pleased to meet you\nENHANCEDSTATUSCODES\nPIPELINING\nEXPN\nVERB\n8BITMIME\nSIZE\nDSN\nAUTH LOGIN PL
AIN\nDELIVERBY\nHELP')
>>> recipients = ['mypersonalemail@gmail.com']
>>> from email.mime.text import MIMEText
>>> from email.mime.multipart import MIMEMultipart
>>> from email.mime.base import MIMEBase
>>> from email import Encoders
>>> msg = MIMEMultipart()
>>> msg['From'] = 'Our company name <noreply@mydomain.com>'
>>> msg['To'] = ', '.join(recipients)
>>> msg['Subject'] = 'TEST FROM THE COMMAND LINE'
>>> msg.attach(MIMEText('this is the body text'))
>>> smtpServer.sendmail('noreplymydomaincom', 'mypersonalemail@gmail.com', msg.as_string())
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 728, in sendmail
    (code, resp) = self.mail(from_addr, esmtp_opts)
  File "/usr/lib/python2.7/smtplib.py", line 481, in mail
    return self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

有人提示吗?我真的被困在这里..

0 个答案:

没有答案