From d796c9dd933ab96ec83b9a634feedd5d32e1ba3f Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 8 Nov 2011 12:31:36 -0600 Subject: Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731 --- doc/html/mail-example.html | 243 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 doc/html/mail-example.html (limited to 'doc/html/mail-example.html') diff --git a/doc/html/mail-example.html b/doc/html/mail-example.html new file mode 100644 index 000000000..2dc1971ff --- /dev/null +++ b/doc/html/mail-example.html @@ -0,0 +1,243 @@ + + + + + +A simple mail client + + + + + + + +
+ +Home + | +All Classes + | +Main Classes + | +Annotated + | +Grouped Classes + | +Functions +

A simple mail client

+ + +

+

This example shows how to use the TQSocket class. The client can only be +used to send mails. The interesting part is the implementation of the +SMTP protocol. +


+

Header file (smtp.h): +

/****************************************************************************
+** $Id: qt/smtp.h   3.3.8   edited Jan 11 14:37 $
+**
+** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
+**
+** This file is part of an example program for TQt.  This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef SMTP_H
+#define SMTP_H
+
+#include <qobject.h>
+#include <qstring.h>
+
+class TQSocket;
+class TQTextStream;
+class TQDns;
+
+class Smtp : public TQObject
+{
+    Q_OBJECT
+
+public:
+    Smtp( const TQString &from, const TQString &to,
+          const TQString &subject, const TQString &body );
+    ~Smtp();
+
+signals:
+    void status( const TQString & );
+
+private slots:
+    void dnsLookupHelper();
+    void readyRead();
+    void connected();
+
+private:
+    enum State {
+        Init,
+        Mail,
+        Rcpt,
+        Data,
+        Body,
+        Quit,
+        Close
+    };
+
+    TQString message;
+    TQString from;
+    TQString rcpt;
+    TQSocket *socket;
+    TQTextStream * t;
+    int state;
+    TQString response;
+    TQDns * mxLookup;
+};
+
+#endif
+
+ +


+

Implementation (smtp.cpp): +

/****************************************************************************
+** $Id: qt/smtp.cpp   3.3.8   edited Jan 11 14:37 $
+**
+** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
+**
+** This file is part of an example program for TQt.  This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "smtp.h"
+
+#include <qtextstream.h>
+#include <qsocket.h>
+#include <qdns.h>
+#include <qtimer.h>
+#include <qapplication.h>
+#include <qmessagebox.h>
+#include <qregexp.h>
+
+
+Smtp::Smtp( const TQString &from, const TQString &to,
+            const TQString &subject,
+            const TQString &body )
+{
+    socket = new TQSocket( this );
+    connect ( socket, SIGNAL( readyRead() ),
+              this, SLOT( readyRead() ) );
+    connect ( socket, SIGNAL( connected() ),
+              this, SLOT( connected() ) );
+
+    mxLookup = new TQDns( to.mid( to.find( '@' )+1 ), TQDns::Mx );
+    connect( mxLookup, SIGNAL(resultsReady()),
+             this, SLOT(dnsLookupHelper()) );
+
+    message = TQString::fromLatin1( "From: " ) + from +
+              TQString::fromLatin1( "\nTo: " ) + to +
+              TQString::fromLatin1( "\nSubject: " ) + subject +
+              TQString::fromLatin1( "\n\n" ) + body + "\n";
+    message.replace( TQString::fromLatin1( "\n" ),
+                     TQString::fromLatin1( "\r\n" ) );
+    message.replace( TQString::fromLatin1( "\r\n.\r\n" ),
+                     TQString::fromLatin1( "\r\n..\r\n" ) );
+
+    this->from = from;
+    rcpt = to;
+
+    state = Init;
+}
+
+
+Smtp::~Smtp()
+{
+    delete t;
+    delete socket;
+}
+
+
+void Smtp::dnsLookupHelper()
+{
+    TQValueList<TQDns::MailServer> s = mxLookup->mailServers();
+    if ( s.isEmpty() ) {
+        if ( !mxLookup->isWorking() )
+            emit status( tr( "Error in MX record lookup" ) );
+        return;
+    }
+
+    emit status( tr( "Connecting to %1" ).arg( s.first().name ) );
+
+    socket->connectToHost( s.first().name, 25 );
+    t = new TQTextStream( socket );
+}
+
+
+void Smtp::connected()
+{
+    emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
+}
+
+void Smtp::readyRead()
+{
+    // SMTP is line-oriented
+    if ( !socket->canReadLine() )
+        return;
+
+    TQString responseLine;
+    do {
+        responseLine = socket->readLine();
+        response += responseLine;
+    } while( socket->canReadLine() && responseLine[3] != ' ' );
+    responseLine.truncate( 3 );
+
+    if ( state == Init && responseLine[0] == '2' ) {
+        // banner was okay, let's go on
+        *t << "HELO there\r\n";
+        state = Mail;
+    } else if ( state == Mail && responseLine[0] == '2' ) {
+        // HELO response was okay (well, it has to be)
+        *t << "MAIL FROM: <" << from << ">\r\n";
+        state = Rcpt;
+    } else if ( state == Rcpt && responseLine[0] == '2' ) {
+        *t << "RCPT TO: <" << rcpt << ">\r\n";
+        state = Data;
+    } else if ( state == Data && responseLine[0] == '2' ) {
+        *t << "DATA\r\n";
+        state = Body;
+    } else if ( state == Body && responseLine[0] == '3' ) {
+        *t << message << ".\r\n";
+        state = Quit;
+    } else if ( state == Quit && responseLine[0] == '2' ) {
+        *t << "TQUIT\r\n";
+        // here, we just close.
+        state = Close;
+        emit status( tr( "Message sent" ) );
+    } else if ( state == Close ) {
+        deleteLater();
+        return;
+    } else {
+        // something broke.
+        TQMessageBox::warning( qApp->activeWindow(),
+                              tr( "TQt Mail Example" ),
+                              tr( "Unexpected reply from SMTP server:\n\n" ) +
+                              response );
+        state = Close;
+    }
+
+    response = "";
+}
+
+ +

See also Network Examples. + + +


+ +
Copyright © 2007 +TrolltechTrademarks +
TQt 3.3.8
+
+ -- cgit v1.2.1