Re: Sending email using Qt.
Is the server working correctly? You can tell easily by using another email client and trying to send some mail.
Re: Sending email using Qt.
Hi,
I am trying to send mail to gmail server. I don't need my own server.
Thank You,
Baluk
Re: Sending email using Qt.
Hello all! First I want to thank all of you. This information has been very helpful. With some minor modifications, I am now able to send an email via SMTP. The code that I am using was pasted from this forum and tweaked to get it to work with Qt4.6.2.
I am having one issue though - my body text never gets transferred! What is happening? I get a subject line, but no body text. My ultimate goal is to attach a file, but I am a bit worried about accomplishing that if I cannot even get my body text to show up!
It is this part of the code that I really do not understand. 1) Does it have to be done in this manner? 2) What is the purpose of the replaces? 3) How is SMTP getting my Subject Text, but not my Body Text? Any help is appreciated. (P.S. I am going to post this on another thread in order to make sure it gets seen).
Code:
...
sMessage = "To: " + to + "\n";
sMessage.append("From: " + from + "\n");
sMessage.append("Subject: " + subject + "\n");
sMessage.append(body);
sMessage.
replace( QString::fromLatin1( "\n" ),
QString::fromLatin1( "\r\n" ) );
sMessage.
replace( QString::fromLatin1( "\r\n.\r\n" ),
QString::fromLatin1( "\r\n..\r\n" ) );
sFrom = from;
sTo = to;
...}
Re: Sending email using Qt.
EDIT: Please don't post in multiple topics. It's considered rude. I posted a reply here only to notice that Wysota posted a reply in your other thread.
Re: Sending email using Qt.
Quote:
Originally Posted by
squidge
EDIT: Please don't post in multiple topics. It's considered rude. I posted a reply here only to notice that Wysota posted a reply in your other thread.
Yes, yes, yes. I have already had my hand slapped. Won't happen again. Thank you for responding.
Re: Sending email using Qt.
Hi am new in qt..My problem is ..My application does connect to server but its not realy sending the emails that i want it to sent.Any help me
Re: Sending email using Qt.
This thread has not had anyone post to it since September 2011. I am curious how many of the above posters were able to get their e-mail functionality working? After a push in the right direction from you guys, plus reading a book on SMTP and some additional web help on Enhanced SMTP, I was able to create a very robust and flexible e-mail client within my application. I have the option to require user authentication (Ehelo) and I can send attachments, which was the real reason that I even pursued having e-mail capabilities in the first place. The bottom line is that not only it is possible to send e-mails using SMTP/Enhanced SMTP, but it is possible to send attachments as well. The number of attachments and the file type does not matter. I hope you were all successful in your pursuit. Thanks for your input.
Re: Sending email using Qt.
Re: Sending email using Qt.
Can someone post some working code please? It'd be much appreciated and save the rest of us a lot of time and effort.
Sure, we could dig at this for a few days and get it to eventually work -- but why climb a mountain when someone's already built an escalator?
Cheers in advance
Re: Sending email using Qt.
Also you can send e-mail by means of Qxt library http://libqxt.bitbucket.org. Namely, one can use
Quote:
int send ( const QxtMailMessage & message )
of QxtSmtp class.
Re: Sending email using Qt.
Quote:
Originally Posted by
nielsenj
Depending on the version of QT, the 3.x series had a smtp mail example in the "networking" examples.
The modified structure for QT 4.x is below:
smtp.h
Code:
/****************************************************************************
** $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
smtp.cpp
Code:
#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 = "";
}
To send a mail just use:
Code:
Smtp *newMail = new Smtp("from@address.com","to@address.com"," Your Subject","My body text");
delete newMail;
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.
Good Luck!
J
Hi, how I can add attachment to this code ?
Regards
Re: Sending email using Qt.
Hello guys,
I am trying to send an email using the code in post#9, i did make the modifiction that were mentioned in post#11. I was trying to send a msg using gmail.
so what i did was :
Smtp = *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailPass ward");
newMail->send("mygmail@gamil.com","mygmail@gamil.com","sub ject","body txt");
but i the connectoin does not want to establish
last msg i got is :
###Config server smtp connect to .............. "smtp.gmail.com"
can you please tell me what is wrong am i doing ??
thanks
Re: Sending email using Qt.
Quote:
so what i did was :
Smtp = *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailP ass ward");
Really? How did you get your compiler to accept that?
Re: Sending email using Qt.
Quote:
Really? How did you get your compiler to accept that?
Man, take it easy it is just a typing mistake.
Smtp *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailPass ward");
Re: Sending email using Qt.
Hi,
I have questions about post #3 of this theme.
1. What is the server this:
Code:
socket->connectToHost( "smtp.yourserver.com", 25);
What have I write instead "smtp.yourserver.com"
2. What is the "output" variable? Is it for example?
Thank you!
Re: Sending email using Qt.
USE SMTP
This tutorial will help you [How to send an email in Qt?][1]
[1]: https://info-4geek.com/how-to-send-an-email-in-qt/