Issue with SMTP Server Implementation: Getting an Error

I am trying to implement an SMTP server by following this article

Here is my server code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print ('Receiving message from:', peer)
print ('Message addressed from:', mailfrom)
print ('Message addressed to :', rcpttos)
print ('Message length :', len(data))
return
server = CustomSMTPServer(('127.0.0.1', 1025), None)
print ("Sever started ...")
asyncore.loop()
</code>
<code>import smtpd import asyncore class CustomSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): print ('Receiving message from:', peer) print ('Message addressed from:', mailfrom) print ('Message addressed to :', rcpttos) print ('Message length :', len(data)) return server = CustomSMTPServer(('127.0.0.1', 1025), None) print ("Sever started ...") asyncore.loop() </code>
import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):
    
    def process_message(self, peer, mailfrom, rcpttos, data):
        print ('Receiving message from:', peer)
        print ('Message addressed from:', mailfrom)
        print ('Message addressed to  :', rcpttos)
        print ('Message length        :', len(data))
        return


server = CustomSMTPServer(('127.0.0.1', 1025), None)
print ("Sever started ...")

asyncore.loop()

And here is my client code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import smtplib
import email.utils
from email.mime.text import MIMEText
# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'
server = smtplib.SMTP()
server.connect('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
"""
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
"""
</code>
<code>import smtplib import email.utils from email.mime.text import MIMEText # Create the message msg = MIMEText('This is the body of the message.') msg['To'] = email.utils.formataddr(('Recipient', '[email protected]')) msg['From'] = email.utils.formataddr(('Author', '[email protected]')) msg['Subject'] = 'Simple test message' server = smtplib.SMTP() server.connect('127.0.0.1', 1025) server.set_debuglevel(True) # show communication with the server server.sendmail('[email protected]', ['[email protected]'], msg.as_string()) """ try: server.sendmail('[email protected]', ['[email protected]'], msg.as_string()) finally: server.quit() """ </code>
import smtplib
import email.utils
from email.mime.text import MIMEText

# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'

server = smtplib.SMTP()
server.connect('127.0.0.1', 1025)

server.set_debuglevel(True) # show communication with the server
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())

"""
try:
    server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
    server.quit()
"""

I am getting the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>send: 'ehlo [127.0.1.1]rn'
reply: b'250-mvnd3x-VirtualBoxrn'
reply: b'250-SIZE 33554432rn'
reply: b'250-8BITMIMErn'
reply: b'250 HELPrn'
reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP'
send: 'mail FROM:<[email protected]> size=236rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<[email protected]>rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'datarn'
reply: b'354 End data with <CR><LF>.<CR><LF>rn'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn'
Traceback (most recent call last):
File "test_client_1.py", line 15, in <module>
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail
(code, resp) = self.data(msg)
File "/usr/lib/python3.8/smtplib.py", line 573, in data
(code, msg) = self.getreply()
File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
</code>
<code>send: 'ehlo [127.0.1.1]rn' reply: b'250-mvnd3x-VirtualBoxrn' reply: b'250-SIZE 33554432rn' reply: b'250-8BITMIMErn' reply: b'250 HELPrn' reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP' send: 'mail FROM:<[email protected]> size=236rn' reply: b'250 OKrn' reply: retcode (250); Msg: b'OK' send: 'rcpt TO:<[email protected]>rn' reply: b'250 OKrn' reply: retcode (250); Msg: b'OK' send: 'datarn' reply: b'354 End data with <CR><LF>.<CR><LF>rn' reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>' data: (354, b'End data with <CR><LF>.<CR><LF>') send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn' Traceback (most recent call last): File "test_client_1.py", line 15, in <module> server.sendmail('[email protected]', ['[email protected]'], msg.as_string()) File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail (code, resp) = self.data(msg) File "/usr/lib/python3.8/smtplib.py", line 573, in data (code, msg) = self.getreply() File "/usr/lib/python3.8/smtplib.py", line 398, in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") smtplib.SMTPServerDisconnected: Connection unexpectedly closed </code>
send: 'ehlo [127.0.1.1]rn'
reply: b'250-mvnd3x-VirtualBoxrn'
reply: b'250-SIZE 33554432rn'
reply: b'250-8BITMIMErn'
reply: b'250 HELPrn'
reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP'
send: 'mail FROM:<[email protected]> size=236rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<[email protected]>rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'datarn'
reply: b'354 End data with <CR><LF>.<CR><LF>rn'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn'
Traceback (most recent call last):
  File "test_client_1.py", line 15, in <module>
    server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
  File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail
    (code, resp) = self.data(msg)
  File "/usr/lib/python3.8/smtplib.py", line 573, in data
    (code, msg) = self.getreply()
  File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

I have double-checked the code and followed the article closely, but I am still encountering this issue. Could anyone help me understand what might be going wrong and how to fix it?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật