返回介绍

A simple mail client

发布于 2019-10-04 14:58:07 字数 5382 浏览 984 评论 0 收藏 0

This example shows how to use the QSocket class. The client can only be used to send mails. The interesting part is the implementation of the SMTP protocol.


Header file (smtp.h):

/****************************************************************************
** $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 <qobject.h>
#include <qstring.h>

class QSocket;
class QTextStream;
class QDns;

class Smtp : public QObject
{
    Q_OBJECT

public:
    Smtp( const QString &from, const QString &to,
          const QString &subject, const QString &body );
    ~Smtp();

signals:
    void status( const QString & );

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

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

    QString message;
    QString from;
    QString rcpt;
    QSocket *socket;
    QTextStream * t;
    int state;
    QString response;
    QDns * mxLookup;
};

#endif


Implementation (smtp.cpp):

/****************************************************************************
** $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 <qtextstream.h>
#include <qsocket.h>
#include <qdns.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qregexp.h>


Smtp::Smtp( const QString &from, const QString &to,
            const QString &subject,
            const QString &body )
{
    socket = new QSocket( this );
    connect ( socket, SIGNAL( readyRead() ),
              this, SLOT( readyRead() ) );
    connect ( socket, SIGNAL( connected() ),
              this, SLOT( connected() ) );

    mxLookup = new QDns( to.mid( to.find( '@' )+1 ), QDns::Mx );
    connect( mxLookup, SIGNAL(resultsReady()),
             this, SLOT(dnsLookupHelper()) );

    message = QString::fromLatin1( "From: " ) + from +
              QString::fromLatin1( "\nTo: " ) + to +
              QString::fromLatin1( "\nSubject: " ) + subject +
              QString::fromLatin1( "\n\n" ) + body + "\n";
    message.replace( QRegExp( QString::fromLatin1( "\n" ) ),
                     QString::fromLatin1( "\r\n" ) );
    message.replace( QRegExp( QString::fromLatin1( "\r\n.\r\n" ) ),
                     QString::fromLatin1( "\r\n..\r\n" ) );

    this->from = from;
    rcpt = to;

    state = Init;
}


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


void Smtp::dnsLookupHelper()
{
    QValueList<QDns::MailServer> s = mxLookup->mailServers();
    if ( s.isEmpty() && mxLookup->isWorking() )
        return;

    emit status( tr( "Connecting to %1" ).arg( s.first().name ) );

    socket->connectToHost( s.first().name, 25 );
    t = new QTextStream( socket );
}


void Smtp::connected()
{
    emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
}


void Smtp::readyRead()
{
    // SMTP is line-oriented
    if ( !socket->canReadLine() )
        return;

    QString responseLine;
    do {
        responseLine = socket->readLine();
        response += responseLine;
    } while( socket->canReadLine() && responseLine[3] != ' ' );
    responseLine.truncate( 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( tr( "Message sent" ) );
    } else if ( state == Close ) {
        delete this;
        return;
    } else {
        // something broke.
        QMessageBox::warning( qApp->activeWindow(),
                              tr( "Qt Mail Example" ),
                              tr( "Unexpected reply from SMTP server:\n\n" ) +
                              response );
        state = Close;
    }

    response = "";
}

See also Network Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文