#ifndef SMTP_H
#define SMTP_H
#include <QApplication>
#include <QMessageBox>
#include <QtDebug>
#include <QString>
#include <QObject>
#include <QTcpSocket>
#include <QTextStream>
{
Q_OBJECT
public:
~Smtp();
signals:
private slots:
void readyRead();
void connected();
void disconnected();
private:
enum State {
Init,
Mail,
Rcpt,
Data,
Body,
Quit,
Close
};
int state;
};
#endif // SMTP_H
#ifndef SMTP_H
#define SMTP_H
#include <QApplication>
#include <QMessageBox>
#include <QtDebug>
#include <QString>
#include <QObject>
#include <QTcpSocket>
#include <QTextStream>
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 readyRead();
void connected();
void stateChanged( QAbstractSocket::SocketState socketState );
void errorReceived( QAbstractSocket::SocketError socketError );
void disconnected();
private:
enum State {
Init,
Mail,
Rcpt,
Data,
Body,
Quit,
Close
};
QString message;
QString from;
QString rcpt;
QTcpSocket * socket;
QTextStream * t;
int state;
QString response;
};
#endif // SMTP_H
To copy to clipboard, switch view to plain text mode
#include "smtp.h"
{
connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
connect( socket, SIGNAL( disconnectedFromHost()), this,
SLOT(disconnected() ) );
message
= QString::fromLatin1( "From: " ) +from
+QString::fromLatin1( "\nTo: " ) +to
+QString::fromLatin1( "\nSubject: " ) +subject
+QString::fromLatin1( "\n\n" ) +body
+"\n";
"\r\n" ) );
message.
replace( QString::fromLatin1( "\r\n.\r\n" ),
QString::fromLatin1( "\r\n..\r\n" ) );
this->from = from;
rcpt = to;
state = Init;
// change this to correct email server address
socket->connectToHost( "smtp.poczta.onet.pl", 25 );
if( socket->waitForConnected( 5000 ) )
qDebug("connected");
}
Smtp::~Smtp()
{
delete t;
delete socket;
}
{
qDebug() <<"stateChanged " << socketState;
}
{
qDebug() << "error " <<socketError;
}
void Smtp::disconnected()
{
qDebug() <<"disconneted";
qDebug() << "error " << socket->errorString();
}
void Smtp::connected()
{
qDebug() << "Connected ";
}
void Smtp::readyRead()
{
if( !socket->canReadLine() )
return;
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";
t->flush();
state = Mail;
}
else if ( state == Mail && responseLine[0] == '2' )
{
// HELLO 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";
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 << ".\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*t << "QUIT\r\n";
// here, we just close.
state = Close;
//t->flush();
emit status( tr( "Message sent" ) );
}
else if ( state == Close )
{
deleteLater();
return;
}
else
{
// something broke.
QMessageBox::warning( qApp
->activeWindow
(), tr
( "Qt Mail Example" ), tr
( "Unexpected reply from SMTP server:\n\n" ) + response
);
state = Close;
}
response = "";
}
#include "smtp.h"
Smtp::Smtp( const QString &from, const QString &to, const QString
&subject, const QString &body )
{
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( disconnectedFromHost()), this,
SLOT(disconnected() ) );
message = QString::fromLatin1( "From: " ) +from
+QString::fromLatin1( "\nTo: " ) +to +QString::fromLatin1( "\nSubject: "
) +subject +QString::fromLatin1( "\n\n" ) +body +"\n";
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;
// change this to correct email server address
socket->connectToHost( "smtp.poczta.onet.pl", 25 );
if( socket->waitForConnected( 5000 ) )
qDebug("connected");
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()
{
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";
t->flush();
state = Mail;
}
else if ( state == Mail && responseLine[0] == '2' )
{
// HELLO 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";
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 << ".\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*t << "QUIT\r\n";
// here, we just close.
state = Close;
//t->flush();
emit status( tr( "Message sent" ) );
}
else if ( state == Close )
{
deleteLater();
return;
}
else
{
// something broke.
QMessageBox::warning( qApp->activeWindow(), tr( "Qt Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
state = Close;
}
response = "";
}
To copy to clipboard, switch view to plain text mode
Smtp *newMail = new Smtp("anonymous@gall.com","recipient@mail.com","Such a title","Such mail content");
Smtp *newMail = new Smtp("anonymous@gall.com","recipient@mail.com","Such a title","Such mail content");
To copy to clipboard, switch view to plain text mode
Bookmarks