SmtpClient-for-Qt/src/mimemessage.cpp

179 lines
4.1 KiB
C++
Raw Normal View History

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>
#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;
}
void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
{
this->hEncoding = hEnc;
}
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;
/* =========== 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();
}
}
mime += " <" + sender->getAddress() + ">\n";
/* ---------------------------------- */
2011-08-30 18:59: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)
{
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();
}
}
mime += " <" + (*it)->getAddress() + ">\n";
}
/* ---------------------------------- */
/* ------------ Subject ------------- */
mime += "Subject: ";
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;
}
/* ---------------------------------- */
mime += "\n";
2011-08-30 18:59:16 +00:00
QString boundary = "----MIME-part-boundary=" + QByteArray().append(QDateTime::currentDateTime().toString()).toBase64() + "-end";
mime += "MIME-Version: 1.0\n";
mime += "Content-type: multipart/mixed; boundary=\"" + boundary + "\"\n\n";
/* ====== END OF MIME HEADER ======== */
2011-08-30 18:59:16 +00:00
boundary = "--" + boundary;
/* ========== MIME BODY ============= */
2011-08-30 18:59:16 +00:00
QList<MimePart*>::iterator i;
for (i = parts.begin(); i != parts.end(); ++i)
mime += boundary + "\n" + (*i)->toString();
mime += boundary + "--\n";
/* ====== END OF MIME BODY ========= */
2011-08-30 18:59:16 +00:00
return mime;
}
/* [3] --- */