首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法发送或接收python aiosmtpd SMTP服务器的电子邮件,卡在250 ok。

无法发送或接收python aiosmtpd SMTP服务器的电子邮件,卡在250 ok。
EN

Stack Overflow用户
提问于 2021-12-25 04:21:43
回答 1查看 793关注 0票数 1

我已经“成功”制作了SMTP服务器。代码很好地连接到SMTP客户端。但它既不能接收电子邮件,也不能发送电子邮件。我尝试了各种测试服务器,以及标准的gmail/yahoo等。下面是代码:

代码语言:javascript
复制
    # Copyright 2014-2021 The aiosmtpd Developers
# SPDX-License-Identifier: Apache-2.0

import asyncio
from asyncio.base_events import Server
import logging
import aiosmtpd
from aiosmtpd.controller import DEFAULT_READY_TIMEOUT, Controller
import ssl
from aiosmtpd.smtp import Envelope, Session
from smtplib import SMTP as SMTPCLient


context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain('cert.pem', 'privkey.pem')


class ExampleHandler():
    async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
        if address.endswith('@example.com'):
            print('not relaying to that domain bro :(')
            return '550 not relaying to that domain'
        envelope.rcpt_tos.append(address)
        print(address+" "+"is added to rcpt_tos")
      

        # Make an envelope for the recipient with the same content.
        
        
        
        return '250 OK'



    # async def handle_EHLO(self, server, session, envelope):
    #     print('EHLO from %s' % envelope.mail_from)
    #     return '250-Hello, how are you?\n250-I am fine\n250 HELP'    


    async def handle_DATA(self, server, session, envelope):
        print('Message from %s' % envelope.mail_from)
        print('Message for %s' % envelope.rcpt_tos)
        print('Message data:\n')
        for ln in envelope.content.decode('utf8', errors='replace').splitlines():
            print(f'> {ln}'.strip())
        print()
        print('End of message')
        # Dump the contents of envelope.content to a file.
        fi=open('./mailbox/firstletter.txt','w')
        fi.write(envelope.content.decode('utf8', errors='replace'))
        fi.close()




        # print everything in DATA.
        # Send the envelope to the recipient.


        return '250 Message will be delivered'
    #Define Relay server.
  
   




async def amain(loop):
    cont = Controller(ExampleHandler(),hostname='x.x.x.x', port=25, server_hostname='Galam Limited',ready_timeout=5000) 
    # Combining ExampleHandler and Controller into a single Controller.
   
    cont.start()


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    loop = asyncio.get_event_loop()
    loop.create_task(amain(loop=loop))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

您可以测试服务器的可达性。我被困住了,花了整整两天都没有用。问题是完全没有连接性,我打开了端口25。确保与高爸爸之间也没有外部问题。任何帮助都会得到认可。

编辑:1线鲨数据的一个快速峰值显示,当我运行客户端脚本时,绝对没有任何数据包被传输到外部。这是我用来测试的clinet脚本。

代码语言:javascript
复制
from smtplib import SMTP as Client
from aiosmtpd.controller import Controller
class controller:
    hostname='192.168.1.33'
    port=25
client = Client(controller.hostname, controller.port)
r = client.sendmail('a@galam.in', ['tester@192.168.1.200'], """\
From: Anne Person <anne@galam.in>
To: Bart Person <tester@192.168.1.200>
Subject: A test
Message-ID: <ant>
Hi Bart, this is Anne.
""")
EN

回答 1

Stack Overflow用户

发布于 2021-12-25 04:45:52

SMTP 250代码意味着已经建立了成功的连接,但是您要发送邮件的远程主机可能已将发送邮件的域归类为不合法的域。

如果您的域没有经过身份验证/验证,就会发生这种情况。您可以通过可信的SMTP服务(如sendgrid )中继邮件。

您还可以通过将邮件从您的服务发送到check-auth@verifier.port25.com来检查您的域是否被验证。Port25是一个自动工具,可以验证您的DNS记录、SPF记录等。

希望这对你有用!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70477971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档