发送电子邮件中的按钮qt creator

时间:2017-12-02 18:28:56

标签: qt email button hyperlink

我正在尝试从我的应用程序发送一封电子邮件,其中包含一个按钮,如果点击该按钮,会将数据发送回我的应用程序,我可以将简单的文本电子邮件发送到给定的电子邮件地址,但是不知道如何从我发送的电子邮件中接收数据。 我正在使用Qt creator 4.8

到目前为止,这是我的代码

smtp.h

#ifndef SMTP_H
#define SMTP_H

#include <QtNetwork/QAbstractSocket>
#include <QtNetwork/QSslSocket>
#include <QtNetwork/qsslsocket.h>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QtWidgets/QMessageBox>
#include <QByteArray>


class Smtp : public QObject
{
   Q_OBJECT


public:
    Smtp( const QString &user, const QString &pass,
      const QString &host, int port = 465, int timeout = 40000 );
    ~Smtp();

    void sendMail( const QString &from, const QString &to,
               const QString &subject, const QString &body );

signals:
     void status( const QString &);

private slots:
void stateChanged(QAbstractSocket::SocketState socketState);
void errorReceived(QAbstractSocket::SocketError socketError);
void disconnected();
void connected();
void readyRead();

private:
int timeout;
QString message;
QTextStream *t;
QSslSocket *socket;
QString from;
QString rcpt;
QString response;
QString user;
QString pass;
QString host;
int port;
enum states{Tls, HandShake 
            ,Auth,User,Pass,Rcpt,Mail,Data,Init,Body,Quit,Close};
             int state;

 };

 #endif // SMTP_H

smtp.cpp

#include "smtp.h"

Smtp::Smtp( const QString &user, const QString &pass, const QString &host, 
            int port, int timeout )
{
socket = new QSslSocket(this);

connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(connected()), this, SLOT(connected() ) );
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), 
        this,SLOT(errorReceived(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, 
        SLOT(stateChanged(QAbstractSocket::SocketState)));
connect(socket, SIGNAL(disconnected()), this,SLOT(disconnected()));


this->user = user;
this->pass = pass;

this->host = host;
this->port = port;
this->timeout = timeout;


 }

 void Smtp::sendMail(const QString &from, const QString &to, const QString 
                     &subject, const QString &body)
{
message = "To: " + to + "\n";
message.append("From: " + from + "\n");
message.append("Subject: " + subject + "\n");
message.append(body);
message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) 
);
message.replace( QString::fromLatin1( "\r\n.\r\n" ),
QString::fromLatin1( "\r\n..\r\n" ) );
this->from = from;
rcpt = to;
state = Init;
socket->connectToHostEncrypted(host, port); 

if (!socket->waitForConnected(timeout)) {
     qDebug() << socket->errorString();
 }

t = new QTextStream( socket );

}

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

void Smtp::stateChanged(QAbstractSocket::SocketState socketState)
{

qDebug() <<"stateChanged " << socketState;
}

void Smtp::errorReceived(QAbstractSocket::SocketError socketError)
{
qDebug() << "error " <<socketError;
}

void Smtp::disconnected()
{

qDebug() <<"disconneted";
qDebug() << "error "  << socket->errorString();
}

void Smtp::connected()
{
qDebug() << "Connected ";
}

void Smtp::readyRead()
{

 qDebug() <<"readyRead";
// SMTP is line-oriented

QString responseLine;
do
{
    responseLine = socket->readLine();
    response += responseLine;
}
while ( socket->canReadLine() && responseLine[3] != ' ' );

responseLine.truncate( 3 );

qDebug() << "Server response code:" <<  responseLine;
qDebug() << "Server response: " << response;

if ( state == Init && responseLine == "220" )
{
    // banner was okay, let's go on
    *t << "EHLO localhost" <<"\r\n";
    t->flush();

    state = HandShake;
}

else if (state == HandShake && responseLine == "250")
{
    socket->startClientEncryption();
    if(!socket->waitForEncrypted(timeout))
    {
        qDebug() << socket->errorString();
        state = Close;
    }


    //Send EHLO once again but now encrypted

    *t << "EHLO localhost" << "\r\n";
    t->flush();
    state = Auth;
}
else if (state == Auth && responseLine == "250")
{
    // Trying AUTH
    qDebug() << "Auth";
    *t << "AUTH LOGIN" << "\r\n";
    t->flush();
    state = User;
}
else if (state == User && responseLine == "334")
{
    //Trying User
    qDebug() << "Username";

    //https://developers.google.com/gmail/xoauth2_protocol
    *t << QByteArray().append(user).toBase64()  << "\r\n";
    t->flush();

    state = Pass;
}
else if (state == Pass && responseLine == "334")
{
    //Trying pass
    qDebug() << "Pass";
    *t << QByteArray().append(pass).toBase64() << "\r\n";
    t->flush();

    state = Mail;
}
else if ( state == Mail && responseLine == "235" )
{

    qDebug() << "MAIL FROM:<" << from << ">";
    *t << "MAIL FROM:<" << from << ">\r\n";
    t->flush();
    state = Rcpt;
}
else if ( state == Rcpt && responseLine == "250" )
{

    *t << "RCPT TO:<" << rcpt << ">\r\n"; //r
    t->flush();
    state = Data;
}
else if ( state == Data && responseLine == "250" )
{

    *t << "DATA\r\n";
    t->flush();
    state = Body;
}
else if ( state == Body && responseLine == "354" )
{

    *t << message << "\r\n.\r\n";
    t->flush();
    state = Quit;
}
else if ( state == Quit && responseLine == "250" )
{

    *t << "QUIT\r\n";
    t->flush();
    // here, we just close.
    state = Close;
    emit status( tr( "Message sent" ) );
}
else if ( state == Close )
{
    deleteLater();
    return;
}
else
{
    // something broke.
    QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Unexpected 
                 reply from SMTP server:\n\n" ) + response );
    state = Close;
    emit status( tr( "Failed to send message" ) );
}
response = "";
}

0 个答案:

没有答案