2011-09-02 12:20:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2011 - Tőkés Attila
|
|
|
|
|
|
|
|
This file is part of SmtpClient for Qt.
|
|
|
|
|
|
|
|
SmtpClient for Qt is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
SmtpClient for Qt is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY.
|
|
|
|
|
2011-09-02 15:24:39 +00:00
|
|
|
See the LICENSE file for more details.
|
2011-09-02 12:20:33 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
#include "mimemessage.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2011-11-18 19:58:16 +00:00
|
|
|
#include "quotedprintable.h"
|
2011-08-30 18:59:16 +00:00
|
|
|
|
|
|
|
/* [1] Constructors and Destructors */
|
|
|
|
|
|
|
|
MimeMessage::MimeMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeMessage::~MimeMessage()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [1] --- */
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Getters and Setters */
|
|
|
|
|
|
|
|
void MimeMessage::setSender(EmailAddress* e)
|
|
|
|
{
|
|
|
|
this->sender = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MimeMessage::addRecipient(EmailAddress* rcpt)
|
|
|
|
{
|
|
|
|
this->recipients << rcpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MimeMessage::setSubject(const QString & subject)
|
|
|
|
{
|
|
|
|
this->subject = subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MimeMessage::addPart(MimePart *part)
|
|
|
|
{
|
|
|
|
this->parts << part;
|
|
|
|
}
|
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
|
2011-11-10 18:50:38 +00:00
|
|
|
{
|
2011-11-18 19:58:16 +00:00
|
|
|
this->hEncoding = hEnc;
|
2011-11-10 18:50:38 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
const EmailAddress & MimeMessage::getSender() const
|
|
|
|
{
|
|
|
|
return *sender;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<EmailAddress*> & MimeMessage::getRecipients() const
|
|
|
|
{
|
|
|
|
return recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString & MimeMessage::getSubject() const
|
|
|
|
{
|
|
|
|
return subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<MimePart*> & MimeMessage::getParts() const
|
|
|
|
{
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] --- */
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Public Methods */
|
|
|
|
|
|
|
|
QString MimeMessage::toString()
|
|
|
|
{
|
|
|
|
QString mime;
|
2011-11-18 19:58:16 +00:00
|
|
|
|
|
|
|
/* =========== MIME HEADER ============ */
|
|
|
|
|
|
|
|
/* ---------- Sender / From ----------- */
|
|
|
|
mime = "From:";
|
|
|
|
if (sender->getName() != "")
|
|
|
|
{
|
|
|
|
switch (hEncoding)
|
|
|
|
{
|
|
|
|
case MimePart::Base64:
|
|
|
|
mime += " =?utf-8?B?" + QByteArray().append(sender->getName()).toBase64() + "?=";
|
|
|
|
break;
|
|
|
|
case MimePart::QuotedPrintable:
|
|
|
|
mime += " =?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append(sender->getName())).replace(' ', "_").replace(':',"=3A") + "?=";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mime += " " + sender->getName();
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += " <" + sender->getAddress() + ">\r\n";
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ---------------------------------- */
|
2011-11-10 18:50:38 +00:00
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ------- Recipients / To ---------- */
|
2011-08-30 18:59:16 +00:00
|
|
|
QList<EmailAddress*>::iterator it;
|
|
|
|
for (it = recipients.begin(); it != recipients.end(); ++it)
|
2011-11-10 18:50:38 +00:00
|
|
|
{
|
2011-11-18 19:58:16 +00:00
|
|
|
mime += "To:";
|
|
|
|
|
|
|
|
if ((*it)->getName() != "")
|
|
|
|
{
|
|
|
|
switch (hEncoding)
|
|
|
|
{
|
|
|
|
case MimePart::Base64:
|
|
|
|
mime += " =?utf-8?B?" + QByteArray().append((*it)->getName()).toBase64() + "?=";
|
|
|
|
break;
|
|
|
|
case MimePart::QuotedPrintable:
|
|
|
|
mime += " =?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append((*it)->getName())).replace(' ', "_").replace(':',"=3A") + "?=";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mime += " " + (*it)->getName();
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += " <" + (*it)->getAddress() + ">\r\n";
|
2011-11-10 18:50:38 +00:00
|
|
|
}
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ---------------------------------- */
|
2011-11-10 18:50:38 +00:00
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ------------ Subject ------------- */
|
2011-11-10 18:50:38 +00:00
|
|
|
mime += "Subject: ";
|
2011-11-18 19:58:16 +00:00
|
|
|
|
|
|
|
switch (hEncoding)
|
|
|
|
{
|
|
|
|
case MimePart::Base64:
|
|
|
|
mime += "=?utf-8?B?" + QByteArray().append(subject).toBase64() + "?=";
|
|
|
|
break;
|
|
|
|
case MimePart::QuotedPrintable:
|
|
|
|
mime += "=?utf-8?Q?" + QuotedPrintable::encode(QByteArray().append(subject)).replace(' ', "_").replace(':',"=3A") + "?=";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mime += subject;
|
|
|
|
}
|
|
|
|
/* ---------------------------------- */
|
|
|
|
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += "\r\n";
|
2011-08-30 18:59:16 +00:00
|
|
|
|
|
|
|
QString boundary = "----MIME-part-boundary=" + QByteArray().append(QDateTime::currentDateTime().toString()).toBase64() + "-end";
|
|
|
|
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += "MIME-Version: 1.0\r\n";
|
|
|
|
mime += "Content-type: multipart/mixed; boundary=\"" + boundary + "\"\r\n\r\n";
|
2011-08-30 18:59:16 +00:00
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ====== END OF MIME HEADER ======== */
|
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
boundary = "--" + boundary;
|
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ========== MIME BODY ============= */
|
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
QList<MimePart*>::iterator i;
|
|
|
|
for (i = parts.begin(); i != parts.end(); ++i)
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += boundary + "\r\n" + (*i)->toString();
|
2011-08-30 18:59:16 +00:00
|
|
|
|
2011-12-16 12:42:26 +00:00
|
|
|
mime += boundary + "--\r\n";
|
2011-08-30 18:59:16 +00:00
|
|
|
|
2011-11-18 19:58:16 +00:00
|
|
|
/* ====== END OF MIME BODY ========= */
|
|
|
|
|
2011-08-30 18:59:16 +00:00
|
|
|
return mime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] --- */
|