home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / network / mail / smtp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  3.3 KB  |  126 lines

  1. /****************************************************************************
  2. ** $Id:  qt/smtp.cpp   3.0.0   edited Oct 2 11:39 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "smtp.h"
  12.  
  13. #include <qtextstream.h>
  14. #include <qsocket.h>
  15. #include <qdns.h>
  16. #include <qtimer.h>
  17. #include <qapplication.h>
  18. #include <qmessagebox.h>
  19. #include <qregexp.h>
  20.  
  21.  
  22. Smtp::Smtp( const QString &from, const QString &to,
  23.         const QString &subject,
  24.         const QString &body )
  25. {
  26.     socket = new QSocket( this );
  27.     connect ( socket, SIGNAL( readyRead() ),
  28.           this, SLOT( readyRead() ) );
  29.     connect ( socket, SIGNAL( connected() ),
  30.           this, SLOT( connected() ) );
  31.  
  32.     mxLookup = new QDns( to.mid( to.find( '@' )+1 ), QDns::Mx );
  33.     connect( mxLookup, SIGNAL(resultsReady()),
  34.          this, SLOT(dnsLookupHelper()) );
  35.  
  36.     message = QString::fromLatin1( "From: " ) + from +
  37.           QString::fromLatin1( "\nTo: " ) + to +
  38.           QString::fromLatin1( "\nSubject: " ) + subject +
  39.           QString::fromLatin1( "\n\n" ) + body + "\n";
  40.     message.replace( QRegExp( QString::fromLatin1( "\n" ) ),
  41.              QString::fromLatin1( "\r\n" ) );
  42.     message.replace( QRegExp( QString::fromLatin1( "\r\n.\r\n" ) ),
  43.              QString::fromLatin1( "\r\n..\r\n" ) );
  44.  
  45.     this->from = from;
  46.     rcpt = to;
  47.  
  48.     state = Init;
  49. }
  50.  
  51.  
  52. Smtp::~Smtp()
  53. {
  54.     delete t;
  55.     delete socket;
  56. }
  57.  
  58.  
  59. void Smtp::dnsLookupHelper()
  60. {
  61.     QValueList<QDns::MailServer> s = mxLookup->mailServers();
  62.     if ( s.isEmpty() && mxLookup->isWorking() )
  63.     return;
  64.  
  65.     emit status( tr( "Connecting to %1" ).arg( s.first().name ) );
  66.  
  67.     socket->connectToHost( s.first().name, 25 );
  68.     t = new QTextStream( socket );
  69. }
  70.  
  71.  
  72. void Smtp::connected()
  73. {
  74.     emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
  75. }
  76.  
  77.  
  78. void Smtp::readyRead()
  79. {
  80.     // SMTP is line-oriented
  81.     if ( !socket->canReadLine() )
  82.     return;
  83.  
  84.     QString responseLine;
  85.     do {
  86.     responseLine = socket->readLine();
  87.     response += responseLine;
  88.     } while( socket->canReadLine() && responseLine[3] != ' ' );
  89.     responseLine.truncate( 3 );
  90.  
  91.     if ( state == Init && responseLine[0] == '2' ) {
  92.     // banner was okay, let's go on
  93.     *t << "HELO there\r\n";
  94.     state = Mail;
  95.     } else if ( state == Mail && responseLine[0] == '2' ) {
  96.     // HELO response was okay (well, it has to be)
  97.     *t << "MAIL FROM: <" << from << ">\r\n";
  98.     state = Rcpt;
  99.     } else if ( state == Rcpt && responseLine[0] == '2' ) {
  100.     *t << "RCPT TO: <" << rcpt << ">\r\n";
  101.     state = Data;
  102.     } else if ( state == Data && responseLine[0] == '2' ) {
  103.     *t << "DATA\r\n";
  104.     state = Body;
  105.     } else if ( state == Body && responseLine[0] == '3' ) {
  106.     *t << message << ".\r\n";
  107.     state = Quit;
  108.     } else if ( state == Quit && responseLine[0] == '2' ) {
  109.     *t << "QUIT\r\n";
  110.     // here, we just close.
  111.     state = Close;
  112.     emit status( tr( "Message sent" ) );
  113.     } else if ( state == Close ) {
  114.     delete this;
  115.     } else {
  116.     // something broke.
  117.     QMessageBox::warning( qApp->activeWindow(),
  118.                   tr( "Qt Mail Example" ),
  119.                   tr( "Unexpected reply from SMTP server:\n\n" ) +
  120.                   response );
  121.     state = Close;
  122.     }
  123.  
  124.     response = "";
  125. }
  126.