📄 mail-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/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"><!--h3.fn,span.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 Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped 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="qsocket.html">QSocket</a> class. The client can only beused to send mails. The interesting part is the implementation of theSMTP protocol.<p> <hr><p> Header file (smtp.h):<p> <pre>/****************************************************************************** $Id: qt/smtp.h 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef SMTP_H#define SMTP_H#include <<a href="qobject-h.html">qobject.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>class QSocket;class QTextStream;class QDns;class Smtp : public <a href="qobject.html">QObject</a>{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: Smtp( const <a href="qstring.html">QString</a> &from, const <a href="qstring.html">QString</a> &to, const <a href="qstring.html">QString</a> &subject, const <a href="qstring.html">QString</a> &body ); ~Smtp();signals: void status( const <a href="qstring.html">QString</a> & );private slots: void dnsLookupHelper(); void readyRead(); void connected();private: enum State { Init, Mail, Rcpt, Data, Body, Quit, Close }; <a href="qstring.html">QString</a> message; <a href="qstring.html">QString</a> from; <a href="qstring.html">QString</a> rcpt; <a href="qsocket.html">QSocket</a> *socket; <a href="qtextstream.html">QTextStream</a> * t; int state; <a href="qstring.html">QString</a> response; <a href="qdns.html">QDns</a> * mxLookup;};#endif</pre><p> <hr><p> Implementation (smtp.cpp):<p> <pre>/****************************************************************************** $Id: qt/smtp.cpp 3.0.5 edited Apr 18 14:18 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "smtp.h"#include <<a href="qtextstream-h.html">qtextstream.h</a>>#include <<a href="qsocket-h.html">qsocket.h</a>>#include <<a href="qdns-h.html">qdns.h</a>>#include <<a href="qtimer-h.html">qtimer.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qregexp-h.html">qregexp.h</a>><a name="f288"></a>Smtp::Smtp( const <a href="qstring.html">QString</a> &from, const <a href="qstring.html">QString</a> &to, const <a href="qstring.html">QString</a> &subject, const <a href="qstring.html">QString</a> &body ){ socket = new <a href="qsocket.html">QSocket</a>( this );<a name="x760"></a> <a href="qobject.html#connect">connect</a> ( socket, SIGNAL( <a href="qsocket.html#readyRead">readyRead</a>() ), this, SLOT( readyRead() ) );<a name="x757"></a> <a href="qobject.html#connect">connect</a> ( socket, SIGNAL( <a href="qsocket.html#connected">connected</a>() ), this, SLOT( connected() ) );<a name="x763"></a><a name="x761"></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 );<a name="x753"></a> <a href="qobject.html#connect">connect</a>( mxLookup, SIGNAL(<a href="qdns.html#resultsReady">resultsReady</a>()), this, SLOT(dnsLookupHelper()) );<a name="x762"></a> message = QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "From: " ) + from + QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\nTo: " ) + to + QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\nSubject: " ) + subject + QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\n\n" ) + body + "\n"; message.replace( QRegExp( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\n" ) ), QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n" ) ); message.replace( QRegExp( QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n.\r\n" ) ), QString::<a href="qstring.html#fromLatin1">fromLatin1</a>( "\r\n..\r\n" ) ); this->from = from; rcpt = to; state = Init;}Smtp::~Smtp(){ delete t; delete socket;}void <a name="f289"></a>Smtp::dnsLookupHelper(){<a name="x752"></a> <a href="qvaluelist.html">QValueList</a><QDns::MailServer> s = mxLookup-><a href="qdns.html#mailServers">mailServers</a>();<a name="x766"></a><a name="x751"></a> if ( s.<a href="qvaluelist.html#isEmpty">isEmpty</a>() && mxLookup-><a href="qdns.html#isWorking">isWorking</a>() ) return;<a name="x765"></a> emit status( <a href="qobject.html#tr">tr</a>( "Connecting to %1" ).arg( s.<a href="qvaluelist.html#first">first</a>().name ) );<a name="x756"></a> socket-><a href="qsocket.html#connectToHost">connectToHost</a>( s.<a href="qvaluelist.html#first">first</a>().name, 25 ); t = new <a href="qtextstream.html">QTextStream</a>( socket );}void <a name="f290"></a>Smtp::connected(){<a name="x758"></a> emit status( <a href="qobject.html#tr">tr</a>( "Connected to %1" ).arg( socket-><a href="qsocket.html#peerName">peerName</a>() ) );}void <a name="f291"></a>Smtp::readyRead(){ // SMTP is line-oriented<a name="x755"></a> if ( !socket-><a href="qsocket.html#canReadLine">canReadLine</a>() ) return; <a href="qstring.html">QString</a> responseLine; do {<a name="x759"></a> responseLine = socket-><a href="qsocket.html#readLine">readLine</a>(); response += responseLine; } while( socket-><a href="qsocket.html#canReadLine">canReadLine</a>() && responseLine[3] != ' ' );<a name="x764"></a> responseLine.<a href="qstring.html#truncate">truncate</a>( 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 << "QUIT\r\n"; // here, we just close. state = Close; emit status( <a href="qobject.html#tr">tr</a>( "Message sent" ) ); } else if ( state == Close ) { delete this; return; } else { // something broke.<a name="x754"></a><a name="x750"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>( qApp-><a href="qapplication.html#activeWindow">activeWindow</a>(), <a href="qobject.html#tr">tr</a>( "Qt Mail Example" ), <a href="qobject.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 © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -