source: trunk/doc/html/mail-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 9.7 KB
Line 
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"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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
37used to send mails. The interesting part is the implementation of the
38SMTP 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 &lt;<a href="qobject-h.html">qobject.h</a>&gt;
55#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
56
57class QSocket;
58class QTextStream;
59class QDns;
60
61class Smtp : public <a href="qobject.html">QObject</a>
62{
63 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
64
65public:
66 Smtp( const <a href="qstring.html">QString</a> &amp;from, const <a href="qstring.html">QString</a> &amp;to,
67 const <a href="qstring.html">QString</a> &amp;subject, const <a href="qstring.html">QString</a> &amp;body );
68 ~Smtp();
69
70signals:
71 void status( const <a href="qstring.html">QString</a> &amp; );
72
73private slots:
74 void dnsLookupHelper();
75 void readyRead();
76 void connected();
77
78private:
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 &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
117#include &lt;<a href="qsocket-h.html">qsocket.h</a>&gt;
118#include &lt;<a href="qdns-h.html">qdns.h</a>&gt;
119#include &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
120#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
121#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
122#include &lt;<a href="qregexp-h.html">qregexp.h</a>&gt;
123
124
125<a name="f297"></a>Smtp::Smtp( const <a href="qstring.html">QString</a> &amp;from, const <a href="qstring.html">QString</a> &amp;to,
126 const <a href="qstring.html">QString</a> &amp;subject,
127 const <a href="qstring.html">QString</a> &amp;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-&gt;from = from;
149 rcpt = to;
150
151 state = Init;
152}
153
154
155Smtp::~Smtp()
156{
157 delete t;
158 delete socket;
159}
160
161
162void <a name="f298"></a>Smtp::dnsLookupHelper()
163{
164<a name="x706"></a> <a href="qvaluelist.html">QValueList</a>&lt;QDns::MailServer&gt; s = mxLookup-&gt;<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-&gt;<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-&gt;<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
178void <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-&gt;<a href="qsocket.html#peerName">peerName</a>() ) );
181}
182
183void <a name="f300"></a>Smtp::readyRead()
184{
185 // SMTP is line-oriented
186<a name="x709"></a> if ( !socket-&gt;<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-&gt;<a href="qsocket.html#readLine">readLine</a>();
192 response += responseLine;
193 } while( socket-&gt;<a href="qsocket.html#canReadLine">canReadLine</a>() &amp;&amp; responseLine[3] != ' ' );
194<a name="x718"></a> responseLine.<a href="qstring.html#truncate">truncate</a>( 3 );
195
196 if ( state == Init &amp;&amp; responseLine[0] == '2' ) {
197 // banner was okay, let's go on
198 *t &lt;&lt; "HELO there\r\n";
199 state = Mail;
200 } else if ( state == Mail &amp;&amp; responseLine[0] == '2' ) {
201 // HELO response was okay (well, it has to be)
202 *t &lt;&lt; "MAIL FROM: &lt;" &lt;&lt; from &lt;&lt; "&gt;\r\n";
203 state = Rcpt;
204 } else if ( state == Rcpt &amp;&amp; responseLine[0] == '2' ) {
205 *t &lt;&lt; "RCPT TO: &lt;" &lt;&lt; rcpt &lt;&lt; "&gt;\r\n";
206 state = Data;
207 } else if ( state == Data &amp;&amp; responseLine[0] == '2' ) {
208 *t &lt;&lt; "DATA\r\n";
209 state = Body;
210 } else if ( state == Body &amp;&amp; responseLine[0] == '3' ) {
211 *t &lt;&lt; message &lt;&lt; ".\r\n";
212 state = Quit;
213 } else if ( state == Quit &amp;&amp; responseLine[0] == '2' ) {
214 *t &lt;&lt; "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-&gt;<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 &copy; 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>
Note: See TracBrowser for help on using the repository browser.