简体   繁体   English

没有使用smtplib发送电子邮件-python

[英]No email sending using smtplib - python

import smtplib

sender = 'den.callanan@gmail.com'
receiver = ['callanden@gmail.com']

message = """From: From Person <den.callanan@gmail.com>
To: To Person <callanden@gmail.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:

    print("trying host and port...")

    smtpObj = smtplib.SMTP('smtp.gmail.com', 465)

    print("sending mail...")

    smtpObj.sendmail(sender, receiver, message)

    print("Succesfully sent email")

except SMTPException:

    print("Error: unable to send email")

I've created two new email accounts (above), both on the same server (gmail) to test this. 我已经在同一个服务器(gmail)上创建了两个新的电子邮件帐户(上述),以对其进行测试。 It reaches the point in which it prints "trying host and port..." and does not go any further. 它到达打印“正在尝试主机和端口...”的地步,并且不再进行任何操作。 So the problem should be with the address and port number I entered. 所以问题应该出在我输入的地址和端口号上。 But according to gmail's outgoing mail server details i've inputted them correctly. 但是根据gmail的传出邮件服务器详细信息,我已经正确输入了它们。 Any ideas what's wrong? 任何想法有什么问题吗?

If I remove the port number or try a different port number such as 587 i'm provided with an error. 如果我删除端口号或尝试使用其他端口号(例如587),则会出现错误。

Sending email via Gmail's SMTP servers requires TLS and authentication. 通过Gmail的SMTP服务器发送电子邮件需要TLS和身份验证。 To authenticate, you will need to make an application-specific password for your account . 要进行身份验证,您需要为您的帐户输入应用专用密码

This script worked for me (though I used my own GMail email address and my own application-specific password). 这个脚本对我有用(尽管我使用了自己的GMail电子邮件地址和自己的应用程序专用密码)。 In the code below, replace APPLICATION_SPECIFIC_PASSWORD with the password you generated. 在下面的代码中,用您生成的密码替换APPLICATION_SPECIFIC_PASSWORD。

import smtplib

sender = 'den.callanan@gmail.com'
receiver = ['callanden@gmail.com']

message = """From: From Person <den.callanan@gmail.com>
To: To Person <callanden@gmail.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
    print("trying host and port...")

    smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    smtpObj.login("den.callanan@gmail.com", "APPLICATION_SPECIFIC_PASSWORD")

    print("sending mail...")

    smtpObj.sendmail(sender, receiver, message)

    print("Succesfully sent email")

except smtplib.SMTPException:
    print("Error: unable to send email")
    import traceback
    traceback.print_exc()

(To debug the problem, I added the print-traceback code in the except statement. The exceptions had specific information on how to get it to work. The code hung when accessing port 465, I think because of problems with TLS negotiation, so I had to try using port 587; then I got good debugging info that explained what to do.) (为调试该问题,我在except语句中添加了print-traceback代码。该异常包含有关如何使其工作的特定信息。该代码在访问端口465时挂起,我认为是因为TLS协商存在问题,所以我必须尝试使用​​端口587;然后我获得了很好的调试信息,该信息说明了如何操作。)

You can see info on the SMTP_SSL object here. 您可以在此处查看有关SMTP_SSL对象的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM