Depending on the version of QT, the 3.x series had a smtp mail example in the "networking" examples.
/****************************************************************************
** $Id: qt/smtp.h 3.3.6 edited Aug 31 2005 $
**
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef SMTP_H
#define SMTP_H
#include <QTcpSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QMessageBox>
{
Q_OBJECT
public:
~Smtp();
signals:
private slots:
void stateChanged
(QTcpSocket::SocketState socketState
);
void errorReceived
(QTcpSocket::SocketError socketError
);
void disconnected();
void connected();
void readyRead();
private:
enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
int state;
};
#endif
/****************************************************************************
** $Id: qt/smtp.h 3.3.6 edited Aug 31 2005 $
**
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#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 &from, const QString &to,
const QString &subject, const QString &body );
~Smtp();
signals:
void status( const QString &);
private slots:
void stateChanged(QTcpSocket::SocketState socketState);
void errorReceived(QTcpSocket::SocketError socketError);
void disconnected();
void connected();
void readyRead();
private:
QString message;
QTextStream *t;
QTcpSocket *socket;
QString from;
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
#include "smtp.h"
{
connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
connect( socket, SIGNAL(error(SocketError)), this,
SLOT(errorReceived(SocketError)));
connect( socket, SIGNAL(stateChanged( SocketState)), this,
SLOT(stateChanged(SocketState)));
connect(socket, SIGNAL(disconnectedFromHost()), this,
SLOT(disconnected()));;
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->connectToHost( "smtp.yourserver.com", 25);
if(socket->waitForConnected ( 30000 )) {qDebug("connected"); }
}
Smtp::~Smtp()
{
delete t;
delete socket;
}
void Smtp
::stateChanged(QTcpSocket::SocketState socketState
) {
qDebug() <<"stateChanged " << socketState;
}
void Smtp
::errorReceived(QTcpSocket::SocketError socketError
) {
qDebug() << "error " <<socketError;
}
void Smtp::disconnected()
{
qDebug() <<"disconneted";
qDebug() << "error " << socket->errorString();
}
void Smtp::connected()
{
output->append("connected");
qDebug() << "Connected ";
}
void Smtp::readyRead()
{
qDebug() <<"readyRead";
// SMTP is line-oriented
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' )
{
// 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 << "\r\n.\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*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 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(SocketError)), this,
SLOT(errorReceived(SocketError)));
connect( socket, SIGNAL(stateChanged( SocketState)), this,
SLOT(stateChanged(SocketState)));
connect(socket, SIGNAL(disconnectedFromHost()), this,
SLOT(disconnected()));;
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->connectToHost( "smtp.yourserver.com", 25);
if(socket->waitForConnected ( 30000 )) {qDebug("connected"); }
t = new QTextStream( socket );
}
Smtp::~Smtp()
{
delete t;
delete socket;
}
void Smtp::stateChanged(QTcpSocket::SocketState socketState)
{
qDebug() <<"stateChanged " << socketState;
}
void Smtp::errorReceived(QTcpSocket::SocketError socketError)
{
qDebug() << "error " <<socketError;
}
void Smtp::disconnected()
{
qDebug() <<"disconneted";
qDebug() << "error " << socket->errorString();
}
void Smtp::connected()
{
output->append("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 );
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' )
{
// 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 << "\r\n.\r\n";
t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*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 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("from@address.com","to@address.com"," Your Subject","My body text");
delete newMail;
Smtp *newMail = new Smtp("from@address.com","to@address.com"," Your Subject","My body text");
delete newMail;
To copy to clipboard, switch view to plain text mode
Please note: change the server address in "smtp.cpp" to your local server, most of the time localhost will work.. remote hosts should also work though.
As well, this may not work right off the bat, i speciallized it to work for my program.. i just hacked it back together for an example. The code is essentially correct, i just usually include the "To: BLAH\n", "From: BLAH\n" and "Subject: BLAH\n" within my message body.. so if there is something wrong it should be in that area.
Bookmarks