Blog

For one of my projects I wanted to develop a crash reporting dialog where user can enter the steps he did to crash the application and then by pressing send button the app automatically will send me user report and the log to my e-mail address. Unfortunatelly Qt doesn't have a Smtp library, and I don't want anything heavy just to send a simple string of characters through gmail smtp server.

Update

The above one actually looks like very decent peace of code, however again, it is a little bit too heavy for my taste and it didn't compile on Qt5 immidiatly (even the example) and I didn't want to go through to try and fix it.

So I found an example code for SMTP from Qt3. Which was almost exactly what I was looking for, it was simple for sure. However, it lacked two major features, it didn't support TLS/SSL communication and authentification I need it to use gmail as my smtp server. Then I thought how hard would it be to improve that code?

SMTP protocol for sending email

First of all every communication with the server will result a response from server. It is important to extract first 3 characters from the message which are response codes. Those codes obviously will be used for error checking whether communication is going smoothly.

Basic algorithm for sending email through smtp:

Connecting to the server and waiting for greeting message

If response code = 220 then send a greeting back to server "EHLO yourdomain.com\r\n"

If response code = 250 then identify from whom the mail is coming from by sending "MAIL FROM:<youremail@mail.com>\r\n"

If response code = 250 then send whom the email is addressed to "RCPT TO:<send_to@mail.com>\r\n"

If response code = 250 then now it is time to inititate sending the contents of the email by sending "DATA\r\n"

If response code = 354 then send your contents for example "This is an email\r\n"

If response code = 250 then your email has been sent and now you can close the connection by sending "QUIT\r\n"

Basically the above algorithm was used in the example code I found from Qt3. As you can see the above algorithm lacks authentification and SSL/TLS handshake. To do add those things to the code it turned out to be actually simpler than I first anticipated.

Basic algorithm for TLS/SSL smtp email sending:

Connecting to the server and waiting for greeting message

If response code = 220 then send a greeting back to server "EHLO yourdomain.com\r\n"

If response code = 250 then initiate TLS communication by making a SSL/TLS handshake, for this I use the methods provided in QSslSocket class, more precisely QSslSocket::startClientEncryption(). (make communictaion encrypted)

If response code = 250 then you have successfully initiated a encrypted communication and now we can start authentification by sending "AUTH LOGIN\r\n"

If response code = 334 then the server is waiting for username, since my aim was to use GMAIL as smtp server their API states that the username and password must be sent in base64 coding, it would look something like this "bXlfdXNlcm5hbWU=\r\n" (my_username)

If response code = 334 then send the server password, and again in base64 coding "bXlfcGFzc3dvcmQ=\r\n" (my_password)

If response code = 235 then you have successfully logged in and now you can identify from whom the mail is coming from by sending "MAIL FROM:<youremail@mail.com>\r\n"

If response code = 250 then send whom the email is addressed to "RCPT TO:<send_to@mail.com>\r\n"

If response code = 250 then now it is time to inititate sending the contents of the email by sending "DATA\r\n"

If response code = 354 then send your contents for example "This is an email\r\n"

If response code = 250 then your email has been sent and now you can close the connection by sending "QUIT\r\n"

That's it, by following the above pseudo code, you should be able to write a simple TLS/SSL SMTP client for sending email using gmail as smtp server. You can use the above approach to send email through telnet aswell.

Important: Every command to server has to be finished with carriege return and new line symbol in other words you have to finish the line with "\r\n"

So by following the steps I wrote above, I slightly rewrote the example code which you can see below. However I would advise just trying to write the code yourself, belive it's not that hard, if you follow the steps.

First of all, for the code to run you will need Qt with ssl support compiled within and OpenSSL library . If you have ssl compiled in the Qt library you don't need to add anything to the pro file, except QT+= network.

It is great for all, I have some different tricks if you want to know it then for that I have some different tricks, for that, you want to learn to code. For that, if you want to know it then for that you just visit some tutorial. And from there it is great for all. If you faced any type...

I have read the post and it is very much helpful because I have got to know about the open cv on the raspberry pi. The images and the screenshots provided here will guide you for your questions. The code you will get here is very unique and can be only used to run OpenCV. If you face any...