#include "smtp.h"
#include <QAbstractSocket>
{
qDebug()<< state;
connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
connect( socket, SIGNAL( disconnected()), this, SLOT(disconnected()));;
connect( socket, SIGNAL( connectionClosed()),this,SLOT(connectionClosed()) );
connect( socket, SIGNAL( aboutToClose()),this,SLOT(aboutToClose()) );
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;
this->subject = subject;
rcpt = to;
socket->connectToHost( strmailserver , port);
if(socket->waitForConnected ( 10000 )){
qDebug("connected");
}
if(socket->waitForReadyRead(30000)){
qDebug("I'm ready");
qDebug()<< socket->state();
qDebug()<< t->device()->canReadLine();
}else{
qDebug("I'm not ready");
qDebug()<< socket->state();
}
}
{
qDebug() <<"stateChanged: stateChanged " << socketState;
}
{
qDebug() << "errorReceived: error " << socketError;
}
void Smtp::disconnected()
{
qDebug() <<"disconnected: " << socket->errorString();
}
void Smtp::connectionClosed(){
qDebug() <<"Connection closed";
}
void Smtp::aboutToClose(){
qDebug() <<"Closing connection";
}
void Smtp::connected()
{
qDebug() << "Connected";
}
void Smtp::readyRead()
{
qDebug()<< "readyRead: "<<socket->state();
qDebug() <<"Starting to send";
do
{
responseLine = socket->readLine();
response += responseLine;
}
while ( socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
qDebug() << "State: " << socket->state() << "\r\n RL: " << responseLine[0];
if ( socket->state() == 3 && responseLine[0] == '2' )
{
qDebug()<< "Before to helo: "<<socket->state();
*t << "EHLO myhost\0\r\n";
qDebug()<< "After: "<<socket->state();
t->flush();
state = Mail;
}
else if ( state == Mail && responseLine[0] == '2' )
{
// HELO response was okay (well, it has to be)
*t << "MAIL FROM: <" << from << ">\r\n";
t->flush();
state = Rcpt;
}
else if ( state == Rcpt && responseLine[0] == '2' )
{
*t << "RCPT TO: <" << rcpt << ">\r\n"; //r
t->flush();
state = Data;
}
else if ( state == Data && responseLine[0] == '2' )
{
*t << "DATA\r\n";
t->flush();
state = Body;
}
else if ( state == Body && responseLine[0] == '3' )
{
*t << "Message-ID: My ID";
t->flush();
*t << "From: "<< from <<"\n\r";
t->flush();
*t << "User-Agent: My User Agent" <<"\n\r";
t->flush();
*t << "MIME-Version: 1.0\n\r";
t->flush();
*t << "To: "<< rcpt <<"\r\n";
t->flush();
*t << "Subject: " << subject << "\r\nContent-Type: text/plain; charset=ISO-8859-15; format=flowed\n\rContent-Transfer-Encoding: 7bit\n\r";
t->flush();
*t << message << "\r\n.\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*t << "QUIT\r\n";
t->flush();
state = Close;
emit status( tr( "Message sent" ) );
}
else if ( state == Close )
{
deleteLater();
return;
}
else
{
// something broke.
QMessageBox::warning( 0, tr
( "Qt Mail Example" ), tr
( "Unexpected reply from SMTP server:\n\n" ) + response
);
state = Close;
}
response = "";
}
Smtp::~Smtp()
{
delete t;
delete socket;
}
#include "smtp.h"
#include <QAbstractSocket>
Smtp::Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to, const QString &subject, const QString &body )
{
qDebug()<< state;
socket = new QTcpSocket( 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()));;
connect( socket, SIGNAL( connectionClosed()),this,SLOT(connectionClosed()) );
connect( socket, SIGNAL( aboutToClose()),this,SLOT(aboutToClose()) );
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;
this->subject = subject;
rcpt = to;
t = new QTextStream( socket );
socket->connectToHost( strmailserver , port);
if(socket->waitForConnected ( 10000 )){
qDebug("connected");
}
if(socket->waitForReadyRead(30000)){
qDebug("I'm ready");
qDebug()<< socket->state();
qDebug()<< t->device()->canReadLine();
}else{
qDebug("I'm not ready");
qDebug()<< socket->state();
}
}
void Smtp::stateChanged(QAbstractSocket::SocketState socketState)
{
qDebug() <<"stateChanged: stateChanged " << socketState;
}
void Smtp::errorReceived(QAbstractSocket::SocketError socketError)
{
qDebug() << "errorReceived: error " << socketError;
}
void Smtp::disconnected()
{
qDebug() <<"disconnected: " << socket->errorString();
}
void Smtp::connectionClosed(){
qDebug() <<"Connection closed";
}
void Smtp::aboutToClose(){
qDebug() <<"Closing connection";
}
void Smtp::connected()
{
qDebug() << "Connected";
}
void Smtp::readyRead()
{
QString u;
qDebug()<< "readyRead: "<<socket->state();
qDebug() <<"Starting to send";
QString responseLine;
do
{
responseLine = socket->readLine();
response += responseLine;
}
while ( socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
qDebug() << "State: " << socket->state() << "\r\n RL: " << responseLine[0];
if ( socket->state() == 3 && responseLine[0] == '2' )
{
qDebug()<< "Before to helo: "<<socket->state();
*t << "EHLO myhost\0\r\n";
qDebug()<< "After: "<<socket->state();
t->flush();
state = Mail;
}
else if ( state == Mail && responseLine[0] == '2' )
{
// HELO response was okay (well, it has to be)
*t << "MAIL FROM: <" << from << ">\r\n";
t->flush();
state = Rcpt;
}
else if ( state == Rcpt && responseLine[0] == '2' )
{
*t << "RCPT TO: <" << rcpt << ">\r\n"; //r
t->flush();
state = Data;
}
else if ( state == Data && responseLine[0] == '2' )
{
*t << "DATA\r\n";
t->flush();
state = Body;
}
else if ( state == Body && responseLine[0] == '3' )
{
*t << "Message-ID: My ID";
t->flush();
*t << "From: "<< from <<"\n\r";
t->flush();
*t << "User-Agent: My User Agent" <<"\n\r";
t->flush();
*t << "MIME-Version: 1.0\n\r";
t->flush();
*t << "To: "<< rcpt <<"\r\n";
t->flush();
*t << "Subject: " << subject << "\r\nContent-Type: text/plain; charset=ISO-8859-15; format=flowed\n\rContent-Transfer-Encoding: 7bit\n\r";
t->flush();
*t << message << "\r\n.\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*t << "QUIT\r\n";
t->flush();
state = Close;
emit status( tr( "Message sent" ) );
}
else if ( state == Close )
{
deleteLater();
return;
}
else
{
// something broke.
QMessageBox::warning( 0, tr( "Qt Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
state = Close;
}
response = "";
}
Smtp::~Smtp()
{
delete t;
delete socket;
}
To copy to clipboard, switch view to plain text mode
Nothing was sent, this is my last line of the output:
#ifndef SMTP_H
#define SMTP_H
#include <QTcpSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QMessageBox>
{
Q_OBJECT
public:
~Smtp();
signals:
private slots:
void disconnected();
void connectionClosed();
void aboutToClose();
void connected();
void readyRead();
private:
enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
int state;
};
#endif
#ifndef SMTP_H
#define SMTP_H
#include <QTcpSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QMessageBox>
class Smtp : public QObject
{
Q_OBJECT
public:
Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to,
const QString &subject, const QString &body );
~Smtp();
signals:
void status( const QString &);
private slots:
void stateChanged(QAbstractSocket::SocketState socketState);
void errorReceived(QAbstractSocket::SocketError socketError);
void disconnected();
void connectionClosed();
void aboutToClose();
void connected();
void readyRead();
private:
QString message;
QTextStream *t;
QTcpSocket *socket;
QString from;
QString subject;
QString rcpt;
QString response;
enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
int state;
};
#endif
To copy to clipboard, switch view to plain text mode
Help me plssss!!
Bookmarks