summaryrefslogtreecommitdiffstats
path: root/doc/html/mail-example.html
blob: 2525a719c0e2bb520be43578aa4dbe2e0dede48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/network/mail/mail.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A simple mail client</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

 
<p> 
<p> This example shows how to use the <a href="tqsocket.html">TQSocket</a> class. The client can only be
used to send mails. The interesting part is the implementation of the
SMTP protocol.
<p> <hr>
<p> Header file (smtp.h):
<p> <pre>/****************************************************************************
** $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 &lt;<a href="tqobject-h.html">tqobject.h</a>&gt;
#include &lt;<a href="tqstring-h.html">tqstring.h</a>&gt;

class TQSocket;
class TQTextStream;
class TQDns;

class Smtp : public <a href="tqobject.html">TQObject</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

public:
    Smtp( const <a href="tqstring.html">TQString</a> &amp;from, const <a href="tqstring.html">TQString</a> &amp;to,
          const <a href="tqstring.html">TQString</a> &amp;subject, const <a href="tqstring.html">TQString</a> &amp;body );
    ~Smtp();

signals:
    void status( const <a href="tqstring.html">TQString</a> &amp; );

private slots:
    void dnsLookupHelper();
    void readyRead();
    void connected();

private:
    enum State {
        Init,
        Mail,
        Rcpt,
        Data,
        Body,
        Quit,
        Close
    };

    <a href="tqstring.html">TQString</a> message;
    <a href="tqstring.html">TQString</a> from;
    <a href="tqstring.html">TQString</a> rcpt;
    <a href="tqsocket.html">TQSocket</a> *socket;
    <a href="tqtextstream.html">TQTextStream</a> * t;
    int state;
    <a href="tqstring.html">TQString</a> response;
    <a href="tqdns.html">TQDns</a> * mxLookup;
};

#endif
</pre>

<p> <hr>
<p> Implementation (smtp.cpp):
<p> <pre>/****************************************************************************
** $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 &lt;<a href="tqtextstream-h.html">tqtextstream.h</a>&gt;
#include &lt;<a href="tqsocket-h.html">tqsocket.h</a>&gt;
#include &lt;<a href="tqdns-h.html">tqdns.h</a>&gt;
#include &lt;<a href="tqtimer-h.html">tqtimer.h</a>&gt;
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="tqmessagebox-h.html">tqmessagebox.h</a>&gt;
#include &lt;<a href="tqregexp-h.html">tqregexp.h</a>&gt;


<a name="f297"></a>Smtp::Smtp( const <a href="tqstring.html">TQString</a> &amp;from, const <a href="tqstring.html">TQString</a> &amp;to,
            const <a href="tqstring.html">TQString</a> &amp;subject,
            const <a href="tqstring.html">TQString</a> &amp;body )
{
    socket = new <a href="tqsocket.html">TQSocket</a>( this );
<a name="x714"></a>    <a href="tqobject.html#connect">connect</a> ( socket, TQ_SIGNAL( <a href="tqsocket.html#readyRead">readyRead</a>() ),
              this, TQ_SLOT( readyRead() ) );
<a name="x711"></a>    <a href="tqobject.html#connect">connect</a> ( socket, TQ_SIGNAL( <a href="tqsocket.html#connected">connected</a>() ),
              this, TQ_SLOT( connected() ) );

<a name="x717"></a><a name="x715"></a>    mxLookup = new <a href="tqdns.html">TQDns</a>( to.<a href="tqstring.html#mid">mid</a>( to.<a href="tqstring.html#find">find</a>( '@' )+1 ), TQDns::Mx );
<a name="x707"></a>    <a href="tqobject.html#connect">connect</a>( mxLookup, TQ_SIGNAL(<a href="tqdns.html#resultsReady">resultsReady</a>()),
             this, TQ_SLOT(dnsLookupHelper()) );

<a name="x716"></a>    message = TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "From: " ) + from +
              TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\nTo: " ) + to +
              TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\nSubject: " ) + subject +
              TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\n\n" ) + body + "\n";
    message.replace( TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\n" ),
                     TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\r\n" ) );
    message.replace( TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\r\n.\r\n" ),
                     TQString::<a href="tqstring.html#fromLatin1">fromLatin1</a>( "\r\n..\r\n" ) );

    this-&gt;from = from;
    rcpt = to;

    state = Init;
}


Smtp::~Smtp()
{
    delete t;
    delete socket;
}


void <a name="f298"></a>Smtp::dnsLookupHelper()
{
<a name="x706"></a>    <a href="tqvaluelist.html">TQValueList</a>&lt;TQDns::MailServer&gt; s = mxLookup-&gt;<a href="tqdns.html#mailServers">mailServers</a>();
<a name="x720"></a>    if ( s.<a href="tqvaluelist.html#isEmpty">isEmpty</a>() ) {
<a name="x705"></a>        if ( !mxLookup-&gt;<a href="tqdns.html#isWorking">isWorking</a>() )
            emit status( <a href="tqobject.html#tr">tr</a>( "Error in MX record lookup" ) );
        return;
    }

<a name="x719"></a>    emit status( <a href="tqobject.html#tr">tr</a>( "Connecting to %1" ).arg( s.<a href="tqvaluelist.html#first">first</a>().name ) );

<a name="x710"></a>    socket-&gt;<a href="tqsocket.html#connectToHost">connectToHost</a>( s.<a href="tqvaluelist.html#first">first</a>().name, 25 );
    t = new <a href="tqtextstream.html">TQTextStream</a>( socket );
}


void <a name="f299"></a>Smtp::connected()
{
<a name="x712"></a>    emit status( <a href="tqobject.html#tr">tr</a>( "Connected to %1" ).arg( socket-&gt;<a href="tqsocket.html#peerName">peerName</a>() ) );
}

void <a name="f300"></a>Smtp::readyRead()
{
    // SMTP is line-oriented
<a name="x709"></a>    if ( !socket-&gt;<a href="tqsocket.html#canReadLine">canReadLine</a>() )
        return;

    <a href="tqstring.html">TQString</a> responseLine;
    do {
<a name="x713"></a>        responseLine = socket-&gt;<a href="tqsocket.html#readLine">readLine</a>();
        response += responseLine;
    } while( socket-&gt;<a href="tqsocket.html#canReadLine">canReadLine</a>() &amp;&amp; responseLine[3] != ' ' );
<a name="x718"></a>    responseLine.<a href="tqstring.html#truncate">truncate</a>( 3 );

    if ( state == Init &amp;&amp; responseLine[0] == '2' ) {
        // banner was okay, let's go on
        *t &lt;&lt; "HELO there\r\n";
        state = Mail;
    } else if ( state == Mail &amp;&amp; responseLine[0] == '2' ) {
        // HELO response was okay (well, it has to be)
        *t &lt;&lt; "MAIL FROM: &lt;" &lt;&lt; from &lt;&lt; "&gt;\r\n";
        state = Rcpt;
    } else if ( state == Rcpt &amp;&amp; responseLine[0] == '2' ) {
        *t &lt;&lt; "RCPT TO: &lt;" &lt;&lt; rcpt &lt;&lt; "&gt;\r\n";
        state = Data;
    } else if ( state == Data &amp;&amp; responseLine[0] == '2' ) {
        *t &lt;&lt; "DATA\r\n";
        state = Body;
    } else if ( state == Body &amp;&amp; responseLine[0] == '3' ) {
        *t &lt;&lt; message &lt;&lt; ".\r\n";
        state = Quit;
    } else if ( state == Quit &amp;&amp; responseLine[0] == '2' ) {
        *t &lt;&lt; "QUIT\r\n";
        // here, we just close.
        state = Close;
        emit status( <a href="tqobject.html#tr">tr</a>( "Message sent" ) );
    } else if ( state == Close ) {
        <a href="tqobject.html#deleteLater">deleteLater</a>();
        return;
    } else {
        // something broke.
<a name="x708"></a><a name="x704"></a>        TQMessageBox::<a href="tqmessagebox.html#warning">warning</a>( tqApp-&gt;<a href="ntqapplication.html#activeWindow">activeWindow</a>(),
                              <a href="tqobject.html#tr">tr</a>( "TQt Mail Example" ),
                              <a href="tqobject.html#tr">tr</a>( "Unexpected reply from SMTP server:\n\n" ) +
                              response );
        state = Close;
    }

    response = "";
}
</pre>

<p>See also <a href="network-examples.html">Network Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>