The structure of MimePart and its subclasses was modified. Now the

following properties are part of MimePart: content-type, content-
added to project.
This commit is contained in:
bluetiger9 2011-11-13 19:18:58 +02:00 committed by Attila
parent 7b1c826d02
commit 2b7f475766
15 changed files with 296 additions and 189 deletions

View File

@ -25,19 +25,18 @@ int main(int argc, char *argv[])
SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
smtp.setUser("your_email_address@gmail.com");
smtp.setUser("your_email@gmail.com");
smtp.setPassword("your_password");
// Create a MimeMessage
MimeMessage message;
message.setSender(new EmailAddress("your_email_address@gmail.com", "Your Name"));
message.setSender(new EmailAddress("your_email@gmail.com", "Your Name"));
message.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name"));
message.setSubject("SmtpClient for Qt - Demo");
// Add some text
MimeText text;
text.setText("Hi!\n This is an email with some attachments.");
message.addPart(&text);
@ -46,7 +45,7 @@ int main(int argc, char *argv[])
MimeAttachment attachment (new QFile("image1.jpg"));
// the file type can be setted. (by default is application/octet-stream)
attachment.setType("image/jpg");
attachment.setContentType("image/jpg");
// Now add it to message
message.addPart(&attachment);

View File

@ -52,11 +52,11 @@ int main(int argc, char *argv[])
// An unique content id must be setted
image1.setContentId("image1");
image1.setType("image/jpg");
image1.setContentType("image/jpg");
MimeInlineFile image2 (new QFile("image2.jpg"));
image2.setContentId("image2");
image2.setType("image/jpg");
image2.setContentType("image/jpg");
message.addPart(&html);
message.addPart(&image1);

View File

@ -35,9 +35,10 @@ MimeAttachment::~MimeAttachment()
void MimeAttachment::prepare()
{
MimeFile::prepare();
this->header += "Content-disposition: attachment\n";
this->header += "Content-disposition: attachment;\n";
/* !!! IMPORTANT !!! */
MimeFile::prepare();
}
/* [2] --- */

View File

@ -23,8 +23,9 @@
MimeFile::MimeFile(QFile *file)
{
this->file = file;
this->type = "application/octet-stream";
this->name = QFileInfo(*file).fileName();
this->cType = "application/octet-stream";
this->cName = QFileInfo(*file).fileName();
this->cEncoding = Base64;
}
MimeFile::~MimeFile()
@ -36,26 +37,6 @@ MimeFile::~MimeFile()
/* [2] Getters and setters */
void MimeFile::setName(const QString & name)
{
this->name = name;
}
void MimeFile::setType(const QString & type)
{
this->type = type;
}
const QString & MimeFile::getName() const
{
return name;
}
const QString & MimeFile::getType() const
{
return type;
}
/* [2] --- */
@ -63,12 +44,12 @@ const QString & MimeFile::getType() const
void MimeFile::prepare()
{
this->header = "Content-Type: " + type + "; name=" + name + "\n";
this->header += "Content-Transfer-Encoding: base64\n";
file->open(QIODevice::ReadOnly);
this->content = file->readAll().toBase64() + "\n";
this->content = file->readAll();
file->close();
/* !!! IMPORTANT !!!! */
MimePart::prepare();
}
/* [3] --- */

View File

@ -35,12 +35,6 @@ public:
/* [2] Getters and Setters */
void setName(const QString & name);
void setType(const QString & type);
const QString & getName() const;
const QString & getType() const;
/* [2] --- */
protected:
@ -48,8 +42,6 @@ protected:
/* [3] Protected members */
QFile* file;
QString name;
QString type;
/* [3] --- */

View File

