Results 1 to 3 of 3

Thread: How to implement Smtp Protocol

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to implement Smtp Protocol

    Hi, i want to send via smtp an email.
    This is my smtp.cpp
    Qt Code:
    1. #include "smtp.h"
    2. #include <QAbstractSocket>
    3.  
    4. Smtp::Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to, const QString &subject, const QString &body )
    5. {
    6. qDebug()<< state;
    7. socket = new QTcpSocket( this );
    8. connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
    9. connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
    10. connect( socket, SIGNAL( error(QAbstractSocket::SocketError)), this, SLOT(errorReceived(QAbstractSocket::SocketError))) ;
    11. connect( socket, SIGNAL( stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
    12. connect( socket, SIGNAL( disconnected()), this, SLOT(disconnected()));;
    13. connect( socket, SIGNAL( connectionClosed()),this,SLOT(connectionClosed()) );
    14. connect( socket, SIGNAL( aboutToClose()),this,SLOT(aboutToClose()) );
    15. message = "To: " + to + "\n";
    16. message.append("From: " + from + "\n");
    17. message.append("Subject: " + subject + "\n");
    18. message.append(body);
    19. message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    20. message.replace( QString::fromLatin1( "\r\n.\r\n" ),
    21. QString::fromLatin1( "\r\n..\r\n" ) );
    22. this->from = from;
    23. this->subject = subject;
    24. rcpt = to;
    25. t = new QTextStream( socket );
    26. socket->connectToHost( strmailserver , port);
    27.  
    28. if(socket->waitForConnected ( 10000 )){
    29. qDebug("connected");
    30. }
    31. if(socket->waitForReadyRead(30000)){
    32. qDebug("I'm ready");
    33. qDebug()<< socket->state();
    34. qDebug()<< t->device()->canReadLine();
    35. }else{
    36. qDebug("I'm not ready");
    37. qDebug()<< socket->state();
    38. }
    39. }
    40. void Smtp::stateChanged(QAbstractSocket::SocketState socketState)
    41. {
    42.  
    43. qDebug() <<"stateChanged: stateChanged " << socketState;
    44. }
    45.  
    46. void Smtp::errorReceived(QAbstractSocket::SocketError socketError)
    47. {
    48. qDebug() << "errorReceived: error " << socketError;
    49. }
    50.  
    51. void Smtp::disconnected()
    52. {
    53. qDebug() <<"disconnected: " << socket->errorString();
    54. }
    55. void Smtp::connectionClosed(){
    56. qDebug() <<"Connection closed";
    57. }
    58. void Smtp::aboutToClose(){
    59. qDebug() <<"Closing connection";
    60. }
    61.  
    62. void Smtp::connected()
    63. {
    64. qDebug() << "Connected";
    65. }
    66.  
    67. void Smtp::readyRead()
    68. {
    69. qDebug()<< "readyRead: "<<socket->state();
    70. qDebug() <<"Starting to send";
    71.  
    72. QString responseLine;
    73. do
    74. {
    75. responseLine = socket->readLine();
    76. response += responseLine;
    77. }
    78. while ( socket->canReadLine() && responseLine[3] != ' ' );
    79. responseLine.truncate( 3 );
    80.  
    81. qDebug() << "State: " << socket->state() << "\r\n RL: " << responseLine[0];
    82. if ( socket->state() == 3 && responseLine[0] == '2' )
    83. {
    84. qDebug()<< "Before to helo: "<<socket->state();
    85. *t << "EHLO myhost\0\r\n";
    86. qDebug()<< "After: "<<socket->state();
    87. t->flush();
    88. state = Mail;
    89. }
    90. else if ( state == Mail && responseLine[0] == '2' )
    91. {
    92. // HELO response was okay (well, it has to be)
    93.  
    94. *t << "MAIL FROM: <" << from << ">\r\n";
    95. t->flush();
    96. state = Rcpt;
    97. }
    98. else if ( state == Rcpt && responseLine[0] == '2' )
    99. {
    100. *t << "RCPT TO: <" << rcpt << ">\r\n"; //r
    101. t->flush();
    102. state = Data;
    103. }
    104. else if ( state == Data && responseLine[0] == '2' )
    105. {
    106. *t << "DATA\r\n";
    107. t->flush();
    108. state = Body;
    109. }
    110. else if ( state == Body && responseLine[0] == '3' )
    111. {
    112. *t << "Message-ID: My ID";
    113. t->flush();
    114. *t << "From: "<< from <<"\n\r";
    115. t->flush();
    116. *t << "User-Agent: My User Agent" <<"\n\r";
    117. t->flush();
    118. *t << "MIME-Version: 1.0\n\r";
    119. t->flush();
    120. *t << "To: "<< rcpt <<"\r\n";
    121. t->flush();
    122. *t << "Subject: " << subject << "\r\nContent-Type: text/plain; charset=ISO-8859-15; format=flowed\n\rContent-Transfer-Encoding: 7bit\n\r";
    123. t->flush();
    124. *t << message << "\r\n.\r\n";
    125. t->flush();
    126. state = Quit;
    127. }
    128. else if ( state == Quit && responseLine[0] == '2' )
    129. {
    130. *t << "QUIT\r\n";
    131. t->flush();
    132. state = Close;
    133. emit status( tr( "Message sent" ) );
    134. }
    135. else if ( state == Close )
    136. {
    137. deleteLater();
    138. return;
    139. }
    140. else
    141. {
    142. // something broke.
    143. QMessageBox::warning( 0, tr( "Qt Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
    144. state = Close;
    145. }
    146. response = "";
    147. }
    148. Smtp::~Smtp()
    149. {
    150. delete t;
    151. delete socket;
    152. }
    To copy to clipboard, switch view to plain text mode 
    Nothing was sent, this is my last line of the output:

    stateChanged: stateChanged QAbstractSocket::UnconnectedState
    disconnected: "Unknown error"

    Nothig was generated on the network, I used wireshark to see packets sent but my socket only receives the message "220 aa011msr.fastwebnet.it ESMTP Service ready". On the socket the message "EHLO myhost\0\r\n" is not sent.

    This is my smtp.h

    Qt Code:
    1. #ifndef SMTP_H
    2. #define SMTP_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QString>
    6. #include <QTextStream>
    7. #include <QDebug>
    8. #include <QMessageBox>
    9.  
    10. class Smtp : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14.  
    15. public:
    16. Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to,
    17. const QString &subject, const QString &body );
    18. ~Smtp();
    19.  
    20. signals:
    21. void status( const QString &);
    22.  
    23. private slots:
    24. void stateChanged(QAbstractSocket::SocketState socketState);
    25. void errorReceived(QAbstractSocket::SocketError socketError);
    26. void disconnected();
    27. void connectionClosed();
    28. void aboutToClose();
    29. void connected();
    30. void readyRead();
    31.  
    32. private:
    33. QString message;
    34. QTcpSocket *socket;
    35. QString from;
    36. QString subject;
    37. QString rcpt;
    38. QString response;
    39. enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
    40. int state;
    41.  
    42. };
    43. #endif
    To copy to clipboard, switch view to plain text mode 
    Help me plssss!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to implement Smtp Protocol

    Have a look at QwwSmtpClient. It has a few flaws but you should be able to fix them easily.
    http://blog.wysota.eu.org/index.php/...qwwsmtpclient/
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to implement Smtp Protocol

    http://johnwiggins.net/jwsmtp/ is also a good one that I have been using for many years...

Similar Threads

  1. SMTP: how to request confirmation?
    By Raccoon29 in forum General Programming
    Replies: 2
    Last Post: 28th January 2009, 15:41
  2. Replies: 1
    Last Post: 25th July 2008, 17:54
  3. Protocol Implementation. ?
    By dheeraj in forum Qt Programming
    Replies: 32
    Last Post: 9th May 2008, 10:26
  4. QUdpSocket error
    By mdecandia in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2007, 10:47
  5. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 19:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.