Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. How can the user send email to me from my app ?

How can the user send email to me from my app ?

Scheduled Pinned Locked Moved Unsolved Brainstorm
qt5.7.0
32 Posts 8 Posters 15.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV VRonin

    When there is a library that is considered standard I tend to suggest it over some 1-man-wrapper. Not because the latter is bad but it's so much easier to find docs/examples/support when a library is very popular.

    This leads me to libcurl (there are some relatively active C++ bindings like https://github.com/JosephP91/curlcpp but the core C api should be more than enough for what you are trying to do)

    Here you can find an example of sending an email using gmail

    AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #15

    @VRonin @p3c0 @mrjj

    Okay guys tell me how can i simply let user to login to his/her email from my app and then let them send email to me ?? I have to get connected to the email server before i let them sign in right ?

    what is a signature ?? Lol

    VRoninV 1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #16

      @Ahti Your users will use their email address only to login to you forum.
      Now that you have email service and a registered user to use that service, your users will use this email user to send email to you.
      You should probably maintain multiple users in case if you have a large userbase.

      157

      1 Reply Last reply
      0
      • AhtiA Ahti

        @VRonin @p3c0 @mrjj

        Okay guys tell me how can i simply let user to login to his/her email from my app and then let them send email to me ?? I have to get connected to the email server before i let them sign in right ?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #17

        @Ahti said in How can the user send email to me from my app ?:

        Okay guys tell me how can i simply let user to login to his/her email from my app and then let them send email to me ?

        This is even harder as you should know their mail server and sometimes you cannot really access it (for example if they use a private MS exchange server)

        I put here another example with libcurl: https://curl.haxx.se/libcurl/c/smtp-mail.html

        If you think it's not for you then the easy route is using QDesktopServices::openUrl("mailto:youremail@address.com");

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        7
        • AhtiA Ahti

          @VRonin

          Well that is difficult to understand and i don't know how to implement it.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #18

          @Ahti Why not just let the user use their preferred EMail client to send an EMail to you? See http://stackoverflow.com/questions/28674805/open-default-mail-app-from-within-qt-with-some-html
          It is much easier and the user can use his/her EMail client.
          You still can have a form in your app (I guess you meant form not forum?), but then you pass that data via a mailto: URL to the system, which then opens the standard EMail client and fill the data user entered in your form.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          AhtiA 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Ahti Why not just let the user use their preferred EMail client to send an EMail to you? See http://stackoverflow.com/questions/28674805/open-default-mail-app-from-within-qt-with-some-html
            It is much easier and the user can use his/her EMail client.
            You still can have a form in your app (I guess you meant form not forum?), but then you pass that data via a mailto: URL to the system, which then opens the standard EMail client and fill the data user entered in your form.

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by
            #19

            @jsulm
            Okay why would he has to login from the broswer and then use my app to send email to me ? Can't he simply login to his email account from my app and send mail to me from my app ? And yes i meant form not forum sorry for the mistake. And i used that "QDesktopService..." And it opens up MS outlook.

            what is a signature ?? Lol

            jsulmJ 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #20

              Ok. listen. we need to talk about sending emails.

              A program cannot just send email. It need the help of
              a mail server. That is another PC that has a post office program.
              Like GMAIL is. its a mail service.

              So for your program to send mails, you need to send via such server.

              That can happen

              1: You pop outlook via QDesktopService and the mail is sent over whatever server outlook is using.

              2: You send via a google account that your program knows username and password for.
              So before sending email via GOOGLE you will login to this account inside the program.
              This is part of the SMTP protocol.

              3: You use some online mail service where you can send email via some API.

              You could also ask user to login to his mail service but I would never give my password to any unknown program so it's not a good solution and also teaches the users to do stupid things.

              So the part you need to understand is that Sending email demands that the MAIL server knows who u are. So the programs need to supply username and password to the MAil server
              to be allowed to send anything.

              AhtiA 2 Replies Last reply
              4
              • AhtiA Ahti

                @jsulm
                Okay why would he has to login from the broswer and then use my app to send email to me ? Can't he simply login to his email account from my app and send mail to me from my app ? And yes i meant form not forum sorry for the mistake. And i used that "QDesktopService..." And it opens up MS outlook.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #21

                @Ahti To be sure: when you say "my app" you mean a desktop app? If so then there is no need to log-in from a browser, why do you think so? The user opens that dialog/form in your app, enters needed data (subject, message body) and presses the "Send" button. Then his/her default EMail client is opened where he/she send the EMail from there.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                AhtiA 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @Ahti To be sure: when you say "my app" you mean a desktop app? If so then there is no need to log-in from a browser, why do you think so? The user opens that dialog/form in your app, enters needed data (subject, message body) and presses the "Send" button. Then his/her default EMail client is opened where he/she send the EMail from there.

                  AhtiA Offline
                  AhtiA Offline
                  Ahti
                  wrote on last edited by Ahti
                  #22

                  @jsulm yes from my desktop app how can i connect to user's email so that he can send email.

                  what is a signature ?? Lol

                  jsulmJ 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Ok. listen. we need to talk about sending emails.

                    A program cannot just send email. It need the help of
                    a mail server. That is another PC that has a post office program.
                    Like GMAIL is. its a mail service.

                    So for your program to send mails, you need to send via such server.

                    That can happen

                    1: You pop outlook via QDesktopService and the mail is sent over whatever server outlook is using.

                    2: You send via a google account that your program knows username and password for.
                    So before sending email via GOOGLE you will login to this account inside the program.
                    This is part of the SMTP protocol.

                    3: You use some online mail service where you can send email via some API.

                    You could also ask user to login to his mail service but I would never give my password to any unknown program so it's not a good solution and also teaches the users to do stupid things.

                    So the part you need to understand is that Sending email demands that the MAIL server knows who u are. So the programs need to supply username and password to the MAil server
                    to be allowed to send anything.

                    AhtiA Offline
                    AhtiA Offline
                    Ahti
                    wrote on last edited by
                    #23

                    @mrjj yeah that is what i want, user should login to his mail account from my desktop app and then should sent mail from my desktop app.

                    what is a signature ?? Lol

                    1 Reply Last reply
                    1
                    • AhtiA Ahti

                      @jsulm yes from my desktop app how can i connect to user's email so that he can send email.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #24

                      @Ahti If you want that the user logs in from your app then the user must enter his EMail account data (server address/port, user name, password). That's why I suggested to not to do it this way but just start users EMail client (Outlook, Thunderbird, what ever). This is much easier and the user can use his/her EMail client.
                      @mrjj already explained what needs to be done, so it is really not an easy task and overkill in my opinion.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • P Offline
                        P Offline
                        peteritv
                        wrote on last edited by
                        #25

                        Again, maybe I am completely out of line here since I am just a Qt beginner myself, but ur talking about browsers, right?
                        So you have some webserver that serves ur app, right?

                        If ur server is not able to handle SMTP (mail) then I think we're done talking.

                        Look at ur server! Not the app itself...

                        P 1 Reply Last reply
                        0
                        • P peteritv

                          Again, maybe I am completely out of line here since I am just a Qt beginner myself, but ur talking about browsers, right?
                          So you have some webserver that serves ur app, right?

                          If ur server is not able to handle SMTP (mail) then I think we're done talking.

                          Look at ur server! Not the app itself...

                          P Offline
                          P Offline
                          peteritv
                          wrote on last edited by
                          #26

                          @peteritv said in How can the user send email to me from my app ?:

                          Again, maybe I am completely out of line here since I am just a Qt beginner myself, but ur talking about browsers, right?
                          So you have some webserver that serves ur app, right?

                          If ur server is not able to handle SMTP (mail) then I think we're done talking.

                          Look at ur server! Not the app itself...

                          I do not believe that sending an e-mail from ur app to you has anything to do with mail suppliers or clients.
                          When I put a simple <a with a href to my e-mail account, anyone, no matter what, can send me an e-mail.

                          You can even put it in ur apps global stylesheet I guess...

                          jsulmJ 1 Reply Last reply
                          0
                          • P peteritv

                            @peteritv said in How can the user send email to me from my app ?:

                            Again, maybe I am completely out of line here since I am just a Qt beginner myself, but ur talking about browsers, right?
                            So you have some webserver that serves ur app, right?

                            If ur server is not able to handle SMTP (mail) then I think we're done talking.

                            Look at ur server! Not the app itself...

                            I do not believe that sending an e-mail from ur app to you has anything to do with mail suppliers or clients.
                            When I put a simple <a with a href to my e-mail account, anyone, no matter what, can send me an e-mail.

                            You can even put it in ur apps global stylesheet I guess...

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #27

                            @peteritv It is a desktop application as far as I know. See above, I already asked what exactly it is.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • thamT Offline
                              thamT Offline
                              tham
                              wrote on last edited by tham
                              #28

                              user should login to his mail account from my desktop app and then should sent mail from my desktop app.

                              As jsulm mentioned, this is not a trivial task, the easiest solution is QDesktopServices::openUrl("mailto:youremail@address.com") as VRonin mentioned.
                              If you insist on implement smtp functions in your app, the most mature library of c++ I could think of is poco(c is libcurl).

                              Following are the example of using gmail by poco

                              #include <Poco/Net/MailMessage.h>
                              #include <Poco/Net/MailRecipient.h>
                              #include <Poco/Net/SMTPClientSession.h>
                              #include <Poco/Net/NetException.h>
                              #include <Poco/Net/SecureSMTPClientSession.h>
                              #include <Poco/Net/InvalidCertificateHandler.h>
                              #include <Poco/Net/AcceptCertificateHandler.h>
                              #include <Poco/Net/SSLManager.h>
                              #include <Poco/Net/SecureStreamSocket.h>
                              #include <Poco/Net/MailRecipient.h>
                              
                              #include <Poco/UnicodeConverter.h>
                              
                              #include <iomanip>
                              #include <iostream>
                              #include <string>
                              
                              int main()
                              {
                                  using namespace Poco::Net;
                              
                                  std::string const host("smtp.gmail.com");
                                  int const port = 465;
                                  std::string const sUserName("userEmail@gmail.com");
                                  std::string const sPassword("whatever");
                                  std::string const to("MyEmail@gmail.com");
                                  std::string const from("myUser");
                                  std::string const subject("new feature suggestion");
                                  std::string const content("more animations");
                                  Poco::Net::MailMessage message;
                              
                                  try{
                                      Poco::SharedPtr<InvalidCertificateHandler> pCert =
                                              new AcceptCertificateHandler(false);
                                      Context::Ptr pContext =
                                              new Poco::Net::Context(Context::CLIENT_USE, "", "",
                                                                     "", Context::VERIFY_NONE);
                                      SSLManager::instance().initializeClient(0, pCert, pContext);
                              
                                      SecureStreamSocket pSSLSocket(pContext);
                                      pSSLSocket.connect(SocketAddress(host, port));
                                      SecureSMTPClientSession secure(pSSLSocket);
                              
                                      secure.login();
                                      bool const tlsStarted = secure.startTLS(pContext);
                                      std::cout<<"tls can start : "<<std::boolalpha<<tlsStarted<<std::endl;
                                      secure.login(SMTPClientSession::AUTH_LOGIN, sUserName, sPassword);
                              
                                      message.setSender(from);
                                      message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, to));
                                      message.setSubject(subject);
                                      message.setContentType("text/plain; charset=UTF-8");
                                      message.setContent(content, MailMessage::ENCODING_8BIT);
                              
                                      std::cout<<"send message"<<std::endl;
                                      secure.sendMessage(message);
                                      std::cout<<"close sender"<<std::endl;
                                      secure.close();
                                  }catch(SMTPException &e){
                                      std::cerr << e.code() << std::endl;
                                      std::cerr << e.message() << std::endl;
                                      std::cerr << e.what() << std::endl;
                                      std::cerr << e.displayText().c_str() << std::endl;
                                  }catch (NetException &e){
                                      std::cerr << e.code() << std::endl;
                                      std::cerr << e.message() << std::endl;
                                      std::cerr << e.what() << std::endl;
                                      std::cerr << e.displayText().c_str() << std::endl;
                                  }catch(std::exception const &ex){
                                      std::cerr<<ex.what()<<std::endl;
                                  }
                              }
                              

                              Not every email server works with this example(I guess in most cases what you need to do is adjust the host and port), if you want to support different servers(gmail, hotmail etc), you will need to try them out one by one.

                              If you do not want to force the users enter their user name and password all of the times, you can save their input by QSettings(better ask the user they want to store their passwords and user name before they do that, and allowed them to remove it later).

                              However, I will prefer this simple SMTP class which only depends on Qt5, because dependency problems of c++ could be quite nasty to solve.

                              1 Reply Last reply
                              3
                              • jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #29

                                To be honest if I would be a user of such an application I would NEVER enter my login data for my EMail account! I only trust my EMail client. An application asking me for my EMail login data would be uninstalled immediately.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  Ok. listen. we need to talk about sending emails.

                                  A program cannot just send email. It need the help of
                                  a mail server. That is another PC that has a post office program.
                                  Like GMAIL is. its a mail service.

                                  So for your program to send mails, you need to send via such server.

                                  That can happen

                                  1: You pop outlook via QDesktopService and the mail is sent over whatever server outlook is using.

                                  2: You send via a google account that your program knows username and password for.
                                  So before sending email via GOOGLE you will login to this account inside the program.
                                  This is part of the SMTP protocol.

                                  3: You use some online mail service where you can send email via some API.

                                  You could also ask user to login to his mail service but I would never give my password to any unknown program so it's not a good solution and also teaches the users to do stupid things.

                                  So the part you need to understand is that Sending email demands that the MAIL server knows who u are. So the programs need to supply username and password to the MAil server
                                  to be allowed to send anything.

                                  AhtiA Offline
                                  AhtiA Offline
                                  Ahti
                                  wrote on last edited by
                                  #30

                                  @mrjj @peteritv @tham @jsulm

                                  ok guys i used the desktopservice::openurl...mailto.. but its opening outlook what if user doesn't have outlook ? why can't i simply open the browser and let him login to his email server and when he/she is logged in i should go directly to compose email and then in 'to' section there should be my emailaddress . how to do that ?

                                  what is a signature ?? Lol

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • AhtiA Ahti

                                    @mrjj @peteritv @tham @jsulm

                                    ok guys i used the desktopservice::openurl...mailto.. but its opening outlook what if user doesn't have outlook ? why can't i simply open the browser and let him login to his email server and when he/she is logged in i should go directly to compose email and then in 'to' section there should be my emailaddress . how to do that ?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #31

                                    @Ahti desktopservice::openurl will open the EMail client the user configured. In your case it looks like this is Outlook, for other users it can be something else (on my Linux machine it would open Thunderbird), no issue here.
                                    Regarding your last question: not all users use web access to their EMail accounts and I don't think there is a standard way to pass all the data for the EMail to such web based EMail clients.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • AhtiA Ahti

                                      I have a contact tab in my app in which i want to let user type his/her email, subject and body and whenever they click on submit button the mail should be send to my email address. How can i do that ?

                                      O Offline
                                      O Offline
                                      oniongarlic
                                      wrote on last edited by
                                      #32

                                      @Ahti do you have your own public server? If so, then perhaps the most easy way would be to code a simple http API that your app (with somekind of authentication obviously) can do a simple http POST to, and that thing then sends the e-mail to you.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved