简体   繁体   English

Python中的邮件客户端仅使用套接字(没有smtplib)

[英]Mail Client in Python using sockets only(no smtplib)

I am trying to write a python program which will send emails without using smtplib. 我正在尝试编写一个python程序,它将在不使用smtplib的情况下发送电子邮件。 I have tried to look other posts on this but could not find a solution. 我试图查看其他帖子,但找不到解决方案。

`from socket import *


msg = "\r\n My email's content!"
endmsg = "\r\n.\r\n"

mailserver = ("smtp.aol.com", 25) 

clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)

recv = clientSocket.recv(1024)
recv = recv.decode()
print("Message after connection request:" + recv)
if recv[:3] != '220':
    print('220 reply not received from server.')
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print("Message after HeLO command:" + recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')

# #Info for username and password
# username = "myusername"
# password = "xxxxxxxx"
# authMsg = "AUTH LOGIN\r\n"
# clientSocket.send(authMsg.encode())
# recv_auth = clientSocket.recv(1024)
# print(recv_auth.decode())

# clientSocket.send(username.encode())
# clientSocket.send("\r\n".encode())
# recv_user = clientSocket.recv(1024)
# print("Response after sending username: "+recv_user.decode())

# clientSocket.send(password.encode())
# clientSocket.send("\r\n".encode())
# recv_pass = clientSocket.recv(1024)
# print("Response after sending password: "+recv_pass.decode())


# clientSocket.send(('starttls\r\n').encode())
# recv_tls = clientSocket.recv(1024)
# print(recv_tls.decode())



mailFrom = "MAIL FROM:<yjorayev@aol.com>\r\n"
clientSocket.send(mailFrom.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("After MAIL FROM command: "+recv2)

rcptTo = "RCPT TO:<ijorayev@gmail.com>\r\n"
clientSocket.send(rcptTo.encode())
recv3 = clientSocket.recv(1024)
recv3 = recv3.decode()
print("After RCPT TO command: "+recv3)

data = "DATA\r\n"
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024)
recv4 = recv4.decode()
print("After DATA command: "+recv4)

clientSocket.send(msg.encode())

clientSocket.send(endmsg.encode())
recv_msg = clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())

quit = "QUIT\r\n"
clientSocket.send(quit.encode())
recv5 = clientSocket.recv(1024)
print(recv5.decode())
clientSocket.close()` 

Is it possible to send an email without logging in to server? 是否可以在不登录服务器的情况下发送电子邮件? If not how do I login correctly? 如果不是我如何正确登录? Every time I tried to log in I have received "authentication failed" error. 每次我尝试登录时都收到“身份验证失败”错误。 I also have errors saying "Need MAIL command" and "Need RCPT command" even though I am sending MAIL FROM and RCPT TO commands. 即使我发送MAIL FROM和RCPT TO命令,我也有错误说“需要MAIL命令”和“需要RCPT命令”。

I was able to send e-mail using smtp.gmx.com 我能够使用smtp.gmx.com发送电子邮件

from socket import *
import base64
import time

msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
mailserver = ("smtp.gmx.com", 25) #Fill in start #Fill in end
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024)
recv = recv.decode()
print("Message after connection request:" + recv)
if recv[:3] != '220':
    print('220 reply not received from server.')
heloCommand = 'EHLO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print("Message after EHLO command:" + recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')

#Info for username and password
username = "xxxxxx"
password = "xxxxxx"
base64_str = ("\x00"+username+"\x00"+password).encode()
base64_str = base64.b64encode(base64_str)
authMsg = "AUTH PLAIN ".encode()+base64_str+"\r\n".encode()
clientSocket.send(authMsg)
recv_auth = clientSocket.recv(1024)
print(recv_auth.decode())

mailFrom = "MAIL FROM:<xxxxxxxx>\r\n"
clientSocket.send(mailFrom.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("After MAIL FROM command: "+recv2)
rcptTo = "RCPT TO:<xxxxxxxxxx>\r\n"
clientSocket.send(rcptTo.encode())
recv3 = clientSocket.recv(1024)
recv3 = recv3.decode()
print("After RCPT TO command: "+recv3)
data = "DATA\r\n"
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024)
recv4 = recv4.decode()
print("After DATA command: "+recv4)
subject = "Subject: testing my client\r\n\r\n" 
clientSocket.send(subject.encode())
date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
date = date + "\r\n\r\n"
clientSocket.send(date.encode())
clientSocket.send(msg.encode())
clientSocket.send(endmsg.encode())
recv_msg = clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())
quit = "QUIT\r\n"
clientSocket.send(quit.encode())
recv5 = clientSocket.recv(1024)
print(recv5.decode())
clientSocket.close()

The only problem is that gmail rejects emails, but some other email servers do receive. 唯一的问题是gmail拒绝电子邮件,但其他一些电子邮件服务器确实收到了。 Now I am trying to send email from smtp.gmail.com which requires SSL or TLS. 现在我正在尝试从smtp.gmail.com发送需要SSL或TLS的电子邮件。 Any suggestions/help on that? 有什么建议/帮助吗?

Is it possible to send an email without logging in to server? 是否可以在不登录服务器的情况下发送电子邮件?

It depends on the server. 这取决于服务器。 smtp.aol.com is the server for customers of AOL mail and this server needs you to log in. The MX for AOL ( dig mx aol.com ) does not need you to login since it is used for communication between MTA but it has other restrictions in order to fight spam. smtp.aol.com是AOL邮件客户的服务器,此服务器需要您登录.AOL的MX( dig mx aol.com )不需要您登录,因为它用于MTA之间的通信但它有其他限制,以打击垃圾邮件。

If not how do I login correctly? 如果不是我如何正确登录?

First you should better use EHLO not HELO and then you would also see which authentication methods are supported by the server. 首先,您应该更好地使用EHLO而不是HELO,然后您还会看到服务器支持哪些身份验证方法。

250-AUTH PLAIN LOGIN XAOL-UAS-MB XOAUTH2

For an introduction into PLAIN and LOGIN authentication (and others) see http://www.samlogic.net/articles/smtp-commands-reference-auth.htm . 有关PLAIN和LOGIN身份验证(以及其他)的介绍,请参阅http://www.samlogic.net/articles/smtp-commands-reference-auth.htm In short: for PLAIN you have to provide base64(username+"\\0"+password) . 简而言之:对于PLAIN,您必须提供base64(username+"\\0"+password)

I also have errors saying "Need MAIL command" and "Need RCPT command" even though I am sending MAIL FROM and RCPT TO commands. 即使我发送MAIL FROM和RCPT TO命令,我也有错误说“需要MAIL命令”和“需要RCPT命令”。

You provide no debug output but I guess that the server is rejecting your MAIL FROM and RCPT TO commands because you are not authorized. 您没有提供调试输出,但我猜服务器拒绝您的MAIL FROM和RCPT TO命令,因为您未获得授权。

You might need to enable IMAP in order to use smtp.gmail.com. 您可能需要启用IMAP才能使用smtp.gmail.com。 I just did that and it works perfectly. 我刚刚做到了,它完美无缺。 You have to use either port 587 or 465. To enable IMAP click on the gear icon in your gmail account, then click Forwarding and POP/IMAP. 您必须使用端口587或465.要启用IMAP,请单击您的Gmail帐户中的齿轮图标,然后单击转发和POP / IMAP。 Finally click enable IMAP and save changes. 最后单击启用IMAP并保存更改。 This should work. 这应该工作。

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

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