qt发送邮件测试

http://blog.chinaunix.net/uid-7210505-id-146366.html


#include <QApplication>
#include <QFont>
#include <QPushButton>

#include <QtNetwork/QTcpSocket>
#include <QThread>

#include <stdio.h>
#include <iostream>

using namespace std;

static void communication(QTcpSocket & socket, const char *msg)
{
        char data[1024];
        int nread = 0;

        if (socket.write(msg, qstrlen(msg)) == -1)
                qDebug() << "@@@@@@@@@@@@@@ socket.write failed";
        socket.flush();

        if (socket.waitForReadyRead(-1) == true)
        {
            memset(data, '\0', sizeof(data));
            socket.readLine(data, 1024);
            qDebug() << data;
        }
}

static void smtpCommunication(QTcpSocket & socket)
{
        communication(socket, "MAIL FROM: leisure.com>\r\n");
        communication(socket, "RCPT TO: \r\n");
        communication(socket, "DATA\r\n");
        communication(socket, "From: [email protected]\r\nTo: [email protected]\r\nSubject: QT EMAIL\r\n\r\nQT EMail Test2\r\n.\r\n");
        communication(socket, "quit\r\n");

        qDebug() << "send email ok." << endl;
}

static void readWelcome(QTcpSocket & socket)
{
        char data[1024];
        int len;

        if (socket.waitForReadyRead(-1) == true)
        {
            memset(data, '\0', sizeof(data));
            len = socket.readLine(data, 1024);
            qDebug() << data << endl;;
        }
}

int main(int argc, char* argv[])
{
        QTcpSocket socket;

        socket.connectToHost("192.168.10.230", 25);

        if (socket.waitForConnected(2000))
        {
                qDebug() << "smtp server connected success.";
                readWelcome(socket);
                smtpCommunication(socket);
                socket.close();
        }
        else
                qDebug() << "connection failed.";

        return 0;
}

你可能感兴趣的:(开源软件)