PDA

View Full Version : Sending Mail



systemz89
9th December 2007, 10:18
Hello...
How can I send an e-mail from a QT4 application? Don't write the code, please explain it me how... I stopped where I can't find any QT4 Class which helps me :(
Thanks, systemz89

wysota
9th December 2007, 10:29
There is no class for smtp. I remember writing a simple similar class for pop3, so that's not a big deal to do it for smtp as well. And there are non-Qt classes for sending mails through smtp - you might use them.

systemz89
9th December 2007, 10:37
There is no class for smtp. I remember writing a simple similar class for pop3, so that's not a big deal to do it for smtp as well. And there are non-Qt classes for sending mails through smtp - you might use them.

And where can I find some non-Qt classes?
And what about QTcpSocket (QNetwork module) class?

wysota
9th December 2007, 11:33
You can use QTcpSocket, but it's just a socket - you need to implement the whole smtp protocol yourself. As for the classes - ask google :)

systemz89
9th December 2007, 11:47
I find on the internet a source code:



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


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( "xx.xx.xx.xxx", 25 );

if( socket->waitForConnected( 30000 ) )
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 = "";
}

#endif


This code is in Qt3 and I can't correct it to Qt4... How can I resolve the convertion from Qt3 to Qt4?

wysota
9th December 2007, 11:51
This code is Qt4 code.

systemz89
9th December 2007, 12:09
This code is Qt4 code.

Ok.
I get the following error on compile:


smtp.h: In constructor ‘Smtp::Smtp(const QString&, const QString&, const QString&, const QString&)’:
smtp.h:79: error: no match for call to ‘(QDebug) (const char [10])’
make: *** [main.o] Error 1


Here is the problem:


if( socket->waitForConnected( 30000 ) )
qDebug()("connected");


And won't work with


if( socket->waitForConnected( 30000 ) )
qDebug() << "connected";

wysota
9th December 2007, 17:39
include <QtDebug>

sabeesh
14th December 2007, 03:54
Hi,
Please write details of your Qt version and OS. If it is Linix, Then you configure a mail server in your system and write the details to a file, What you need to send as a mail and use this command for send it as a mail
system("cat YourFileName.txt | mail -s \" Your Subject \" to_email_id@xxx.com");

After this you remove your test file.

sadjoker
15th June 2008, 01:29
The Smtp class which systemz89 provided is working. Thanx :) for saving me time.
Only changed to qDebug() << "connected"; made the class separated to .h and .cpp, and added main.cpp for execution. Then i tested it with local smtp server and it worked.
Now i should think how to make TLS AUTH to some smtp server too.

patrik08
15th June 2008, 09:02
The Smtp class which systemz89 provided is working. Thanx :) for saving me time.
Only changed to qDebug() << "connected"; made the class separated to .h and .cpp, and added main.cpp for execution. Then i tested it with local smtp server and it worked.
Now i should think how to make TLS AUTH to some smtp server too.


Have a look on:
http://qt-webdav.svn.sourceforge.net/viewvc/qt-webdav/sendmail_smtp_auth_console/
i write this chunk and work on qmail server auth smtp + formated html text mail

sadjoker
15th June 2008, 16:16
thanx, i`ll have a look.
I decided to try the next thing:
i got the "Free SMTP Server" app... i`ll try to call it with QProcess from my app, then send the mail trough it and close the process. no need to use anyone else smtp servers.