简体   繁体   中英

How can I send email using Gmail's SMTP without using smtplib?

This is my code:

import socket
import base64
s=socket.socket()    
s.connect(('smtp.gmx.com',25))    
message=s.recv(1024)    
print message    
s.send("EHLO gmx\r\n")    
message=s.recv(1024)    
print message    
Username=raw_input("Inesrt Username: ")    
Password=raw_input("Insert Password: ")    
UP=("\x00"+Username+"\x00"+Password).encode("base64")    
UP=UP.strip("\n")    
print "AUTH PLAIN "+UP    
s.send("AUTH PLAIN "+UP+"\r\n")    
message=s.recv(10000000)    
print message   
s.send("MAIL FROM:"+Username+"\r\n")    
message=s.recv(10000)    
print message    
To=raw_input("TO:")   
s.send("RCPT TO:"+To+"\r\n")   
message=s.recv(10000)  
print message    
s.send("DATA\r\n")    
message=s.recv(100000)    
print message    
Subject=raw_input("Subject: ")    
Text=raw_input("Message:")    
s.send("Subject: "+Subject+"\r\n\r\n"+Text+"\r\n.\r\n")
message=s.recv(10000)    
print message
s.send("QUIT\r\n")
message=s.recv(100000)   
print message   
s.close()

It works for regular email, but it does not work for Gmail or Yahoo. How can I send email using Gmail's SMTP using similar code?

Gmail SMTP server does not support plain SMTP protocol. You have to use TLS/SSL and connect to port 465 . See this question for more pointers how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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