title: Send email via Qt
body: <p>i'm trying to send an email via qt i'used this code but i recive this error "4----  "502 5.5.1 Unrecognized command. q9sm1859778wjo.9 - gsmtp\n" and thanks for your help. 
  smrp.h</p>
  
  <pre><code>#include &lt;QtCore&gt;
  #include &lt;QCoreApplication&gt;
  #include &lt;QObject&gt;
  
  #include &lt;QTcpSocket&gt;
  #include &lt;QString&gt;
  #include &lt;QTextStream&gt;
  #include &lt;QDebug&gt;
  #include &lt;QAbstractSocket&gt;
  #include &lt;QDateTime&gt;
  #include &lt;QDate&gt;
  #include &lt;QLocale&gt;
  #include &lt;QObject&gt;
  #include &lt;QTcpSocket&gt;
  
  /*
    USAGE!
    Smtp *newMail  = new Smtp("remoteserver","username","pass");
    newMail-&gt;Send("from@address.com","to@address.com"," Your Subject","My body text");
    class emit ErrorCloseAll() / SuccessQuit()   or get QStringList ErrorMSG; to read error and data....
  
  */
  class Smtp : public QObject
  {
      Q_OBJECT
  
      public:
              Smtp(QString smtphost, QString smtpusername, QString smtppass);
              ~Smtp();
              void setConfig(QString smtphost, QString smtpusername, QString smtppass);
              void send( const QString &amp;from, const QString &amp;to,const QString &amp;subject, const QString &amp;body );
              int linesend;
              QStringList errorMSG;
              QStringList getErrorlist();
      signals:
              void status( const QString &amp;);
              void connectorSuccess();
              void sendLine();
              void errorCloseAll();
              void successQuit();
  
      private slots:
              void disconnected();
              void connected();
              void readLiner();
              void putSendLine();
      private:
              bool isconnect;
              QString smtphost;
              QString smtpusername;
              QString smtppass;
              QString message;
              QString output;
              QString RemoteServerName;
              QString mailstatus;
              QTextStream *t;
              QTcpSocket *smtpsocket;
              QString from;
              QString rcpt;
              QString response;
              QString sendLineAndGrab(QString senddata);
              int Timeout;
              QString encodeBase64( QString xml );
              QString decodeBase64( QString xml );
              int dateswap(QString form, uint unixtime );
                  QString TimeStampMail();
  
  
  };
  #endif
  </code></pre>
  
  <p><em>smtp.cpp</em></p>
  
  <pre><code>#include "smtp.h"
  
  Smtp::Smtp(QString smtphost, QString smtpusername, QString smtppass)
  {
          qDebug() &lt;&lt; "### Class Smtp start";
          this-&gt;smtphost = smtphost;
          this-&gt;smtpusername = smtpusername;
          this-&gt;smtppass = smtppass;
  }
  void Smtp::setConfig(QString smtphost, QString smtpusername, QString smtppass)
  {
          this-&gt;smtphost = smtphost;
          this-&gt;smtpusername = smtpusername;
          this-&gt;smtppass = smtppass;
  }
  
  void Smtp::send( const QString &amp;from, const QString &amp;to, const QString &amp;subject, const QString &amp;body )
  {
          int waittime = 5 * 1000;
          this-&gt;from = from;
          rcpt = to;
          errorMSG.clear();
          Timeout = waittime;
          linesend = 0;
          isconnect = false;
  
          message = TimeStampMail()+"\n";
          message.append("User-Agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716)\n");
          message.append("X-Accept-Language: zh_CN, zh_CN\n");
          message.append("MIME-Version: 1.0\n");
          message.append("To: " + to + "\n");
          message.append("From: "+from+" &lt;" + from + "&gt;\n");
          message.append("Subject: " + subject + "\n");
          message.append("Content-Type: text/html; charset=UTF8;\n");   /* or txt */
          message.append("Content-transfer-encoding: 7BIT\n\n\n\n");
          message.append(body);
          message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
          message.replace( QString::fromLatin1( "\r\n.\r\n" ),QString::fromLatin1( "\r\n..\r\n" ) );
  
          smtpsocket = new QTcpSocket(this);
          connect( this, SIGNAL(connectorSuccess()), this ,SLOT(readLiner()));
          connect( this, SIGNAL(sendLine()), this ,SLOT(putSendLine()));
          if (smtphost.size() &gt; 0)
          {
                  smtpsocket-&gt;connectToHost("smtp.gmail.com",25);
                  qDebug()&lt;&lt;"meh";
          }
          else
          {
              qDebug()&lt;&lt;"mele7";
                  smtpsocket-&gt;connectToHost("localhost",25);
          }
          if (smtpsocket-&gt;waitForConnected(Timeout))
          {
                  if (smtpsocket-&gt;waitForReadyRead(Timeout))
                  {
                          isconnect = true;
                          emit connectorSuccess();
                  }
          }
          else
          {
                  emit errorCloseAll();
              qDebug()&lt;&lt;"---close all ---";
          }
  
  }
  
  void Smtp::readLiner()
  {
          if (isconnect)
          {
                  QTextCodec *codecx;
                  codecx = QTextCodec::codecForMib(106);
                  t = new QTextStream( smtpsocket );
                  t-&gt;setCodec(codecx);
  
                  int loops = 0;
                  while (!t-&gt;atEnd())
                  {
                          loops++;
                          response = t-&gt;readLine();
                  }
                  if (response.size() &gt; 0)
                  {
                          RemoteServerName = response;
                          mailstatus = response.left(3);
                          if (mailstatus == "220")
                          {
                                  response="";
                                  linesend = 1;
                                  emit sendLine();
                          }
                  }
                  else
                  {
                          emit errorCloseAll();
                      qDebug()&lt;&lt;"here";
                  }
          }
  }
  
  Smtp::~Smtp()
  
  {
  }
  
  /* LINE SENDER  */
  void Smtp::putSendLine()
  {
          int current = linesend;
          switch(current)
          {
                  case 1:
                          response = sendLineAndGrab("ehlo " + smtphost);
                          if (response.size() &gt; 0)
                          {
                                  errorMSG.append(response);
                                  linesend = 2;
                                  emit sendLine();
                          }
                          response ="";
                          break;
                  case 2:
                          response = sendLineAndGrab("AUTH LOGIN");
                          if (response.size() &gt;=0)
                          {
                                  errorMSG.append(response);
                                  linesend = 3;
                                  emit sendLine();
                          }
  
                          response ="";
  
                          break;
                  case 3:
                          response = sendLineAndGrab(encodeBase64(smtpusername));   /* username send */
                          if (response.size() &gt; 0)
                          {
                                  errorMSG.append(response);
                                  linesend = 4;
                                  emit sendLine();
                          }
  
                          response ="";
                          break;
                  case 4:
                          response = sendLineAndGrab(encodeBase64(smtppass));     /* pass send */
                         qDebug()&lt;&lt;smtppass;
                          qDebug() &lt;&lt; "4---- " &lt;&lt; response;
                          if (response.size() &gt; 0)
                          {
                                  errorMSG.append(response);
                                  if (response.contains("235", Qt::CaseInsensitive))
                                  {
                                          linesend = 5;
                                          emit sendLine();
                                  }
  
                          }
  
                          response ="";
                          break;
                  case 5:
                          response = sendLineAndGrab("MAIL FROM:&lt;"+from+"&gt;");
                          if (response.size() &gt; 0)
                          {
                                  linesend = 6;
                                  emit sendLine();
                          }
  
                          break;
                  case 6:
                          response = sendLineAndGrab("RCPT TO: &lt;"+rcpt+"&gt;");
                          if (response.size() &gt; 0)
                          {
                                  errorMSG.append(response);
                                  response ="";
                                  response = sendLineAndGrab("DATA");
                                  if (!response.contains("not", Qt::CaseInsensitive))
                                  {
                                          errorMSG.append(response);
                                          response ="";
                                          linesend = 7;
                                          emit sendLine();
                                  }
                          }
                          response ="";
                          break;
                  case 7:
                          response = sendLineAndGrab(message+"\r\n.");
  
                          if (response.size() &amp;&amp; response.contains("ok", Qt::CaseInsensitive) )
                          {
                                  errorMSG.append(response);
                                  linesend = 8;
                                  emit sendLine();
                          }
                          response ="";
                          break;
                  case 8:
                          sendLineAndGrab("QUIT");
                          break;
                  default:
                          /*emit ErrorCloseAll();*/
                          return;
                          break;
          }
  }
  
  /* SENDER AND RECIVER  */
  QString Smtp::sendLineAndGrab(QString senddata)
  {
          QString incommingData = "";
          if (isconnect)
          {
     /////////QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
                  int current = linesend;
                  int loops = 0;
  
                  *t &lt;&lt; senddata &lt;&lt; "\r\n";
                  t-&gt;flush();
                  if (senddata != "QUIT") {
                          if (smtpsocket-&gt;waitForReadyRead(Timeout))
                          {
                                  while (!t-&gt;atEnd())
                                  {
                                          loops++;
                                          QString opera = t-&gt;readLine()+"\n";
                                          incommingData = opera + incommingData;
                                  }
                          }
                  } else
                  {
            ////////QApplication::restoreOverrideCursor();
                          delete smtpsocket;
                          delete t;
                          isconnect = false;
                          emit successQuit();
                          return incommingData;
                  }
      ///////QApplication::restoreOverrideCursor();
          }
          else
          {
                  emit errorCloseAll();
          }
          return incommingData;
  }
  
  void Smtp::disconnected()
  {
          qDebug() &lt;&lt;"disconneted";
          qDebug() &lt;&lt; "error "  &lt;&lt; smtpsocket-&gt;errorString();
  }
  void Smtp::connected()
  {
          output.append("connected");
  }
  
  QString Smtp::encodeBase64( QString xml )
  {
          QByteArray text;
          text.append(xml);
          return text.toBase64();
  }
  
  QString Smtp::decodeBase64( QString xml )
  {
          QByteArray xcode("");;
          xcode.append(xml);
          QByteArray precode(QByteArray::fromBase64(xcode));
          QString notetxt = precode.data();
          return notetxt;
  }
  
  
  QStringList Smtp::getErrorlist()
  {
          return errorMSG;
  }
  
  int Smtp::dateswap(QString form, uint unixtime )
  {
          QDateTime fromunix;
          fromunix.setTime_t(unixtime);
          QString numeric = fromunix.toString((const QString)form);
          bool ok;
          return (int)numeric.toFloat(&amp;ok);
  }
  
  
  QString Smtp::TimeStampMail()
  {
          /* mail rtf Date format! http://www.faqs.org/rfcs/rfc788.html */
          QDateTime timer1( QDateTime::currentDateTime() );
  
  
          uint unixtime = timer1.toTime_t();
          QDateTime fromunix;
          fromunix.setTime_t(unixtime);
  
  
          QStringList RTFdays = QStringList() &lt;&lt; "giorno_NULL" &lt;&lt; "Mon" &lt;&lt; "Tue" &lt;&lt; "Wed" &lt;&lt; "Thu" &lt;&lt; "Fri" &lt;&lt; "Sat" &lt;&lt; "Sun";
          QStringList RTFmonth = QStringList() &lt;&lt; "mese_NULL" &lt;&lt; "Jan" &lt;&lt; "Feb" &lt;&lt; "Mar" &lt;&lt; "Apr" &lt;&lt; "May" &lt;&lt; "Jun" &lt;&lt; "Jul" &lt;&lt; "Aug" &lt;&lt; "Sep" &lt;&lt; "Oct" &lt;&lt; "Nov" &lt;&lt; "Dec";
          QDate timeroad(dateswap("yyyy",unixtime),dateswap("M",unixtime),dateswap("d",unixtime));
  
          QStringList rtfd_line;
          rtfd_line.clear();
          rtfd_line.append("Date: ");
          rtfd_line.append(RTFdays.at(timeroad.dayOfWeek()));
          rtfd_line.append(", ");
          rtfd_line.append(QString::number(dateswap("d",unixtime)));
          rtfd_line.append(" ");
          rtfd_line.append(RTFmonth.at(dateswap("M",unixtime)));
          rtfd_line.append(" ");
          rtfd_line.append(QString::number(dateswap("yyyy",unixtime)));
          rtfd_line.append(" ");
          rtfd_line.append(fromunix.toString("hh:mm:ss"));
          rtfd_line.append(" +0100");
  
          return QString(rtfd_line.join(""));
  }
  </code></pre>