2017-09-15 40 views
0

我知道我有內置的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發送者?)

回答

0

嘗試使用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 –

相關問題