[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/network/mail/mail.doc:5 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>A simple mail client</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A simple mail client</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | <p> This example shows how to use the <a href="qsocket.html">QSocket</a> class. The client can only be
|
---|
| 37 | used to send mails. The interesting part is the implementation of the
|
---|
| 38 | SMTP protocol.
|
---|
| 39 | <p> <hr>
|
---|
| 40 | <p> Header file (smtp.h):
|
---|
| 41 | <p> <pre>/****************************************************************************
|
---|
| 42 | ** $Id: mail-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 43 | **
|
---|
| 44 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 45 | **
|
---|
| 46 | ** This file is part of an example program for Qt. This example
|
---|
| 47 | ** program may be used, distributed and modified without limitation.
|
---|
| 48 | **
|
---|
| 49 | *****************************************************************************/
|
---|
| 50 |
|
---|
| 51 | #ifndef SMTP_H
|
---|
| 52 | #define SMTP_H
|
---|
| 53 |
|
---|
| 54 | #include <<a href="qobject-h.html">qobject.h</a>>
|
---|
| 55 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
| 56 |
|
---|
| 57 | class QSocket;
|
---|
| 58 | class QTextStream;
|
---|
| 59 | class QDns;
|
---|
| 60 |
|
---|
| 61 | class Smtp : public <a href="qobject.html">QObject</a>
|
---|
| 62 | {
|
---|
| 63 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
| 64 |
|
---|
| 65 | public:
|
---|
| 66 | Smtp( const <a href="qstring.html">QString</a> &from, const <a href="qstring.html">QString</a> &to,
|
---|
| 67 | const <a href="qstring.html">QString</a> &subject, const <a href="qstring.html">QString</a> &body );
|
---|
| 68 | ~Smtp();
|
---|
| 69 |
|
---|
| 70 | signals:
|
---|
| 71 | void status( const <a href="qstring.html">QString</a> & );
|
---|
| 72 |
|
---|
| 73 | private slots:
|
---|
| 74 | void dnsLookupHelper();
|
---|
| 75 | void readyRead();
|
---|
| 76 | void connected();
|
---|
| 77 |
|
---|
| 78 | private:
|
---|
| 79 | enum State {
|
---|
| 80 | Init,
|
---|
| 81 | Mail,
|
---|
| 82 | Rcpt,
|
---|
| 83 | Data,
|
---|
| 84 | Body,
|
---|
| 85 | Quit,
|
---|
| 86 | Close
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | <a href="qstring.html">QString</a> message;
|
---|
| 90 | <a href="qstring.html">QString</a> from;
|
---|
| 91 | <a href="qstring.html">QString</a> rcpt;
|
---|
| 92 | <a href="qsocket.html">QSocket</a> *socket;
|
---|
| 93 | <a href="qtextstream.html">QTextStream</a> * t;
|
---|
| 94 | int state;
|
---|
| 95 | <a href="qstring.html">QString</a> response;
|
---|
| 96 | <a href="qdns.html">QDns</a> * mxLookup;
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | #endif
|
---|
| 100 | </pre>
|
---|
| 101 |
|
---|
| 102 | <p> <hr>
|
---|
| 103 | <p> Implementation (smtp.cpp):
|
---|
| 104 | <p> <pre>/****************************************************************************
|
---|
| 105 | ** $Id: mail-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 106 | **
|
---|
| 107 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 108 | **
|
---|
| 109 | ** This file is part of an example program for Qt. This example
|
---|
| 110 | ** program may be used, distributed and modified without limitation.
|
---|
| 111 | **
|
---|
| 112 | *****************************************************************************/
|
---|
| 113 |
|
---|
| 114 | #include "smtp.h"
|
---|
| 115 |
|
---|
| 116 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
| 117 | #include <<a href="qsocket-h.html">qsocket.h</a>>
|
---|
| 118 | #include <<a href="qdns-h.html">qdns.h</a>>
|
---|
| 119 | #include <<a href="qtimer-h.html">qtimer.h</a>>
|
---|
| 120 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 121 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
| 122 | #include <<a href="qregexp-h.html">qregexp.h</a>>
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | <a name="f297"></a>Smtp::Smtp( const <a href="qstring.html">QString</a> &from, const <a href="qstring.html">QString</a> &to,
|
---|
| 126 | const <a href="qstring.html">QString</a> &subject,
|
---|
| 127 | const <a href="qstring.html">QString</a> &body )
|
---|
| 128 | {
|
---|
| 129 | socket = new <a href="qsocket.html">QSocket</a>( this );
|
---|
| 130 | <a name="x714"></a> <a href="qobject.html#connect">connect</a> ( socket, SIGNAL( <a href="qsocket.html#readyRead">readyRead</a>() ),
|
---|
| 131 | this, SLOT( readyRead() ) );
|
---|
| 132 | <a name="x711"></a> <a href="qobject.html#connect">connect</a> ( socket, SIGNAL( <a href="qsocket.html#connected">connected</a>() ),
|
---|
| 133 | this, SLOT( connected() ) );
|
---|
| 134 |
|
---|
| 135 | <a name="x717"></a><a name="x715"></a> mxLookup = new <a href="qdns.html">QDns</a>( to.<a href="qstring.html#mid">mid</a>( to.<a href="qstring.html#find">find</a>( '@' )+1 ), QDns::Mx );
|
---|
| 136 | <a name="x707"></a> <a href="qobject.html#connect">connect</a>( mxLookup, SIGNAL(<a href="qdns.html#resultsReady">resultsReady</a>()),
|
---|
| 137 | this, SLOT(dnsLookupHelper()) );
|
---|
| 138 |
|
---|
| 139 | <a name="x716"></a> message = QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "From: " ) + from +
|
---|
| 140 | QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\nTo: " ) + to +
|
---|
| 141 | QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\nSubject: " ) + subject +
|
---|
| 142 | QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\n\n" ) + body + "\n";
|
---|
| 143 | message.replace( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\n" ),
|
---|
| 144 | QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n" ) );
|
---|
| 145 | message.replace( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n.\r\n" ),
|
---|
| 146 | QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n..\r\n" ) );
|
---|
| 147 |
|
---|
| 148 | this->from = from;
|
---|
| 149 | rcpt = to;
|
---|
| 150 |
|
---|
| 151 | state = Init;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 | Smtp::~Smtp()
|
---|
| 156 | {
|
---|
| 157 | delete t;
|
---|
| 158 | delete socket;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | void <a name="f298"></a>Smtp::dnsLookupHelper()
|
---|
| 163 | {
|
---|
| 164 | <a name="x706"></a> <a href="qvaluelist.html">QValueList</a><QDns::MailServer> s = mxLookup-><a href="qdns.html#mailServers">mailServers</a>();
|
---|
| 165 | <a name="x720"></a> if ( s.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ) {
|
---|
| 166 | <a name="x705"></a> if ( !mxLookup-><a href="qdns.html#isWorking">isWorking</a>() )
|
---|
| 167 | emit status( <a href="qobject.html#tr">tr</a>( "Error in MX record lookup" ) );
|
---|
| 168 | return;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | <a name="x719"></a> emit status( <a href="qobject.html#tr">tr</a>( "Connecting to %1" ).arg( s.<a href="qvaluelist.html#first">first</a>().name ) );
|
---|
| 172 |
|
---|
| 173 | <a name="x710"></a> socket-><a href="qsocket.html#connectToHost">connectToHost</a>( s.<a href="qvaluelist.html#first">first</a>().name, 25 );
|
---|
| 174 | t = new <a href="qtextstream.html">QTextStream</a>( socket );
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | void <a name="f299"></a>Smtp::connected()
|
---|
| 179 | {
|
---|
| 180 | <a name="x712"></a> emit status( <a href="qobject.html#tr">tr</a>( "Connected to %1" ).arg( socket-><a href="qsocket.html#peerName">peerName</a>() ) );
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void <a name="f300"></a>Smtp::readyRead()
|
---|
| 184 | {
|
---|
| 185 | // SMTP is line-oriented
|
---|
| 186 | <a name="x709"></a> if ( !socket-><a href="qsocket.html#canReadLine">canReadLine</a>() )
|
---|
| 187 | return;
|
---|
| 188 |
|
---|
| 189 | <a href="qstring.html">QString</a> responseLine;
|
---|
| 190 | do {
|
---|
| 191 | <a name="x713"></a> responseLine = socket-><a href="qsocket.html#readLine">readLine</a>();
|
---|
| 192 | response += responseLine;
|
---|
| 193 | } while( socket-><a href="qsocket.html#canReadLine">canReadLine</a>() && responseLine[3] != ' ' );
|
---|
| 194 | <a name="x718"></a> responseLine.<a href="qstring.html#truncate">truncate</a>( 3 );
|
---|
| 195 |
|
---|
| 196 | if ( state == Init && responseLine[0] == '2' ) {
|
---|
| 197 | // banner was okay, let's go on
|
---|
| 198 | *t << "HELO there\r\n";
|
---|
| 199 | state = Mail;
|
---|
| 200 | } else if ( state == Mail && responseLine[0] == '2' ) {
|
---|
| 201 | // HELO response was okay (well, it has to be)
|
---|
| 202 | *t << "MAIL FROM: <" << from << ">\r\n";
|
---|
| 203 | state = Rcpt;
|
---|
| 204 | } else if ( state == Rcpt && responseLine[0] == '2' ) {
|
---|
| 205 | *t << "RCPT TO: <" << rcpt << ">\r\n";
|
---|
| 206 | state = Data;
|
---|
| 207 | } else if ( state == Data && responseLine[0] == '2' ) {
|
---|
| 208 | *t << "DATA\r\n";
|
---|
| 209 | state = Body;
|
---|
| 210 | } else if ( state == Body && responseLine[0] == '3' ) {
|
---|
| 211 | *t << message << ".\r\n";
|
---|
| 212 | state = Quit;
|
---|
| 213 | } else if ( state == Quit && responseLine[0] == '2' ) {
|
---|
| 214 | *t << "QUIT\r\n";
|
---|
| 215 | // here, we just close.
|
---|
| 216 | state = Close;
|
---|
| 217 | emit status( <a href="qobject.html#tr">tr</a>( "Message sent" ) );
|
---|
| 218 | } else if ( state == Close ) {
|
---|
| 219 | <a href="qobject.html#deleteLater">deleteLater</a>();
|
---|
| 220 | return;
|
---|
| 221 | } else {
|
---|
| 222 | // something broke.
|
---|
| 223 | <a name="x708"></a><a name="x704"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>( qApp-><a href="qapplication.html#activeWindow">activeWindow</a>(),
|
---|
| 224 | <a href="qobject.html#tr">tr</a>( "Qt Mail Example" ),
|
---|
| 225 | <a href="qobject.html#tr">tr</a>( "Unexpected reply from SMTP server:\n\n" ) +
|
---|
| 226 | response );
|
---|
| 227 | state = Close;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | response = "";
|
---|
| 231 | }
|
---|
| 232 | </pre>
|
---|
| 233 |
|
---|
| 234 | <p>See also <a href="network-examples.html">Network Examples</a>.
|
---|
| 235 |
|
---|
| 236 | <!-- eof -->
|
---|
| 237 | <p><address><hr><div align=center>
|
---|
| 238 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 239 | <td>Copyright © 2007
|
---|
| 240 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 241 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 242 | </table></div></address></body>
|
---|
| 243 | </html>
|
---|