试图发送电子邮件通过python与smtp协议没有成功

问题描述:

我知道我有内置的lib“smtplib”在Python中,但我想学习如何做到这一点没有它, 在阅读有关此协议后,我试图喜欢一个代码示例显示如何通过python做到这一点,但我找不到任何东西,所以我自己创建了一个,但没有成功。 这是我的代码:试图发送电子邮件通过python与smtp协议没有成功

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: [email protected]") 
print "I just announced google!" 

print "Announcing google about the receiver (us...)..." 
soc.send("RCPT To: [email protected]") 
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: [email protected]\nTo: [email protected]\nSubject: HEY\n\nBYE\n.") 
soc.send("QUIT") 

soc.close() 

与该代码,我使用Wireshark是嗤之以鼻: The sniffed information

我会很乐意从你们那里听到一些帮助。 (另外,我不知道......我不应该犯一些认证,使谷歌确保它是真的我messege发送者?)

尝试使用Python的内置模块,SMPT Python SMPT Lib 并请参见本教程的详细信息关于Sending Emails with Python

注意:如果你有2FA,你需要在做任何事情之前禁用它!

的Python 3 示例代码:

import smtplib 

    uname = "[email protected]" 
    pword = "your_gmail_account_password" 

    from = "[email protected]" 
    to = "[email protected]" 
    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() 
+0

我知道我使用这个库。但我写明确我想这样做,没有它 –

+0

然后使用电子邮件服务试试吧!内置的mail()函数不能通过python等邮件发送邮件!您需要使用电子邮件服务或SMPT! – gemgr

+0

更喜欢smtp .. tnx –