Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

email - SMTP authentication with self-signed .PEM Certificate Python

I'm trying to authenticate as client on a SMTP server, that requires certificate.
The problem is i'm only given a self-sigend certificate (.pem) Note: I'm trying to implement the solution in tls 1.2

The code i expected to use is below, but since they gave me only one self-signed certificate for the authentication is pretty much useless(?)

Do you have any suggestion?

def create_smtp_server(smtp_server, port, username, password, certificate):
    """
    Create a SMTP server instance to send the Email
    :param: smtp_server:
    :param: port:
    :param: username:
    :param: password:
    :return: SMTP server instance
    """
    try:
        client_cert = certificate
        server_cert = certificate
        client_key = certificate
        server = smtplib.SMTP(smtp_server, port)
        server.connect(smtp_server, port)
        server.ehlo()
        context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=server_cert)
        context.load_cert_chain(certfile=client_cert, keyfile=client_key)
        server.starttls(context=context)
        server.login(username, password)
        log(log_type="SERVER_CONNECTED", msg="SMTP server created", detail=None)
    except Exception as e:
        log(log_type="SERVER_CONNECTION_ERROR", msg="Can't create SMTP server", detail=e)
        server = None
    return server

I also tried with the code blow but did not work

...
context = ssl.create_default_context(certificate)
server.starttls(context=context)
...
question from:https://stackoverflow.com/questions/65935962/smtp-authentication-with-self-signed-pem-certificate-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...