#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;
}