0

I try to send automatic emails from a Qt application if a user creates a new incident. Obviously, the purpose is to warn other people about this incident.

My problem is actually to send the email. I found this library of BlueTiger9. It's an email sender which seems to work but unfortunately not for me, and I'm currently facing to an issue that I cannot explain.

I put the following parts of my code that I assume will help to define what's going wrong. If anything more is needed, just let me know.

In my .pro file:

    LIBS += -L$$PWD/ -lSMTPEmail

I included a file that includes all the other headers and I added all the headers of the library in my project too.

    #ifndef SMTPMIME_H
    #define SMTPMIME_H

    #include "smtpclient.h"
    #include "mimepart.h"
    #include "mimehtml.h"
    #include "mimeattachment.h"
    #include "mimemessage.h"
    #include "mimetext.h"
    #include "mimeinlinefile.h"
    #include "mimefile.h"

    #endif // SMTPMIME_H

In each header included, there is another inclusion of the following code:

    #ifndef SMTPEXPORTS_H
    #define SMTPEXPORTS_H

    #ifdef SMTP_BUILD
    #define SMTP_EXPORT Q_DECL_EXPORT
    #else
    #define SMTP_EXPORT Q_DECL_IMPORT
    #endif

    #endif // SMTPEXPORTS_H

Then I just tried to use the example of sending a simple email (I just added the proxy settings):

    SmtpClient smtp("smtp.office365.com", 587, SmtpClient::TlsConnection);
    smtp.setUser("USER_Off365");
    smtp.setPassword("PASSWORD_Off365");
    smtp.setAuthMethod(SmtpClient::AuthLogin);
    QNetworkProxy proxy(QNetworkProxy::HttpProxy, "PROXY_ADD", PROXY_PORT, "PROXY_USER", "PROXY_PW");
    smtp.getSocket()->setProxy(proxy);
    MimeMessage message;
    message.setSender(new EmailAddress("MY_ADDRESS", "MY_NAME"));
    message.addRecipient(new EmailAddress("ADDRESS_TO", "A_NAME"));
    message.setSubject("Subject");

    MimeText text;
    text.setText("Test");

    message.addPart(&text);

    if(smtp.connectToHost()){
        if(smtp.login()){
            if(smtp.sendMail(message)){
                smtp.quit();
            }
        }
    }

But I am not able to connect to the SMTP server. The error that I received from the socket is "Error communicating with HTTP Proxy"

Trying with an Internet connection from my phone, I am able to go to the next step but I still have a problem of authentication.

Just in case, I need to use Office365 SMTP server and the settings to connect to it are available on this page. (smtp.office365.com / 587 explicit / TLS v1-1.2)

If anyone has any idea which could help, it would be really appreciated!

Tuvia
  • 13
  • 7
  • I see that by default auth method of `SmtpClient` is rejected by the server. You will have to set `smtp.setAuthMethod(SmtpClient::AuthLogin);`. However, you have a problem that socket is unconnected. You can just put `qDebug() << text` print into `SmtpClient::sendMessage`. So, you will see on which step the socket is unconnected. – Orest Hera Oct 28 '15 at 17:03
  • Thanks for the idea. I just edited my code and my post. But I'm not really comfortable with SMTP since it's the first time I have to use it by my own. – Tuvia Oct 28 '15 at 17:15
  • It's pretty obvious? The socket is NOT connected. You should check for errors, which you do not anywhere. – user3427419 Oct 28 '15 at 17:16
  • Well, I just changed a little bit my code and now, of course, I don't reach any error since I checked before if the connection is established to the server. The problem is so, why am I not able to connect to the server? – Tuvia Oct 28 '15 at 17:21
  • It might be related to proxy. You can also put some debug prints into `SmtpClient::connectToHost` to see which step failed. Make sure that SSL works for you. At first print in your app `QSslSocket::supportsSsl()`. It should be `true` to use encryption. You are working on Windows. There is no OpenSSL library by default in Windows and it is not shipped with Qt. – Orest Hera Oct 28 '15 at 17:45
  • So SSL is supported. Yes I'm working on Windows, sorry to not mentionned it earlier. So from what I understood is that I have to build the library. Please correct me if I am wrong? – Tuvia Oct 28 '15 at 17:54
  • The function `QSslSocket::supportsSsl()` returns `true` or `false`? If it is `true` then there is somewhere is your system `PATH` some OpenSSL dll. It does not mean that it supports required `TLS` version. If `false` it means that there is no OpenSSL. I see some installers http://slproweb.com/products/Win32OpenSSL.html, but it also can be compiled from source. – Orest Hera Oct 28 '15 at 17:59
  • It is hard to guess. It is also possible to print error code and string from socket when it fails: `smtp.getSocket()->error();` and `smtp.getSocket()->errorString();` – Orest Hera Oct 28 '15 at 18:06
  • I received a "Error communicating with HTTP Proxy". – Tuvia Oct 28 '15 at 18:15
  • Qt is able to communicated with `smtp.office365.com` through HTTP proxy using `SmtpClient` and TLS connection. But in your case the proxy does not accept such connection. You established TCP connection with the proxy, however it replied something wrong. Such error means that the response code from the proxy is not one from the list 200, 407, 403, 404, 405, 503, for example "400 Bad Request". Wireshark can be used to log real traffic. You can try your setup with some mail client. – Orest Hera Oct 28 '15 at 19:29
  • Well, I tried to add some code to show the response code of the proxy but it seems not even to answer. I also tried to ping it, I can resolve its hostname to the IP address but I'm not able to ping. I think I'll just change the way I send the emails, It will take less time. Btw, I already use Outlook with this email account for my own, and it works perfectly... – Tuvia Oct 29 '15 at 13:23

0 Answers0