Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending email issue.
Qt 6.12 LTS is out! See what's new in the release blog

Sending email issue.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 2.2k 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.
  • jsulmJ jsulm

    @jenya7 How is this related to Qt?
    "What user name and password it expects?" - well, from the mail account from which you want to send mails?

    J Offline
    J Offline
    jenya7
    wrote on last edited by
    #3

    @jsulm
    The only relation - I use a Qt class (smtp). So may be I do something wrong.

    I log with "pass" to "mail_from@gmail.com" and can send an email from it. So "mail_from@gmail.com" and "pass" seems perfectly valid to me.

    jsulmJ 1 Reply Last reply
    0
    • J jenya7

      @jsulm
      The only relation - I use a Qt class (smtp). So may be I do something wrong.

      I log with "pass" to "mail_from@gmail.com" and can send an email from it. So "mail_from@gmail.com" and "pass" seems perfectly valid to me.

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

      @jenya7 said in Sending email issue.:

      I use a Qt class (smtp)

      Smtp is not a Qt class.
      From where did you get it?

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

      J 1 Reply Last reply
      0
      • jsulmJ jsulm

        @jenya7 said in Sending email issue.:

        I use a Qt class (smtp)

        Smtp is not a Qt class.
        From where did you get it?

        J Offline
        J Offline
        jenya7
        wrote on last edited by jenya7
        #5

        @jsulm said in Sending email issue.:

        @jenya7 said in Sending email issue.:

        I use a Qt class (smtp)

        Smtp is not a Qt class.
        From where did you get it?

        I meant - implemented in Qt.

        Smtp::Smtp( const QString &user, const QString &pass, const QString &host, int port, int timeout )
        {    
            socket = new QSslSocket(this);
        
            connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
            connect(socket, SIGNAL(connected()), this, SLOT(connected() ) );
            connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(errorReceived(QAbstractSocket::SocketError)));   
            connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
            connect(socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
        
            this->user = user;
            this->pass = pass;
        
            this->host = host;
            this->port = port;
            this->timeout = timeout;
        }
        
        void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body, QStringList files)
        {
            message = "To: " + to + "\n";    
            message.append("From: " + from + "\n");
            message.append("Subject: " + subject + "\n");
        
            //Let's intitiate multipart MIME with cutting boundary "frontier"
            message.append("MIME-Version: 1.0\n");
            message.append("Content-Type: multipart/mixed; boundary=frontier\n\n");
        
            message.append( "--frontier\n" );
            //message.append( "Content-Type: text/html\n\n" );  //Uncomment this for HTML formating, coment the line below
            message.append( "Content-Type: text/plain\n\n" );
            message.append(body);
            message.append("\n\n");
        
            if(!files.isEmpty())
            {
                qDebug() << "Files to be sent: " << files.size();
                foreach(QString filePath, files)
                {
                    QFile file(filePath);
                    if(file.exists())
                    {
                        if (!file.open(QIODevice::ReadOnly))
                        {
                            qDebug("Couldn't open the file");
                            QMessageBox::warning(nullptr, tr( "Qt Simple SMTP client" ), tr( "Couldn't open the file\n\n" ));
                                return ;
                        }
                        QByteArray bytes = file.readAll();
                        message.append( "--frontier\n" );
                        message.append( "Content-Type: application/octet-stream\nContent-Disposition: attachment; filename="+ QFileInfo(file.fileName()).fileName() +";\nContent-Transfer-Encoding: base64\n\n" );
                        message.append(bytes.toBase64());
                        message.append("\n");
                    }
                }
            }
            else
                qDebug() << "No attachments found";
        
            message.append( "--frontier--\n" );
        
            message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
            message.replace( QString::fromLatin1( "\r\n.\r\n" ),QString::fromLatin1( "\r\n..\r\n" ) );
        
            this->from = from;
            rcpt = to;
            state = Init;
            socket->connectToHostEncrypted(host, static_cast<quint16>(port)); //"smtp.gmail.com" and 465 for gmail TLS
            if (!socket->waitForConnected(timeout)) {
                 qDebug() << socket->errorString();
             }
        
            t = new QTextStream( socket );
        }
        
        jsulmJ 1 Reply Last reply
        0
        • J jenya7

          @jsulm said in Sending email issue.:

          @jenya7 said in Sending email issue.:

          I use a Qt class (smtp)

          Smtp is not a Qt class.
          From where did you get it?

          I meant - implemented in Qt.

          Smtp::Smtp( const QString &user, const QString &pass, const QString &host, int port, int timeout )
          {    
              socket = new QSslSocket(this);
          
              connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
              connect(socket, SIGNAL(connected()), this, SLOT(connected() ) );
              connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(errorReceived(QAbstractSocket::SocketError)));   
              connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
              connect(socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
          
              this->user = user;
              this->pass = pass;
          
              this->host = host;
              this->port = port;
              this->timeout = timeout;
          }
          
          void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body, QStringList files)
          {
              message = "To: " + to + "\n";    
              message.append("From: " + from + "\n");
              message.append("Subject: " + subject + "\n");
          
              //Let's intitiate multipart MIME with cutting boundary "frontier"
              message.append("MIME-Version: 1.0\n");
              message.append("Content-Type: multipart/mixed; boundary=frontier\n\n");
          
              message.append( "--frontier\n" );
              //message.append( "Content-Type: text/html\n\n" );  //Uncomment this for HTML formating, coment the line below
              message.append( "Content-Type: text/plain\n\n" );
              message.append(body);
              message.append("\n\n");
          
              if(!files.isEmpty())
              {
                  qDebug() << "Files to be sent: " << files.size();
                  foreach(QString filePath, files)
                  {
                      QFile file(filePath);
                      if(file.exists())
                      {
                          if (!file.open(QIODevice::ReadOnly))
                          {
                              qDebug("Couldn't open the file");
                              QMessageBox::warning(nullptr, tr( "Qt Simple SMTP client" ), tr( "Couldn't open the file\n\n" ));
                                  return ;
                          }
                          QByteArray bytes = file.readAll();
                          message.append( "--frontier\n" );
                          message.append( "Content-Type: application/octet-stream\nContent-Disposition: attachment; filename="+ QFileInfo(file.fileName()).fileName() +";\nContent-Transfer-Encoding: base64\n\n" );
                          message.append(bytes.toBase64());
                          message.append("\n");
                      }
                  }
              }
              else
                  qDebug() << "No attachments found";
          
              message.append( "--frontier--\n" );
          
              message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
              message.replace( QString::fromLatin1( "\r\n.\r\n" ),QString::fromLatin1( "\r\n..\r\n" ) );
          
              this->from = from;
              rcpt = to;
              state = Init;
              socket->connectToHostEncrypted(host, static_cast<quint16>(port)); //"smtp.gmail.com" and 465 for gmail TLS
              if (!socket->waitForConnected(timeout)) {
                   qDebug() << socket->errorString();
               }
          
              t = new QTextStream( socket );
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @jenya7 said in Sending email issue.:

          Smtp::sendMail

          It looks like you're not using user name and password when sending mails.

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

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @jenya7 said in Sending email issue.:

            Smtp::sendMail

            It looks like you're not using user name and password when sending mails.

            J Offline
            J Offline
            jenya7
            wrote on last edited by jenya7
            #7

            @jsulm said in Sending email issue.:

            @jenya7 said in Sending email issue.:

            Smtp::sendMail

            It looks like you're not using user name and password when sending mails.

            I see. It was advised as a reliable source.

            Do we have any reliable example?

            J 1 Reply Last reply
            0
            • J jenya7

              @jsulm said in Sending email issue.:

              @jenya7 said in Sending email issue.:

              Smtp::sendMail

              It looks like you're not using user name and password when sending mails.

              I see. It was advised as a reliable source.

              Do we have any reliable example?

              J Offline
              J Offline
              jenya7
              wrote on last edited by
              #8

              Hmm...I try this example.
              The same problem. Here

              // Sending command: AUTH PLAIN base64('\0' + username + '\0' + password)
               sendMessage("AUTH PLAIN " + QByteArray().append((char) 0).append(user).append((char) 0).append(password).toBase64());
              
              // Wait for the server's response
              waitForResponse();
              
              // If the response is not 235 then the authentication was faild
               if (responseCode != 235)
               {
                       emit smtpError(AuthenticationFailedError);
                     return false;
               }
              

              I get responseCode = 535.

              J.HilkJ 1 Reply Last reply
              0
              • J jenya7

                Hmm...I try this example.
                The same problem. Here

                // Sending command: AUTH PLAIN base64('\0' + username + '\0' + password)
                 sendMessage("AUTH PLAIN " + QByteArray().append((char) 0).append(user).append((char) 0).append(password).toBase64());
                
                // Wait for the server's response
                waitForResponse();
                
                // If the response is not 235 then the authentication was faild
                 if (responseCode != 235)
                 {
                         emit smtpError(AuthenticationFailedError);
                       return false;
                 }
                

                I get responseCode = 535.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #9

                @jenya7 said in Sending email issue.:

                this example

                IIRC gmail, blocks IMAP ad pop3 by default, you have to log in to your gmail account and enable it in the settings

                • Go to "Settings", e.g. click on the "Gears" icon and select "Settings".
                • Click on "Forwarding and POP/IMAP".
                • Enable "IMAP Access" and/or "POP Download"

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                J 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @jenya7 said in Sending email issue.:

                  this example

                  IIRC gmail, blocks IMAP ad pop3 by default, you have to log in to your gmail account and enable it in the settings

                  • Go to "Settings", e.g. click on the "Gears" icon and select "Settings".
                  • Click on "Forwarding and POP/IMAP".
                  • Enable "IMAP Access" and/or "POP Download"
                  J Offline
                  J Offline
                  jenya7
                  wrote on last edited by
                  #10

                  @J-Hilk
                  email.png

                  This is what I have. Looks OK.

                  J.HilkJ 1 Reply Last reply
                  0
                  • J jenya7

                    @J-Hilk
                    email.png

                    This is what I have. Looks OK.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #11

                    @jenya7 looks good.

                    have you seen this SO topic about response code 535?

                    https://stackoverflow.com/questions/42558903/expected-response-code-250-but-got-code-535-with-message-535-5-7-8-username

                    You can try the solution posted there, see if that helps


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    J 1 Reply Last reply
                    6
                    • J.HilkJ J.Hilk

                      @jenya7 looks good.

                      have you seen this SO topic about response code 535?

                      https://stackoverflow.com/questions/42558903/expected-response-code-250-but-got-code-535-with-message-535-5-7-8-username

                      You can try the solution posted there, see if that helps

                      J Offline
                      J Offline
                      jenya7
                      wrote on last edited by
                      #12

                      @J-Hilk
                      yeah, thank you. "Allow less secure apps" did the magic.

                      1 Reply Last reply
                      1

                      • Login

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