• SMTP port 465 with OpenSSL

    C/C++, Programming

    To secure your SMTP, we are going to use OpenSSL comand in LINUX environment and we are going use authentication that is generated by base64. I will show you the basic command that is required for this topic. And lastly, I made a C++ class SSLSocket to send an email which is similar steps in LINUX command and I am going to use GMAIL as our SMTP server.


    LINUX: Initiate OpenSSL command

    openssl s_client -connect smtp.gmail.com:465 -crlf

    Using the above command, you will be receiving a message similar like this "220 smtp.gmail.com ESMTP - gsmtp" which shows that the command is successfull.

     

    LINUX: HELO/EHLO clause

    helo invadev.com

    The sending server will identify who it is and the receiving server will accept any given name. Right now there is no requirement to give the correct information at this stage. The above command will return something similar like this "250 smtp.gmail.com at your service".

     

    LINUX: authentication

    auth plain AHVzZXJuYW1...kQGdtYWlsLmNvbQ==

    By sending this command, you will be receiving a response similar like this "235 2.7.0 Accepted". To generate base64 just simply run this comand in LINUX command prompt "echo -ne '\0username@gmail.com\0password'|base64" and take note to use of \0 before username and password.

     

    LINUX: mail from

    mail from:

    By sending this command, you will be receiving a response similar like this "250 2.1.0 OK - gsmtp".

     

    LINUX: rcpt to

    rcpt to:

    By sending this command, you will be receiving a response similar like this "250 2.1.0 OK - gsmtp".

     

    LINUX: data

    data

    By sending this command, you will be receiving a response similar like this "354  Go ahead - gsmtp".

     

    LINUX: content

    subject: Hello everyone!
    This is a test, please ignore
    .

    To end the content just simply type '.' (DOT) in the next line and you will be receiving similar message like this "250 2.0.0 OK - gsmtp"

     

    C/C++: Class instance

    cpp::net::ssl::SSLSocket sslsocket("smtp.gmail.com", 465);

    Creates instance of SSLSocket by passing SMTP and port.

     

    C/C++: connect

    sslsocket.connect()

    This will establish connection between the socket and the SMTP server

     

    C/C++: readLine

    sslsocket.readLine(buffer);

    This will read a response from the SMTP server terminated by newline.

     

    C/C++: write

    sslsocket.write("HELO invadev.com\r\n");

    This will send command to the SMTP server.

     

    C/C++: codes

        cpp::net::ssl::SSLSocket sslsocket("smtp.gmail.com", 465);

        if(sslsocket.connect())
        {
            ret = sslsocket.readLine(buffer);
            
            sslsocket.write("helo invadev.com\r\n");
            ret = sslsocket.readLine(buffer);

            sslsocket.write("auth plain AHVzZXJuYW1l...tYWlsLmNvbQ==\r\n");
            ret = sslsocket.readLine(buffer);

            sslsocket.write("mail from: \r\n");
            ret = sslsocket.readLine(buffer);

            sslsocket.write("rcpt to: \r\n");
            ret = sslsocket.readLine(buffer);

            sslsocket.write("data\r\n");
            ret = sslsocket.readLine(buffer);

            sslsocket.write(\
            "Subject: Test SMTP\r\n" \
            "This is a test from sslsocket.\r\n" \
            ".\r\n");
            ret = sslsocket.readLine(buffer);
        }

     

  • blog comments powered by Disqus
  • Support Us
  • Help this website up and running by buying me a coffee for only $0.50.