@ -18,11 +18,9 @@
/* [1] Constructors and Destructors */
MimeHtml::MimeHtml(const QString &html)
MimeHtml::MimeHtml(const QString &html) : MimeText(html)
{
this->html = html;
this->encoding = _7Bit;
this->charset = "utf-8";
this->cType = "text/html";
}
MimeHtml::~MimeHtml() {}
@ -34,33 +32,14 @@ MimeHtml::~MimeHtml() {}
void MimeHtml::setHtml(const QString & html)
{
this->html = html;
}
void MimeHtml::setEncoding(MimePart::Encoding enc)
{
this->encoding = enc;
}
void MimeHtml::setCharset(const QString & charset)
{
this->charset = charset;
this->text = html;
}
const QString & MimeHtml::getHtml() const
{
return html;
return text;
}
const QString & MimeHtml::getCharset() const
{
return charset;
}
MimePart::Encoding MimeHtml::getEncoding() const
{
return encoding;
}
/* [2] --- */
@ -69,27 +48,8 @@ MimePart::Encoding MimeHtml::getEncoding() const
void MimeHtml::prepare()
{
this->header = "Content-Type: text/html; charset="
+ this->charset + "\n";
this->header += "Content-Transfer-Encoding: ";
switch (encoding)
{
case _7Bit:
header += "7bit\n";
content = this->html.toAscii();
break;
case _8Bit:
header += "8bit\n";
content = this->html.toUtf8();
break;
case Base64:
header += "base64\n";
content = QByteArray().append(this->html).toBase64();
}
this->content += "\n";
/* !!! IMPORTANT !!! */
MimeText::prepare();
}
/* [3] --- */

View File

