尝试使用smtp协议通过python发送电子邮件但没有成功

时间:2017-09-15 10:43:48

标签: python email smtp

我知道我有内置的lib" smtplib"在python中,但我想学习没有它的方法, 在阅读了这个协议之后,我试着喜欢一个代码示例,展示了如何通过python实现这一点,但我找不到任何东西,所以我自己构建了一个,但没有成功。 这是我的代码:

import socket

soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print "Connecting to smtp server of google by SSL..."
soc.connect(("smtp.gmail.com", 465))
print "Connected to smtp server of google by SSL!"

print "Sending helo to our friend..."
soc.send("HELO FromX")
print "'helo' went good!"

print "Announcing google about the Sender (us...)..."
soc.send("mail From: xxx@google.com")
print "I just announced google!"

print "Announcing google about the receiver (us...)..."
soc.send("RCPT To: xxx@gmail.com")
print "I just announced google!"

print "Ask google for sending him some data..."
soc.send("DATA")
print "Google accepted us... of course he is, we are handsome!"

print "Now the all other stuff..."
soc.send("From: xxx@gmail.com\nTo: xxx@gmail.com\nSubject: HEY\n\nBYE\n.")
soc.send("QUIT")

soc.close()

使用该代码,我得到了Wireshark的嗅觉: The sniffed information

我很乐意听到你们的帮助。 (另外,我想知道......我不应该提交一些身份验证来让谷歌确定它真的是我的消息发送者吗?)

1 个答案:

答案 0 :(得分:0)

尝试使用Python的内置模块进行SMPT Python SMPT Lib 有关Sending Emails with Python

的详细信息,请参阅本教程

注意:如果您有2FA,则需要在执行任何操作前将其禁用!

Python 3 示例代码:

    import smtplib

    uname = "you@gmail.com"
    pword = "your_gmail_account_password"

    from = "you@gmail.com"
    to = "to@example.com"
    msg = "Hi There!"

    s = smtplib.SMTP('smtp.gmail.com:587')
    s.ehlo()
    s.starttls()
    s.login(uname,pword)

    s.sendmail(from,to,msg)
    print("Mail Send Successfully")
    s.quit()