SmtpClient-for-Qt/src/mimemessage.cpp

247 lines
6.0 KiB
C++
Raw Normal View History

2011-09-02 12:20:33 +00:00
/*
2012-07-24 14:06:55 +00:00
Copyright (c) 2011-2012 - Tőkés Attila
2011-09-02 12:20:33 +00:00
This file is part of SmtpClient for Qt.
2012-07-24 14:06:55 +00:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2011-09-02 12:20:33 +00:00
2012-07-24 14:06:55 +00:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2011-09-02 12:20:33 +00:00
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"
2012-07-23 18:01:08 +00:00
#include <typeinfo>
2011-08-30 18:59:16 +00:00
/* [1] Constructors and Destructors */
2012-07-23 18:01:08 +00:00
MimeMessage::MimeMessage(bool createAutoMimeContent) :
hEncoding(MimePart::_8Bit)
2011-08-30 18:59:16 +00:00
{
2012-07-23 18:01:08 +00:00
if (createAutoMimeContent)
this->content = new MimeMultiPart();
2011-08-30 18:59:16 +00:00
}
MimeMessage::~MimeMessage()
{
}
/* [1] --- */
/* [2] Getters and Setters */
MimePart& MimeMessage::getContent() {
return *content;
}
void MimeMessage::setContent(MimePart *content) {
this->content = content;
}
2011-08-30 18:59:16 +00:00
void MimeMessage::setSender(EmailAddress* e)
{
this->sender = e;
}
2012-07-23 18:01:08 +00:00
void MimeMessage::addRecipient(EmailAddress* rcpt, RecipientType type)
2011-08-30 18:59:16 +00:00
{
2012-07-23 18:01:08 +00:00
switch (type)
{
case To:
recipientsTo << rcpt;
break;
case Cc:
recipientsCc << rcpt;
break;
case Bcc:
recipientsBcc << rcpt;
break;
}
}
void MimeMessage::addTo(EmailAddress* rcpt) {
this->recipientsTo << rcpt;
}
void MimeMessage::addCc(EmailAddress* rcpt) {
this->recipientsCc << rcpt;
}
void MimeMessage::addBcc(EmailAddress* rcpt) {
this->recipientsBcc << rcpt;
2011-08-30 18:59:16 +00:00
}
void MimeMessage::setSubject(const QString & subject)
{
this->subject = subject;
}
void MimeMessage::addPart(MimePart *part)
{
2012-07-23 18:01:08 +00:00
if (typeid(*content) == typeid(MimeMultiPart)) {
((MimeMultiPart*) content)->addPart(part);
};
2011-08-30 18:59:16 +00:00
}
void MimeMessage::setHeaderEncoding(MimePart::Encoding hEnc)
{
this->hEncoding = hEnc;
}
2011-08-30 18:59:16 +00:00
const EmailAddress & MimeMessage::getSender() const
{
return *sender;
}
2012-07-23 18:01:08 +00:00
const QList<EmailAddress*> & MimeMessage::getRecipients(RecipientType type) const
2011-08-30 18:59:16 +00:00
{
2012-07-23 18:01:08 +00:00
switch (type)
{
default:
case To:
return recipientsTo;
case Cc:
return recipientsCc;
case Bcc:
return recipientsBcc;
}
2011-08-30 18:59:16 +00:00
}
const QString & MimeMessage::getSubject() const
{
return subject;
}
const QList<MimePart*> & MimeMessage::getParts() const
{
2012-07-24 14:09:19 +00:00
if (typeid(*content) == typeid(MimeMultiPart)) {
return ((MimeMultiPart*) content)->getParts();
2012-07-23 18:01:08 +00:00
}
else {
QList<MimePart*> *res = new QList<MimePart*>();
res->append(content);
return *res;
}
2011-08-30 18:59:16 +00:00
}
/* [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() + ">\r\n";
/* ---------------------------------- */
2011-08-30 18:59:16 +00:00
2012-07-23 18:01:08 +00:00
/* ------- Recipients / To ---------- */
mime += "To:";
QList<EmailAddress*>::iterator it; int i;
for (i = 0, it = recipientsTo.begin(); it != recipientsTo.end(); ++it, ++i)
{
2012-07-23 18:01:08 +00:00
if (i != 0) { mime += ","; }
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();
}
}
2012-07-23 18:01:08 +00:00
mime += " <" + (*it)->getAddress() + ">";
}
2012-07-23 18:01:08 +00:00
mime += "\r\n";
/* ---------------------------------- */
/* ------- Recipients / Cc ---------- */
2012-08-09 19:34:56 +00:00
if (recipientsCc.size() != 0) {
mime += "Cc:";
}
2012-07-23 18:01:08 +00:00
for (i = 0, it = recipientsCc.begin(); it != recipientsCc.end(); ++it, ++i)
{
if (i != 0) { mime += ","; }
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() + ">";
}
2012-08-09 19:34:56 +00:00
if (recipientsCc.size() != 0) {
mime += "\r\n";
}
/* ---------------------------------- */
/* ------------ Subject ------------- */
mime += "Subject: ";
2012-07-23 18:01:08 +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;
}
/* ---------------------------------- */
mime += "\r\n";
mime += "MIME-Version: 1.0\r\n";
2012-07-23 18:01:08 +00:00
mime += content->toString();
2011-08-30 18:59:16 +00:00
return mime;
}
/* [3] --- */