0

我正在尝试在我的 PC 上设置一个需要身份验证的 SMTP 服务器(就像 gmail 一样)。尝试从客户端发送电子邮件 - “服务器不支持 SMTP AUTH 扩展”。我从python aiosmtpd 服务器获得了基本身份验证https://github.com/aio-libs/aiosmtpd/blob/master/examples/authenticated_relayer/server.py的帮助。

服务器代码 -

import os
import ssl
import subprocess
from aiosmtpd.smtp import SMTP
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Debugging
from aiosmtpd.smtp import AuthResult, LoginPassword


def auth_myauthenticator(server, session, envelope, mechanism, auth_data):
    print("call-----------")
    assert isinstance(auth_data, LoginPassword)
    username = auth_data.login
    password = auth_data.password    
    
    return AuthResult(success=True)


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

class CustomController():

    def handle_data(self, server, session, envelope, data):
        print("Handle data-----------------------------------------")
        
        
controller = Controller(CustomController(),hostname='127.0.0.1', port=809, ssl_context=context,
        authenticator=auth_myauthenticator , auth_required= True)
controller.start()

input('Running. Press enter to stop.\n')
controller.stop()

客户代码 -

import smtplib

sender = "xxx@gmail.com"
reciever = 'yyyy@gmail.com'

try:
   smtpObj = smtplib.SMTP_SSL('127.0.0.1',809)  
   smtpObj.login(sender,password)
   smtpObj.sendmail(sender, reciever, "Hii")
   print("Successfully sent email")
except Exception as e:
   print("Error: unable to send email", e)

在客户端代码中使用这个 smtplib.SMTP_SSL('smtp.gmail.com',465) 我能够通过 gmail 发送电子邮件。

还尝试评论只是为了调试

smtpObj.login(sender,password)

Error: unable to send email (530, b'5.7.0 Authentication required', 'xxxxx@gmail.com')
4

0 回答 0