[Qt-interest] Email
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Nov 3 10:40:39 CET 2010
On 2010-11-03 Joshua Joshua Wambua wrote:
> Hi, does anyone know how to send and retrieve emails in qt using pop/smtp?
There was a Qt 3 example using a simple SMTP implementation:
http://doc.qt.nokia.com/3.3/mail-example.html
Not sure where that example has gone (I did not find it in the current Qt 4 examples).
It does not support attachements, but in one of my own applications I extended it in such a way, also using QMdCodec (providing some Base64 etc. encodings, but nowadays Qt4 provides this itself, I think via QByteArray, check docs).
This is totally Qt 3 networking, so it does need some re-write, but the SMTP implementation is simple enough to get inspired. As I said, it works for sending simple ASCII messages with binary attachements - but that's about it, no fancy email server password checking done etc.: it simply grabs the first MailServer, as returned by http://doc.qt.nokia.com/3.3/qdns.html#mailServers and connects to it on port 25 - simple as that. It used to work with my email provider at home (funnily even after they enforced password authentication when sending mails via Thunderbird, for example ;)
I am attaching the files used, but again, the QMdCodec part you can probably ignore, it is simply used for doing Base64 encoding for the attachement.
Cheers,
Oliver
p.s. For the record, here is the "Extended Qt 3 Smtp implementation, supporting attachements":
/*!**************************************************************************
** $Id: Smtp.cpp 4981 2007-07-20 14:41:36Z ok $
**
** Copyright (C) 1992-2000 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.
**
*****************************************************************************/
#include <qtextstream.h>
#include <qsocket.h>
#include <qdns.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qregexp.h>
#include <qmime.h>
#include <qfileinfo.h>
#include "../../Utils/src/Version.h"
#include "QMdCodec.h"
#include "Smtp.h"
// **************
// public methods
// **************
Smtp::Smtp (const QString &fromName, const QString &fromAddress,
const QString &toName, const QString &toAddress,
const QString &subject, const QString &body,
const QString &attachment)
{
QFileInfo fileInfo (attachment);
QMimeSourceFactory *mimeSourceFactory;
const QMimeSource *mimeSource;
QString multiPart;
QString mimeBoundary;
QStringList monthNameList;
QStringList dayNameList;
// non-localised names
monthNameList << "Jan" << "Feb" << "Mar" << "Apr" << "Mai" << "Jun"
<< "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
dayNameList << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
m_socket = new QSocket( this );
m_t = 0;
connect (m_socket, SIGNAL (readyRead()),
this, SLOT (readyRead()));
connect (m_socket, SIGNAL (connected()),
this, SLOT (connected()));
m_mxLookup = new QDns (toAddress.mid (toAddress.find ('@') + 1), QDns::Mx);
connect (m_mxLookup, SIGNAL(resultsReady()),
this, SLOT(dnsLookupHelper()));
mimeSourceFactory = QMimeSourceFactory::defaultFactory();
mimeSourceFactory->setFilePath (attachment);
mimeSource = mimeSourceFactory->data (attachment);
if (mimeSource != 0)
{
#ifdef _DEBUG
if (mimeSource->format() != 0) {
qDebug ("Smtp::Smtp: MIME format: %s", mimeSource->format());
}
else {
qDebug ("Smtp::Smtp: MIME format: NULL");
}
#endif
mimeBoundary = QString::fromLatin1 ("boundary-%1").arg (mimeSource->serialNumber());
if (QString (mimeSource->format()).contains ("text")) {
multiPart = QString::fromLatin1 ("\n\n--%1").arg (mimeBoundary) +
QString ("\nContent-Type: text/plain; charset=\"iso-8859-1\"; format=flowed") +
QString ("\nContent-Transfer-Encoding: 8bit\n\n") + body +
QString ("\n\n--%1").arg (mimeBoundary) +
QString ("\nContent-Type: %1").arg (mimeSource->format()) +
QString ("\nContent-Transfer-Encoding: 8bit") +
QString ("\nContent-Disposition: attachment; filename=%1\n\n").arg (fileInfo.fileName()) +
QString (mimeSource->encodedData (mimeSource->format()));
}
else {
multiPart = QString::fromLatin1 ("\n\n--%1").arg (mimeBoundary) +
QString ("\nContent-Type: text/plain; charset=\"iso-8859-1\"; format=flowed") +
QString ("\nContent-Transfer-Encoding: 8bit\n\n") + body +
QString ("\n\n--%1").arg (mimeBoundary) +
QString ("\nContent-Type: %1").arg (mimeSource->format()) +
QString ("\nContent-Transfer-Encoding: BASE64") +
QString ("\nContent-Disposition: attachment; filename=%1\n\n").arg (fileInfo.fileName()) +
QString (QCodecs::base64Encode (mimeSource->encodedData (mimeSource->format()), true));
}
QDateTime currentDateTime = QDateTime::currentDateTime();
m_message = QString ("From: %1 <%2>").arg (fromName).arg (fromAddress) +
"\nSubject: " + subject +
QString ("\nDate: %1, %2 %3 %4 %5 -0000")
.arg (dayNameList[currentDateTime.date().dayOfWeek() - 1])
.arg (currentDateTime.date().day())
.arg (monthNameList[currentDateTime.date().month() - 1])
.arg (currentDateTime.date().year())
.arg (currentDateTime.toString ("hh:mm:ss")) +
QString ("\nTo: %1 <%2>").arg (toName).arg (toAddress) +
QString ("\nOrganisation: %1").arg ("Lunchtime") +
QString ("\nUser-agent: %1 %2").arg (Version::getApplicationTitle()).arg (Version::toString()) +
"\nMIME-Version: 1.0 " +
QString ("\nContent-Type: multipart/mixed; boundary=\"%1\"\n").arg (mimeBoundary) +
"\n\nThis message is in MIME format. Use a proper mail-reader.\n\n" +
multiPart + "\n" +
QString ("\n--%1--\n").arg (mimeBoundary);
m_message.replace (QString::fromLatin1 ("\n"), QString::fromLatin1 ("\r\n"));
m_message.replace (QString::fromLatin1 ("\r\n.\r\n"), QString::fromLatin1 ("\r\n..\r\n"));
m_from = fromAddress;
m_rcpt = toAddress;
m_state = Init;
}
else {
#ifdef _DEBUG
qDebug ("Smpt::Smtp: an error occured, no mimeSource! NOT sending mail.");
#endif
m_state = Close;
this->deleteLater();
}
}
void Smtp::abort() {
#ifdef _DEBUG
qDebug ("Smpt::abort: CALLED.");
#endif
m_state = Close;
this->deleteLater();
}
// *****************
// protected methods
// *****************
Smtp::~Smtp()
{
if (m_t != 0) {
delete m_t;
}
delete m_socket;
}
void Smtp::dnsLookupHelper()
{
QValueList<QDns::MailServer> s = m_mxLookup->mailServers();
if (s.isEmpty()) {
if (!m_mxLookup->isWorking())
emit message (tr ("Error in MX record lookup"));
return;
}
emit message (tr ("Connecting to %1").arg (s.first().name));
m_socket->connectToHost (s.first().name, 25);
m_t = new QTextStream (m_socket);
}
void Smtp::connected()
{
emit message (tr ("Connected to %1").arg (m_socket->peerName()));
}
void Smtp::readyRead()
{
// SMTP is line-oriented
if (m_socket->canReadLine() == false) {
return;
}
QString responseLine;
do {
responseLine = m_socket->readLine();
m_response += responseLine;
} while( m_socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
if ( m_state == Init && responseLine[0] == '2' ) {
// banner was okay, let's go on
*m_t << "HELO there\r\n";
m_state = Mail;
} else if ( m_state == Mail && responseLine[0] == '2' ) {
// HELO response was okay (well, it has to be)
*m_t << "MAIL FROM: <" << m_from << ">\r\n";
m_state = Rcpt;
} else if ( m_state == Rcpt && responseLine[0] == '2' ) {
*m_t << "RCPT TO: <" << m_rcpt << ">\r\n";
m_state = Data;
} else if ( m_state == Data && responseLine[0] == '2' ) {
*m_t << "DATA\r\n";
m_state = Body;
} else if ( m_state == Body && responseLine[0] == '3' ) {
*m_t << m_message << ".\r\n";
m_state = Quit;
} else if ( m_state == Quit && responseLine[0] == '2' ) {
*m_t << "QUIT\r\n";
// here, we just close.
m_state = Close;
emit message( tr( "Message sent" ) );
} else if ( m_state == Close ) {
deleteLater();
return;
} else {
// something broke.
QMessageBox::warning (qApp->focusWidget(),
tr ("Send failure - ") + Version::getApplicationTitle(),
tr ("Unexpected reply from SMTP server:\n\n") +
m_response );
m_state = Close;
}
m_response = "";
}
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: QMdCodec.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101103/4bc28287/attachment.pl
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: QMdCodec.h
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101103/4bc28287/attachment.h
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Smtp.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101103/4bc28287/attachment-0001.pl
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Smtp.h
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101103/4bc28287/attachment-0001.h
More information about the Qt-interest-old
mailing list