@ -17,9 +17,9 @@
#ifndef MIMEHTML_H
#define MIMEHTML_H
#include "mimepart.h"
#include "mimetext.h"
class MimeHtml : public MimePart
class MimeHtml : public MimeText
{
Q_OBJECT
public:
@ -35,12 +35,8 @@ public:
/* [2] Getters and Setters */
void setHtml(const QString & html);
void setEncoding(Encoding enc);
void setCharset(const QString & charset);
const QString& getHtml() const;
const QString& getCharset() const;
Encoding getEncoding() const;
/* [2] --- */
@ -48,10 +44,6 @@ protected:
/* [3] Protected members */
QString html;
QString charset;
Encoding encoding;
/* [3] --- */

View File

@ -21,7 +21,6 @@
MimeInlineFile::MimeInlineFile(QFile *f)
: MimeFile(f)
{
return;
}
MimeInlineFile::~MimeInlineFile()
@ -32,16 +31,6 @@ MimeInlineFile::~MimeInlineFile()
/* [2] Getters and Setters */
void MimeInlineFile::setContentId(const QString & cid)
{
this->cid = cid;
}
const QString & MimeInlineFile::getContentId() const
{
return cid;
}
/* [2] --- */
@ -49,10 +38,10 @@ const QString & MimeInlineFile::getContentId() const
void MimeInlineFile::prepare()
{
MimeFile::prepare();
this->header += "Content-Disposition: inline\n";
this->header += "Content-id: <" + cid + ">\n";
this->header += "Content-disposition: inline\n";
/* !!! IMPORTANT !!! */
MimeFile::prepare();
}
/* [3] --- */

View File

@ -33,17 +33,12 @@ public:
/* [2] Getters and Setters */
void setContentId(const QString & cid);
const QString & getContentId() const;
/* [2] --- */
protected:
/* [3] Protected members */
QString cid;
/* [3] --- */

View File

@ -15,11 +15,14 @@
*/
#include "mimepart.h"
#include "quotedprintable.h"
/* [1] Constructors and Destructors */
MimePart::MimePart()
{
cEncoding = _7Bit;
prepared = false;
}
MimePart::~MimePart()
@ -32,7 +35,7 @@ MimePart::~MimePart()
/* [2] Getters and Setters */
void MimePart::setContent(const QString & content)
void MimePart::setContent(const QByteArray & content)
{
this->content = content;
}
@ -52,11 +55,61 @@ const QString& MimePart::getHeader() const
return header;
}
const QString& MimePart::getContent() const
const QByteArray& MimePart::getContent() const
{
return content;
}
void MimePart::setContentId(const QString & cId)
{
this->cId = cId;
}
const QString & MimePart::getContentId() const
{
return this->cId;
}
void MimePart::setContentName(const QString & cName)
{
this->cName = cName;
}
const QString & MimePart::getContentName() const
{
return this->cName;
}
void MimePart::setContentType(const QString & cType)
{
this->cType = cType;
}
const QString & MimePart::getContentType() const
{
return this->cType;
}
void MimePart::setCharset(const QString & charset)
{
this->cCharset = charset;
}
const QString & MimePart::getCharset() const
{
return this->cCharset;
}
void MimePart::setEncoding(Encoding enc)
{
this->cEncoding = enc;
}
MimePart::Encoding MimePart::getEncoding() const
{
return this->cEncoding;
}
/* [2] --- */
@ -64,9 +117,10 @@ const QString& MimePart::getContent() const
QString MimePart::toString()
{
prepare();
if (!prepared)
prepare();
return header + "\n" + content;
return mimeString;
}
/* [3] --- */
@ -74,6 +128,75 @@ QString MimePart::toString()
/* [4] Protected methods */
void MimePart::prepare() {}
void MimePart::prepare()
{
mimeString = QString();
/* === Header Prepare === */
/* Content-Type */
mimeString.append("Content-Type: ").append(cType);
if (cName != "")
mimeString.append("; name=\"").append(cName).append("\"");
if (cCharset != "")
mimeString.append("; charset=").append(cCharset);
mimeString.append("\n");
/* ------------ */
/* Content-Transfer-Encoding */
mimeString.append("Content-Transfer-Encoding: ");
switch (cEncoding)
{
case _7Bit:
mimeString.append("7bit\n");
break;
case _8Bit:
mimeString.append("8bit\n");
break;
case Base64:
mimeString.append("base64\n");
break;
case QuotedPrintable:
mimeString.append("quoted-printable\n");
break;
}
/* ------------------------ */
/* Content-Id */
if (cId != NULL)
mimeString.append("Content-ID: <").append(cId).append(">\n");
/* ---------- */
/* ------------------------- */
mimeString.append(header).append("\n");
/* === End of Header Prepare === */
/* === Content Encoding === */
switch (cEncoding)
{
case _7Bit:
mimeString.append(QString(content).toAscii());
break;
case _8Bit:
mimeString.append(content);
break;
case Base64:
mimeString.append(content.toBase64());
break;
case QuotedPrintable:
mimeString.append(QuotedPrintable::encode(content));
break;
}
mimeString.append("\n");
/* === End of Content Encoding === */
prepared = true;
}
/* [4] --- */

View File

@ -25,11 +25,11 @@ class MimePart : public QObject
public:
/* [0] Enumerations */
enum Encoding {
Base64,
_7Bit,
_8Bit
_8Bit,
Base64,
QuotedPrintable
};
@ -47,13 +47,28 @@ public:
/* [2] Getters and Setters */
const QString& getHeader() const;
const QString& getContent() const;
const QByteArray& getContent() const;
void setContent(const QString & content);
void setContent(const QByteArray & content);
void setHeader(const QString & header);
void addHeaderLine(const QString & line);
void setContentId(const QString & cId);
const QString & getContentId() const;
void setContentName(const QString & cName);
const QString & getContentName() const;
void setContentType(const QString & cType);
const QString & getContentType() const;
void setCharset(const QString & charset);
const QString & getCharset() const;
void setEncoding(Encoding enc);
Encoding getEncoding() const;
/* [2] --- */
@ -61,23 +76,29 @@ public:
QString toString();
virtual void prepare();
/* [3] --- */
protected:
/* [4] Protected members */
QString header;
QString content;
QByteArray content;
QString cId;
QString cName;
QString cType;
QString cCharset;
Encoding cEncoding;
QString mimeString;
bool prepared;
/* [4] --- */
/* [5] Protected methods */
virtual void prepare();
/* [5] --- */
};
#endif // MIMEPART_H

View File

@ -18,11 +18,12 @@
/* [1] Constructors and Destructors */
MimeText::MimeText(const QString &text)
MimeText::MimeText(const QString &txt)
{
this->text = text;
this->charset = "utf-8";
this->encoding = _7Bit;
this->text = txt;
this->cType = "text/plain";
this->cCharset = "utf-8";
this->cEncoding = _8Bit;
}
MimeText::~MimeText() { }
@ -37,31 +38,11 @@ void MimeText::setText(const QString & text)
this->text = text;
}
void MimeText::setEncoding(MimePart::Encoding enc)
{
this->encoding = enc;
}
void MimeText::setCharset(const QString & charset)
{
this->charset = charset;
}
const QString & MimeText::getText() const
{
return text;
}
MimePart::Encoding MimeText::getEncoding() const
{
return encoding;
}
const QString & MimeText::getCharset() const
{
return charset;
}
/* [2] --- */
@ -69,28 +50,11 @@ const QString & MimeText::getCharset() const
void MimeText::prepare()
{
this->header = "Content-Type: text/plain; charset="
+ this->charset + "\n";
this->header += "Content-Transfer-Encoding: ";
switch (encoding)
{
case _7Bit:
header += "7bit\n";
content = this->text.toAscii();
break;
case _8Bit:
header += "8bit\n";
content = this->text.toUtf8();
break;
case Base64:
header += "base64\n";
content = QByteArray().append(this->text).toBase64();
}
this->content += "\n";
this->content.clear();
this->content.append(text);
/* !!! IMPORTANT !!! */
MimePart::prepare();
}
/* [3] --- */

View File

@ -34,12 +34,8 @@ public:
/* [2] Getters and Setters*/
void setText(const QString & text);
void setEncoding(Encoding enc);
void setCharset(const QString & charset);
const QString & getText() const;
MimePart::Encoding getEncoding() const;
const QString & getCharset() const;
/* [2] --- */
@ -48,9 +44,6 @@ protected:
/* [3] Protected members */
QString text;
QString charset;
Encoding encoding;
/* [3] --- */

64
src/quotedprintable.cpp Normal file
View File

@ -0,0 +1,64 @@
/*
Copyright (c) 2011 - Tőkés Attila (tokes_atti@yahoo.com)
This project 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.
This project is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the LICENSE file for more details.
*/
#include "quotedprintable.h"
QString& QuotedPrintable::encode(const QByteArray &input)
{
QString *output = new QString();
char byte;
const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0; i < input.length() ; ++i)
{
byte = input[i];
if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61))
{
output->append(byte);
}
else
{
output->append('=');
output->append(hex[((byte >> 4) & 0x0F)]);
output->append(hex[(byte & 0x0F)]);
}
}
return *output;
}
QByteArray& QuotedPrintable::decode(const QString &input)
{
// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F
const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};
QByteArray *output = new QByteArray();
for (int i = 0; i < input.length(); ++i)
{
if (input.at(i).toAscii() == '=')
{
output->append((hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0']);
}
else
{
output->append(input.at(i).toAscii());
}
}
return *output;
}

33
src/quotedprintable.h Normal file
View File

@ -0,0 +1,33 @@
/*
Copyright (c) 2011 - Tőkés Attila (tokes_atti@yahoo.com)
This project 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.
This project is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the LICENSE file for more details.
*/
#ifndef QUOTEDPRINTABLE_H
#define QUOTEDPRINTABLE_H
#include <QObject>
#include <QByteArray>
class QuotedPrintable : public QObject
{
Q_OBJECT
public:
static QString& encode(const QByteArray &input);
static QByteArray& decode(const QString &input);
private:
QuotedPrintable();
};
#endif // QUOTEDPRINTABLE_H