servus, ich hab mal wieder eine recht spezille frage. ich sitz nun schon etwas länger an dem problem mit c++ (vs2008) eine mail aus dem programm heraus zu versenden. mir geht es um folgendes ich hab ein programm was einen text z.b eine log datei erstellt und dann als anhang an meine mailadresse schicken soll. meine mailadresse könnte z.b bei gmx web.de oder sonstwo sein und es muss jeder anwender ausführen können, dabei ist die adresse über und an die versendet wird aber fest.
okay das ists eigentlich auch schon*gg*.
verwende vs2008 mit qt integration 4.4.3 jeweils prof. wäre super wenn mir da jemand etwas weiter helfen könnte. diesen code habe ich bisher, der funzt aber irgendwie nicht (wird kompiliert aber es kommt keine mail). das beispiel habe ich in einem anderen forum gefunden, ist also nicht von mir.
main:.cpp
smtp.cpp
smtp.h:
okay das ists eigentlich auch schon*gg*.
verwende vs2008 mit qt integration 4.4.3 jeweils prof. wäre super wenn mir da jemand etwas weiter helfen könnte. diesen code habe ich bisher, der funzt aber irgendwie nicht (wird kompiliert aber es kommt keine mail). das beispiel habe ich in einem anderen forum gefunden, ist also nicht von mir.
main:.cpp
Code:
Smtp *newMail = new Smtp("a@b.de","f@b.de"," Your Subject","My body text");
delete newMail;
smtp.cpp
Code:
#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.live.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() }
void Smtp::errorReceived(QTcpSocket::SocketError socketError)
{
qDebug() }
void Smtp::disconnected()
{
qDebug() qDebug() errorString();
}
void Smtp::connected()
{
//output.append("connected");
qDebug() }
void Smtp::readyRead()
{
qDebug() // 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 t->flush();
state = Mail;
}
else if ( state == Mail && responseLine[0] == '2' )
{
// HELO response was okay (well, it has to be)
*t t->flush();
state = Rcpt;
}
else if ( state == Rcpt && responseLine[0] == '2' )
{
*t /r
t->flush();
state = Data;
}
else if ( state == Data && responseLine[0] == '2' )
{
*t t->flush();
state = Body;
}
else if ( state == Body && responseLine[0] == '3' )
{
*t t->flush();
state = Quit;
}
else if ( state == Quit && responseLine[0] == '2' )
{
*t 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 = "";
}
smtp.h:
Code:
#ifndef SMTP_H
#define SMTP_H
#include QTcpSocket>
#include
#include
#include
#include
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 output;
QString response;
enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
int state;
};
#endif
Zuletzt bearbeitet von einem Moderator: