Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to send Email from my application?
QtWS25 Last Chance

How to send Email from my application?

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 6 Posters 4.1k 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.
  • C ChrisW67
    19 Jan 2024, 04:52

    @Long-Vu-Ngoc You can send email from a Qt program by:

    • Communicate directly with an SMTP server to send the email. There are a few Qt SMTP client libraries (under LGPL) that can be found with a simple search.
    • Automating an existing mail client (e.g. Outlook)
    • Execute a child process to send an email. For example, mailx on Linux, a Powershell script, or blat on Windows.

    Your template HTML mail looks like you are intending that recipients click a download button to fetch the file from some external server. That is not the same as attaching the PDF to an email.

    L Offline
    L Offline
    Long Vu Ngoc
    wrote on 19 Jan 2024, 05:28 last edited by
    #3

    @ChrisW67 Thank you for your answer. I will try your suggestion/

    1 Reply Last reply
    0
    • C ChrisW67
      19 Jan 2024, 04:52

      @Long-Vu-Ngoc You can send email from a Qt program by:

      • Communicate directly with an SMTP server to send the email. There are a few Qt SMTP client libraries (under LGPL) that can be found with a simple search.
      • Automating an existing mail client (e.g. Outlook)
      • Execute a child process to send an email. For example, mailx on Linux, a Powershell script, or blat on Windows.

      Your template HTML mail looks like you are intending that recipients click a download button to fetch the file from some external server. That is not the same as attaching the PDF to an email.

      L Offline
      L Offline
      Long Vu Ngoc
      wrote on 19 Jan 2024, 06:49 last edited by Long Vu Ngoc
      #4

      @ChrisW67 Can I automate opening mail client (Outlook) and attach html template in body of email. When recipients click a download button, PDF file that attached also in email will be downloaded. Can I make that?

      1 Reply Last reply
      0
      • C ChrisW67
        19 Jan 2024, 04:52

        @Long-Vu-Ngoc You can send email from a Qt program by:

        • Communicate directly with an SMTP server to send the email. There are a few Qt SMTP client libraries (under LGPL) that can be found with a simple search.
        • Automating an existing mail client (e.g. Outlook)
        • Execute a child process to send an email. For example, mailx on Linux, a Powershell script, or blat on Windows.

        Your template HTML mail looks like you are intending that recipients click a download button to fetch the file from some external server. That is not the same as attaching the PDF to an email.

        L Offline
        L Offline
        Long Vu Ngoc
        wrote on 19 Jan 2024, 07:59 last edited by
        #5

        @ChrisW67 Currently, I use QProcess to run Outlook.exe with command like that to send email

        Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body=Hello
        

        But I don't know how to embedded the html to body of email

        J 1 Reply Last reply 19 Jan 2024, 08:20
        0
        • L Long Vu Ngoc
          19 Jan 2024, 07:59

          @ChrisW67 Currently, I use QProcess to run Outlook.exe with command like that to send email

          Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body=Hello
          

          But I don't know how to embedded the html to body of email

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 19 Jan 2024, 08:20 last edited by
          #6

          @Long-Vu-Ngoc said in How to send Email from my application?:

          But I don't know how to embedded the html to body of email

          Provide it as string to "body" parameter:

          Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
          

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

          L 2 Replies Last reply 19 Jan 2024, 08:39
          0
          • J jsulm
            19 Jan 2024, 08:20

            @Long-Vu-Ngoc said in How to send Email from my application?:

            But I don't know how to embedded the html to body of email

            Provide it as string to "body" parameter:

            Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
            
            L Offline
            L Offline
            Long Vu Ngoc
            wrote on 19 Jan 2024, 08:39 last edited by
            #7

            @jsulm Should I paste html code at here or path to html file

            1 Reply Last reply
            0
            • J jsulm
              19 Jan 2024, 08:20

              @Long-Vu-Ngoc said in How to send Email from my application?:

              But I don't know how to embedded the html to body of email

              Provide it as string to "body" parameter:

              Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
              
              L Offline
              L Offline
              Long Vu Ngoc
              wrote on 19 Jan 2024, 08:49 last edited by
              #8

              @jsulm I do like that

              body = "<!DOCTYPE html>"
                         "<html>"
                              "<head>"
                                  "<title>Example</title>"
                              "<body>"
                                  " <p>This is an example of a simple HTML page with one paragraph.</p>"
                              "</body>"
                          "</html>"
                  ;
              

              and that is body of the email
              b49705dc-a6cc-4e9b-8ed1-9a7fe0818e6c-image.png

              J 1 Reply Last reply 19 Jan 2024, 14:21
              0
              • L Long Vu Ngoc referenced this topic on 19 Jan 2024, 14:14
              • L Long Vu Ngoc
                19 Jan 2024, 08:49

                @jsulm I do like that

                body = "<!DOCTYPE html>"
                           "<html>"
                                "<head>"
                                    "<title>Example</title>"
                                "<body>"
                                    " <p>This is an example of a simple HTML page with one paragraph.</p>"
                                "</body>"
                            "</html>"
                    ;
                

                and that is body of the email
                b49705dc-a6cc-4e9b-8ed1-9a7fe0818e6c-image.png

                J Offline
                J Offline
                JonB
                wrote on 19 Jan 2024, 14:21 last edited by JonB
                #9

                @Long-Vu-Ngoc
                And what is your question? Does something not work? Does it send email? Does email arrive but is not right? You don't say, so how should we know?

                The HTML you have posted is incorrect/malformed, so I would not expect it to work. Make it correct before you try to send it.

                L 1 Reply Last reply 19 Jan 2024, 14:31
                0
                • J JonB
                  19 Jan 2024, 14:21

                  @Long-Vu-Ngoc
                  And what is your question? Does something not work? Does it send email? Does email arrive but is not right? You don't say, so how should we know?

                  The HTML you have posted is incorrect/malformed, so I would not expect it to work. Make it correct before you try to send it.

                  L Offline
                  L Offline
                  Long Vu Ngoc
                  wrote on 19 Jan 2024, 14:31 last edited by
                  #10

                  @JonB Hi
                  I want to use Qt to launch Outlook and attach html file in the body of email
                  As I commented, I use QProcess to run Outlook.exe and use command to attach html file in body of the email

                  Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
                  

                  As @jsulm suggeset, I paste my html code in body. Sorry I don't know about html, I have just learned it.
                  Outlook already launch but the body email don't show html file.

                  J 1 Reply Last reply 19 Jan 2024, 14:49
                  0
                  • L Long Vu Ngoc
                    19 Jan 2024, 14:31

                    @JonB Hi
                    I want to use Qt to launch Outlook and attach html file in the body of email
                    As I commented, I use QProcess to run Outlook.exe and use command to attach html file in body of the email

                    Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
                    

                    As @jsulm suggeset, I paste my html code in body. Sorry I don't know about html, I have just learned it.
                    Outlook already launch but the body email don't show html file.

                    J Offline
                    J Offline
                    JonB
                    wrote on 19 Jan 2024, 14:49 last edited by
                    #11

                    @Long-Vu-Ngoc
                    I still don't understand whether you are saying that if you send non-HTML, like you show here ...&body="HERE YOUR HTML", that works? But if you send where you showed body = "<!DOCTYPE html>" ... that does not work? It arrives but is incorrect? It does not arrive? I don't know what you are saying.

                    If you are trying to send the HTML body you show in body = "<!DOCTYPE html>" ... then you need to start by correcting it. Where did you get that string from, it is not right? It has a <head> start tag but no </head> matching end tag, so it is invalid HTML, and I don't know what Outlook will do with it.

                    L 1 Reply Last reply 19 Jan 2024, 15:02
                    0
                    • J JonB
                      19 Jan 2024, 14:49

                      @Long-Vu-Ngoc
                      I still don't understand whether you are saying that if you send non-HTML, like you show here ...&body="HERE YOUR HTML", that works? But if you send where you showed body = "<!DOCTYPE html>" ... that does not work? It arrives but is incorrect? It does not arrive? I don't know what you are saying.

                      If you are trying to send the HTML body you show in body = "<!DOCTYPE html>" ... then you need to start by correcting it. Where did you get that string from, it is not right? It has a <head> start tag but no </head> matching end tag, so it is invalid HTML, and I don't know what Outlook will do with it.

                      L Offline
                      L Offline
                      Long Vu Ngoc
                      wrote on 19 Jan 2024, 15:02 last edited by Long Vu Ngoc
                      #12

                      @JonB I just find a example html code in the internet. I try another but it still don't show html in the body of the email. It just show a text like that
                      bb80e4d4-af99-48b7-afa5-b18d4981c69d-image.png

                      J 1 Reply Last reply 19 Jan 2024, 15:13
                      0
                      • L Long Vu Ngoc
                        19 Jan 2024, 15:02

                        @JonB I just find a example html code in the internet. I try another but it still don't show html in the body of the email. It just show a text like that
                        bb80e4d4-af99-48b7-afa5-b18d4981c69d-image.png

                        J Offline
                        J Offline
                        JonB
                        wrote on 19 Jan 2024, 15:13 last edited by
                        #13

                        @Long-Vu-Ngoc
                        At least your new example is correct HTML now. I don't know where you copied the previous HTML you had from.
                        In Outlook you have a setting as to whether to display emails as plain text or HTML. Have you set that to allow it to show as HTML?

                        L 1 Reply Last reply 19 Jan 2024, 15:21
                        0
                        • J JonB
                          19 Jan 2024, 15:13

                          @Long-Vu-Ngoc
                          At least your new example is correct HTML now. I don't know where you copied the previous HTML you had from.
                          In Outlook you have a setting as to whether to display emails as plain text or HTML. Have you set that to allow it to show as HTML?

                          L Offline
                          L Offline
                          Long Vu Ngoc
                          wrote on 19 Jan 2024, 15:21 last edited by
                          #14

                          @JonB I already set that but it still not work.
                          ca29604d-57d6-46b8-ab7b-ec373f4d72ec-image.png
                          8aa96929-319f-4e32-9e0b-4b9b0e54174c-image.png

                          J 1 Reply Last reply 19 Jan 2024, 15:38
                          0
                          • L Long Vu Ngoc
                            19 Jan 2024, 15:21

                            @JonB I already set that but it still not work.
                            ca29604d-57d6-46b8-ab7b-ec373f4d72ec-image.png
                            8aa96929-319f-4e32-9e0b-4b9b0e54174c-image.png

                            J Offline
                            J Offline
                            JonB
                            wrote on 19 Jan 2024, 15:38 last edited by
                            #15

                            @Long-Vu-Ngoc
                            That is actually the setting for whether to send messages in HTML. I thought there was one for whether to display in HTML received messages, but maybe not.

                            I have never used a command line like

                            Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
                            

                            It is possible that this body=... argument automatically means send as text not HTML, I don't know.

                            On the message received in the inbox, can you display the source and see what it looks like. I think you right-click in an empty area of the body when showing a message and select View Source. Let's see whether it looks like it has sent any HTML.

                            L 1 Reply Last reply 19 Jan 2024, 16:11
                            0
                            • J JonB
                              19 Jan 2024, 15:38

                              @Long-Vu-Ngoc
                              That is actually the setting for whether to send messages in HTML. I thought there was one for whether to display in HTML received messages, but maybe not.

                              I have never used a command line like

                              Outlook.exe /c ipm.note /m user@contoso.com?subject=Test&body="HERE YOUR HTML"
                              

                              It is possible that this body=... argument automatically means send as text not HTML, I don't know.

                              On the message received in the inbox, can you display the source and see what it looks like. I think you right-click in an empty area of the body when showing a message and select View Source. Let's see whether it looks like it has sent any HTML.

                              L Offline
                              L Offline
                              Long Vu Ngoc
                              wrote on 19 Jan 2024, 16:11 last edited by
                              #16

                              @JonB said in How to send Email from my application?:

                              It is possible that this body=... argument automatically means send as text not HTML, I don't know.

                              You said right. I check source and it is Plain text,
                              Here is source: </head>
                              <body lang="EN-US" link="#0563C1" vlink="#954F72">
                              <div class="WordSection1">
                              <p class="MsoPlainText"><html><head></head><body>Hello World<h1>Hello World</h1><p>Hello World</p></body></html><o:p></o:p></p>
                              </div>
                              <br>
                              <span style="font-size: 8pt; font-family: Arial, sans-serif, serif, EmojiFont; color: rgb(115, 115, 115);"><i>***********************************************************************
                              <br>

                              J 1 Reply Last reply 19 Jan 2024, 16:16
                              0
                              • L Long Vu Ngoc
                                19 Jan 2024, 16:11

                                @JonB said in How to send Email from my application?:

                                It is possible that this body=... argument automatically means send as text not HTML, I don't know.

                                You said right. I check source and it is Plain text,
                                Here is source: </head>
                                <body lang="EN-US" link="#0563C1" vlink="#954F72">
                                <div class="WordSection1">
                                <p class="MsoPlainText"><html><head></head><body>Hello World<h1>Hello World</h1><p>Hello World</p></body></html><o:p></o:p></p>
                                </div>
                                <br>
                                <span style="font-size: 8pt; font-family: Arial, sans-serif, serif, EmojiFont; color: rgb(115, 115, 115);"><i>***********************************************************************
                                <br>

                                J Offline
                                J Offline
                                JonB
                                wrote on 19 Jan 2024, 16:16 last edited by
                                #17

                                @Long-Vu-Ngoc
                                Yeah. I did have a Google around a looked quite a bit, not one example of this command line being used to send HTML (as opposed to plain) text, and not one person asking.

                                IIRC, when you type into Outlook UI for rich text it does something like sends a message which has both HTML and plain text versions embedded in it, as "alternatives". For whatever reason I'm thinking you can't make it send as HTML from command-line body=... argument.

                                L 1 Reply Last reply 19 Jan 2024, 16:29
                                0
                                • J JonB
                                  19 Jan 2024, 16:16

                                  @Long-Vu-Ngoc
                                  Yeah. I did have a Google around a looked quite a bit, not one example of this command line being used to send HTML (as opposed to plain) text, and not one person asking.

                                  IIRC, when you type into Outlook UI for rich text it does something like sends a message which has both HTML and plain text versions embedded in it, as "alternatives". For whatever reason I'm thinking you can't make it send as HTML from command-line body=... argument.

                                  L Offline
                                  L Offline
                                  Long Vu Ngoc
                                  wrote on 19 Jan 2024, 16:29 last edited by
                                  #18

                                  @JonB Do you have any suggestions for that? I want to open a mail app (outlook) and attach the html file in the body

                                  J C 2 Replies Last reply 19 Jan 2024, 16:33
                                  0
                                  • L Long Vu Ngoc
                                    19 Jan 2024, 16:29

                                    @JonB Do you have any suggestions for that? I want to open a mail app (outlook) and attach the html file in the body

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 19 Jan 2024, 16:33 last edited by JonB
                                    #19

                                    @Long-Vu-Ngoc
                                    If you really wanted to do this, given we can't find how to do it you would need to use VBA. Google if you don't know this. Qt has QAxObject as entry point to this. It's going to be a lot more code. Or you may be able to run Powershell as external command passing it parameters, that allows you to access this level.

                                    I would be looking at sending via SMTP instead, as @ChrisW67 mentioned above. But that too is some work, and you need to connect to an SMTP server available to you.

                                    1 Reply Last reply
                                    0
                                    • L Long Vu Ngoc
                                      19 Jan 2024, 16:29

                                      @JonB Do you have any suggestions for that? I want to open a mail app (outlook) and attach the html file in the body

                                      C Offline
                                      C Offline
                                      ChrisW67
                                      wrote on 19 Jan 2024, 22:00 last edited by ChrisW67
                                      #20

                                      @Long-Vu-Ngoc. @JonB

                                      The mailto: syntax used in the Outlook command line allows only for plain text messages. That syntax predates HTML/rich text email by a long margin.

                                      To send an HTML body via Outlook you could use a variation of the Powershell script found here. Something like:

                                      # File blah.ps1
                                      param (
                                          [string]$to = "default@default.com",
                                          [string]$cc = "default@default.com",
                                          [string]$subject = "default subject",
                                          [string]$body = "body content",
                                          [switch]$html = $false
                                      )
                                      
                                      $ol = New-Object -comObject Outlook.Application
                                      
                                      $mail = $ol.CreateItem(0)
                                      $mail.To = $to
                                      $mail.Cc = $cc
                                      $mail.Subject = $subject
                                      if($html) {
                                          $mail.HTMLBody = $body
                                      } else {
                                          $mail.Body = $body
                                      }
                                      
                                      $mail.Send
                                      

                                      Launch Powershell using QProcess in much the same sort of way. IMHO all emails should contain a usable text-only part along with any rich text format. Outlook's interface seems to actively fight this (setting HTMLBody mangles Body).

                                      Edit: If you are going to use Powershell then you should look at Send-MailMessage. This avoids Outlook client altogether, but requires an SMTP server (which may be a Microsoft mail server).

                                      You may have to deal with restrictions on running Powershell scripts.

                                      Have a look at Qutlook example to see how you might automate the application more directly.

                                      1 Reply Last reply
                                      1
                                      • L Long Vu Ngoc
                                        19 Jan 2024, 04:00

                                        I have a Pdf fle in my application. I want to send this file by email when user press send via email button in my app. It will send a Email Notification like that.
                                        e84d8f51-df3a-4d05-8d58-b6c0488c3c1a-image.png
                                        Do you have any idea to do that?
                                        Thank you!

                                        Ronel_qtmasterR Offline
                                        Ronel_qtmasterR Offline
                                        Ronel_qtmaster
                                        wrote on 20 Jan 2024, 10:30 last edited by
                                        #21

                                        @Long-Vu-Ngoc it is simple.sendind emails is through the SMTP protocol

                                        1.Go a read about the smtp protocol and commands
                                        2.Create a Tcp socket and connect to the smtp server
                                        3.Write hello to the server and see if the connection is successful.
                                        4. In your mail account you should grant access of your application
                                        5. Use Switch structure to Switch between the differences responses of the server, in order to now which info you should send( title, body, attachements etc...)

                                        this is an exemple of code i used for tests.However it is not complete.Hope that it helps.

                                        main.cpp
                                        #include <QCoreApplication>
                                        #include <QDebug>
                                        #include <QString>
                                        #include <iostream>
                                        #include <QHostAddress>
                                        #include <QTextStream>
                                        #include "smtp.h"

                                        QString command;
                                        QString password;
                                        QString email;
                                        QString host;
                                        qint16 port;
                                        using namespace std;
                                        int main(int argc, char *argv[])
                                        {

                                        QCoreApplication a(argc, argv);
                                        smtp client;
                                        command ="";
                                        qDebug()<<"----------------------";
                                        qDebug()<<"MailSender 2.0\r\n";
                                        qDebug()<<"----------------------";
                                        qDebug()<<"serverName: stmp.mail.yahoo.com";
                                        qDebug()<<"port: 25";
                                        qDebug()<<"CommandList -->";
                                        qDebug()<<"HELO";
                                         qDebug () <<"EHLO";
                                        qDebug()<<"AUTH LOGIN";
                                        qDebug()<<"MAIL FROM";
                                        qDebug () <<"RCPT TO";
                                         qDebug () <<"TURN";
                                          qDebug () <<"ATRN";
                                           qDebug () <<"SIZE";
                                            qDebug () <<"ETRN";
                                           qDebug () <<"PIPELINING";
                                            qDebug () <<"CHUNKING";
                                             qDebug () <<"DATA";
                                            qDebug () <<"DSN";
                                             qDebug () <<"RSET";
                                              qDebug () <<"VRFY";
                                               qDebug () <<"HELP";
                                        qDebug () <<"QUIT";
                                        qDebug()<<"----------------------";
                                        client.socket->connectToHost("smtp.mail.yahoo.com", 25);
                                        qDebug()<<"connecting";
                                        
                                        return a.exec();
                                        

                                        }
                                        smtp.h

                                        #ifndef SMTP_H
                                        #define SMTP_H

                                        #include <QObject>
                                        #include <QTcpSocket>

                                        class smtp : public QObject
                                        {
                                        Q_OBJECT

                                        public:
                                        explicit smtp(QObject *parent = 0);
                                        ~smtp();
                                        QTcpSocket *socket;
                                        QString last;
                                        int state;
                                        int c;
                                        enum states{
                                        HELLO,
                                        AUTH_LOGIN,
                                        USER,
                                        PASSWORD

                                        };
                                        protected:

                                        private slots:
                                        void stateChanged(QAbstractSocket::SocketState socketState);
                                        void errorReceived(QAbstractSocket::SocketError socketError);

                                        void disconnected();
                                        void connected();
                                        void readyread();
                                        void enteraCommand(QString instruction);
                                        

                                        };

                                        #endif // SMTP_H

                                        smtp.cpp

                                        #include "smtp.h"
                                        #include <QTextStream>
                                        #include <QString>
                                        #include <QTimer>
                                        #include <QTime>
                                        #include <stdio.h>
                                        #include <iostream>
                                        #include <QTime>
                                        #include <QHostAddress>

                                        using namespace std;
                                        QString c;
                                        smtp::smtp(QObject *parent) :
                                        QObject(parent)
                                        {
                                        socket = new QTcpSocket(this);
                                        connect(socket,SIGNAL(connected()), this, SLOT(connected()));
                                        connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
                                        connect(socket,SIGNAL(readyRead()), this, SLOT(readyread()));
                                        connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(stateChanged(QAbstractSocket::SocketState)));
                                        connect(socket,SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorReceived(QAbstractSocket::SocketError)));
                                        last = "";
                                        state = HELLO;
                                        c =0;

                                        }

                                        smtp::~smtp()
                                        {

                                        }

                                        void smtp::connected()
                                        {
                                        qDebug()<<"connected";
                                        socket->write("HELO yahoo.com\r\n");

                                        }
                                        void smtp::disconnected()
                                        {
                                        qDebug()<<"disconnected";
                                        qDebug()<<socket->errorString();

                                        qDebug()<<"1= Try again";
                                        
                                        
                                        QString response;
                                        QTextStream stream(stdin);
                                        stream >> response;
                                        if(response == "1")
                                        {
                                            socket->connectToHost("smtp.mail.yahoo.com", 25);
                                            qDebug() <<"connecting to smtp.mail.yahoo.com";
                                        
                                        }
                                        else
                                        {
                                            qDebug()<<"invalid command";
                                        
                                        }
                                        

                                        }
                                        void smtp::readyread()
                                        {
                                        while (!socket->atEnd()) {
                                        QString line = socket->readLine();
                                        qDebug()<< line;
                                        if(line.startsWith("220"))
                                        {
                                        QString instruction = "enter the command AUTH LOGIN";
                                        enteraCommand(instruction);
                                        }
                                        else if(line.startsWith("250"))
                                        {
                                        qDebug()<<line;
                                        enteraCommand("entrez une commande");
                                        }
                                        else
                                        if(line.startsWith("334"))
                                        {
                                        if(c == 1)
                                        {
                                        enteraCommand("password");
                                        }
                                        else
                                        {
                                        enteraCommand("email");
                                        }
                                        }
                                        else
                                        {
                                        qDebug()<<line;
                                        enteraCommand("entrez une commande");
                                        }

                                        }
                                        }

                                        void smtp::stateChanged(QAbstractSocket::SocketState socketState)
                                        {
                                        qDebug()<<socketState;
                                        }
                                        void smtp::errorReceived(QAbstractSocket::SocketError socketError)
                                        {
                                        qDebug()<< socketError;

                                        }
                                        void smtp::enteraCommand(QString instruction)
                                        {
                                        QString command;
                                        qDebug()<<instruction;
                                        QTextStream stream(stdin);
                                        stream >> command;
                                        if(instruction == "password" || instruction == "email")
                                        {
                                        socket->write(command.toLatin1().toBase64());
                                        }else
                                        {
                                        socket->write(command.toLatin1());
                                        }

                                        }

                                        smtp.ui

                                        <?xml version='1.0'?>
                                        <ui version="4.0">
                                        <author/>
                                        <comment/>
                                        <exportmacro/>
                                        <class>smtp</class>
                                        <widget name="smtp" class="QWidget">
                                        <property name="geometry">
                                        <rect>
                                        <x>0</x>
                                        <y>0</y>
                                        <width>640</width>
                                        <height>480</height>
                                        </rect>
                                        </property>
                                        <property name="windowTitle">
                                        <string>Form</string>
                                        </property>
                                        </widget>
                                        <pixmapfunction/>
                                        <connections/>
                                        </ui>

                                        smtp.pro

                                        QT += core network
                                        QT -= gui

                                        CONFIG += c++11

                                        TARGET = mailSender_conso
                                        CONFIG += console
                                        CONFIG -= app_bundle

                                        TEMPLATE = app

                                        SOURCES += main.cpp
                                        smtp.cpp

                                        HEADERS +=
                                        smtp.h

                                        J 1 Reply Last reply 20 Jan 2024, 13:52
                                        0
                                        • Ronel_qtmasterR Ronel_qtmaster
                                          20 Jan 2024, 10:30

                                          @Long-Vu-Ngoc it is simple.sendind emails is through the SMTP protocol

                                          1.Go a read about the smtp protocol and commands
                                          2.Create a Tcp socket and connect to the smtp server
                                          3.Write hello to the server and see if the connection is successful.
                                          4. In your mail account you should grant access of your application
                                          5. Use Switch structure to Switch between the differences responses of the server, in order to now which info you should send( title, body, attachements etc...)

                                          this is an exemple of code i used for tests.However it is not complete.Hope that it helps.

                                          main.cpp
                                          #include <QCoreApplication>
                                          #include <QDebug>
                                          #include <QString>
                                          #include <iostream>
                                          #include <QHostAddress>
                                          #include <QTextStream>
                                          #include "smtp.h"

                                          QString command;
                                          QString password;
                                          QString email;
                                          QString host;
                                          qint16 port;
                                          using namespace std;
                                          int main(int argc, char *argv[])
                                          {

                                          QCoreApplication a(argc, argv);
                                          smtp client;
                                          command ="";
                                          qDebug()<<"----------------------";
                                          qDebug()<<"MailSender 2.0\r\n";
                                          qDebug()<<"----------------------";
                                          qDebug()<<"serverName: stmp.mail.yahoo.com";
                                          qDebug()<<"port: 25";
                                          qDebug()<<"CommandList -->";
                                          qDebug()<<"HELO";
                                           qDebug () <<"EHLO";
                                          qDebug()<<"AUTH LOGIN";
                                          qDebug()<<"MAIL FROM";
                                          qDebug () <<"RCPT TO";
                                           qDebug () <<"TURN";
                                            qDebug () <<"ATRN";
                                             qDebug () <<"SIZE";
                                              qDebug () <<"ETRN";
                                             qDebug () <<"PIPELINING";
                                              qDebug () <<"CHUNKING";
                                               qDebug () <<"DATA";
                                              qDebug () <<"DSN";
                                               qDebug () <<"RSET";
                                                qDebug () <<"VRFY";
                                                 qDebug () <<"HELP";
                                          qDebug () <<"QUIT";
                                          qDebug()<<"----------------------";
                                          client.socket->connectToHost("smtp.mail.yahoo.com", 25);
                                          qDebug()<<"connecting";
                                          
                                          return a.exec();
                                          

                                          }
                                          smtp.h

                                          #ifndef SMTP_H
                                          #define SMTP_H

                                          #include <QObject>
                                          #include <QTcpSocket>

                                          class smtp : public QObject
                                          {
                                          Q_OBJECT

                                          public:
                                          explicit smtp(QObject *parent = 0);
                                          ~smtp();
                                          QTcpSocket *socket;
                                          QString last;
                                          int state;
                                          int c;
                                          enum states{
                                          HELLO,
                                          AUTH_LOGIN,
                                          USER,
                                          PASSWORD

                                          };
                                          protected:

                                          private slots:
                                          void stateChanged(QAbstractSocket::SocketState socketState);
                                          void errorReceived(QAbstractSocket::SocketError socketError);

                                          void disconnected();
                                          void connected();
                                          void readyread();
                                          void enteraCommand(QString instruction);
                                          

                                          };

                                          #endif // SMTP_H

                                          smtp.cpp

                                          #include "smtp.h"
                                          #include <QTextStream>
                                          #include <QString>
                                          #include <QTimer>
                                          #include <QTime>
                                          #include <stdio.h>
                                          #include <iostream>
                                          #include <QTime>
                                          #include <QHostAddress>

                                          using namespace std;
                                          QString c;
                                          smtp::smtp(QObject *parent) :
                                          QObject(parent)
                                          {
                                          socket = new QTcpSocket(this);
                                          connect(socket,SIGNAL(connected()), this, SLOT(connected()));
                                          connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
                                          connect(socket,SIGNAL(readyRead()), this, SLOT(readyread()));
                                          connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),this, SLOT(stateChanged(QAbstractSocket::SocketState)));
                                          connect(socket,SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorReceived(QAbstractSocket::SocketError)));
                                          last = "";
                                          state = HELLO;
                                          c =0;

                                          }

                                          smtp::~smtp()
                                          {

                                          }

                                          void smtp::connected()
                                          {
                                          qDebug()<<"connected";
                                          socket->write("HELO yahoo.com\r\n");

                                          }
                                          void smtp::disconnected()
                                          {
                                          qDebug()<<"disconnected";
                                          qDebug()<<socket->errorString();

                                          qDebug()<<"1= Try again";
                                          
                                          
                                          QString response;
                                          QTextStream stream(stdin);
                                          stream >> response;
                                          if(response == "1")
                                          {
                                              socket->connectToHost("smtp.mail.yahoo.com", 25);
                                              qDebug() <<"connecting to smtp.mail.yahoo.com";
                                          
                                          }
                                          else
                                          {
                                              qDebug()<<"invalid command";
                                          
                                          }
                                          

                                          }
                                          void smtp::readyread()
                                          {
                                          while (!socket->atEnd()) {
                                          QString line = socket->readLine();
                                          qDebug()<< line;
                                          if(line.startsWith("220"))
                                          {
                                          QString instruction = "enter the command AUTH LOGIN";
                                          enteraCommand(instruction);
                                          }
                                          else if(line.startsWith("250"))
                                          {
                                          qDebug()<<line;
                                          enteraCommand("entrez une commande");
                                          }
                                          else
                                          if(line.startsWith("334"))
                                          {
                                          if(c == 1)
                                          {
                                          enteraCommand("password");
                                          }
                                          else
                                          {
                                          enteraCommand("email");
                                          }
                                          }
                                          else
                                          {
                                          qDebug()<<line;
                                          enteraCommand("entrez une commande");
                                          }

                                          }
                                          }

                                          void smtp::stateChanged(QAbstractSocket::SocketState socketState)
                                          {
                                          qDebug()<<socketState;
                                          }
                                          void smtp::errorReceived(QAbstractSocket::SocketError socketError)
                                          {
                                          qDebug()<< socketError;

                                          }
                                          void smtp::enteraCommand(QString instruction)
                                          {
                                          QString command;
                                          qDebug()<<instruction;
                                          QTextStream stream(stdin);
                                          stream >> command;
                                          if(instruction == "password" || instruction == "email")
                                          {
                                          socket->write(command.toLatin1().toBase64());
                                          }else
                                          {
                                          socket->write(command.toLatin1());
                                          }

                                          }

                                          smtp.ui

                                          <?xml version='1.0'?>
                                          <ui version="4.0">
                                          <author/>
                                          <comment/>
                                          <exportmacro/>
                                          <class>smtp</class>
                                          <widget name="smtp" class="QWidget">
                                          <property name="geometry">
                                          <rect>
                                          <x>0</x>
                                          <y>0</y>
                                          <width>640</width>
                                          <height>480</height>
                                          </rect>
                                          </property>
                                          <property name="windowTitle">
                                          <string>Form</string>
                                          </property>
                                          </widget>
                                          <pixmapfunction/>
                                          <connections/>
                                          </ui>

                                          smtp.pro

                                          QT += core network
                                          QT -= gui

                                          CONFIG += c++11

                                          TARGET = mailSender_conso
                                          CONFIG += console
                                          CONFIG -= app_bundle

                                          TEMPLATE = app

                                          SOURCES += main.cpp
                                          smtp.cpp

                                          HEADERS +=
                                          smtp.h

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 20 Jan 2024, 13:52 last edited by
                                          #22

                                          @Ronel_qtmaster
                                          Hi there. Your code may be fine, but would benefit from enclosing inside forum's Code tags (icon </> when posting, or ``` (3 backticks) above & below code block.

                                          The problem for the OP is that (a) he needs to have some external email account to do this and (b) your code may work with smtp.mail.yahoo.com but, say, gmail's one long since stopped users from doing this and require user to be logged in or use some other form of authentication before they allow it these days. It's only fair to bring this to OP's attention.

                                          V 1 Reply Last reply 21 Jan 2024, 10:08
                                          0

                                          12/25

                                          19 Jan 2024, 15:02

                                          • Login

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