source: trunk/examples/network/mail/smtp.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/****************************************************************************
2** $Id: smtp.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "smtp.h"
12
13#include <qtextstream.h>
14#include <qsocket.h>
15#include <qdns.h>
16#include <qtimer.h>
17#include <qapplication.h>
18#include <qmessagebox.h>
19#include <qregexp.h>
20
21
22Smtp::Smtp( const QString &from, const QString &to,
23 const QString &subject,
24 const QString &body )
25{
26 socket = new QSocket( this );
27 connect ( socket, SIGNAL( readyRead() ),
28 this, SLOT( readyRead() ) );
29 connect ( socket, SIGNAL( connected() ),
30 this, SLOT( connected() ) );
31
32 mxLookup = new QDns( to.mid( to.find( '@' )+1 ), QDns::Mx );
33 connect( mxLookup, SIGNAL(resultsReady()),
34 this, SLOT(dnsLookupHelper()) );
35
36 message = QString::fromLatin1( "From: " ) + from +
37 QString::fromLatin1( "\nTo: " ) + to +
38 QString::fromLatin1( "\nSubject: " ) + subject +
39 QString::fromLatin1( "\n\n" ) + body + "\n";
40 message.replace( QString::fromLatin1( "\n" ),
41 QString::fromLatin1( "\r\n" ) );
42 message.replace( QString::fromLatin1( "\r\n.\r\n" ),
43 QString::fromLatin1( "\r\n..\r\n" ) );
44
45 this->from = from;
46 rcpt = to;
47
48 state = Init;
49}
50
51
52Smtp::~Smtp()
53{
54 delete t;
55 delete socket;
56}
57
58
59void Smtp::dnsLookupHelper()
60{
61 QValueList<QDns::MailServer> s = mxLookup->mailServers();
62 if ( s.isEmpty() ) {
63 if ( !mxLookup->isWorking() )
64 emit status( tr( "Error in MX record lookup" ) );
65 return;
66 }
67
68 emit status( tr( "Connecting to %1" ).arg( s.first().name ) );
69
70 socket->connectToHost( s.first().name, 25 );
71 t = new QTextStream( socket );
72}
73
74
75void Smtp::connected()
76{
77 emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
78}
79
80void Smtp::readyRead()
81{
82 // SMTP is line-oriented
83 if ( !socket->canReadLine() )
84 return;
85
86 QString responseLine;
87 do {
88 responseLine = socket->readLine();
89 response += responseLine;
90 } while( socket->canReadLine() && responseLine[3] != ' ' );
91 responseLine.truncate( 3 );
92
93 if ( state == Init && responseLine[0] == '2' ) {
94 // banner was okay, let's go on
95 *t << "HELO there\r\n";
96 state = Mail;
97 } else if ( state == Mail && responseLine[0] == '2' ) {
98 // HELO response was okay (well, it has to be)
99 *t << "MAIL FROM: <" << from << ">\r\n";
100 state = Rcpt;
101 } else if ( state == Rcpt && responseLine[0] == '2' ) {
102 *t << "RCPT TO: <" << rcpt << ">\r\n";
103 state = Data;
104 } else if ( state == Data && responseLine[0] == '2' ) {
105 *t << "DATA\r\n";
106 state = Body;
107 } else if ( state == Body && responseLine[0] == '3' ) {
108 *t << message << ".\r\n";
109 state = Quit;
110 } else if ( state == Quit && responseLine[0] == '2' ) {
111 *t << "QUIT\r\n";
112 // here, we just close.
113 state = Close;
114 emit status( tr( "Message sent" ) );
115 } else if ( state == Close ) {
116 deleteLater();
117 return;
118 } else {
119 // something broke.
120 QMessageBox::warning( qApp->activeWindow(),
121 tr( "Qt Mail Example" ),
122 tr( "Unexpected reply from SMTP server:\n\n" ) +
123 response );
124 state = Close;
125 }
126
127 response = "";
128}
Note: See TracBrowser for help on using the repository browser.