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); 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"); smtp.setPassword("your_password");
// Create a MimeMessage // Create a MimeMessage
MimeMessage message; 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.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name"));
message.setSubject("SmtpClient for Qt - Demo"); message.setSubject("SmtpClient for Qt - Demo");
// Add some text // Add some text
MimeText text; MimeText text;
text.setText("Hi!\n This is an email with some attachments."); text.setText("Hi!\n This is an email with some attachments.");
message.addPart(&text); message.addPart(&text);
@ -46,7 +45,7 @@ int main(int argc, char *argv[])
MimeAttachment attachment (new QFile("image1.jpg")); MimeAttachment attachment (new QFile("image1.jpg"));
// the file type can be setted. (by default is application/octet-stream) // 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 // Now add it to message
message.addPart(&attachment); message.addPart(&attachment);

View File

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

View File

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

View File

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

View File

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

View File

@ -18,11 +18,9 @@
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeHtml::MimeHtml(const QString &html) MimeHtml::MimeHtml(const QString &html) : MimeText(html)
{ {
this->html = html; this->cType = "text/html";
this->encoding = _7Bit;
this->charset = "utf-8";
} }
MimeHtml::~MimeHtml() {} MimeHtml::~MimeHtml() {}
@ -34,33 +32,14 @@ MimeHtml::~MimeHtml() {}
void MimeHtml::setHtml(const QString & html) void MimeHtml::setHtml(const QString & html)
{ {
this->html = html; this->text = html;
}
void MimeHtml::setEncoding(MimePart::Encoding enc)
{
this->encoding = enc;
}
void MimeHtml::setCharset(const QString & charset)
{
this->charset = charset;
} }
const QString & MimeHtml::getHtml() const const QString & MimeHtml::getHtml() const
{ {
return html; return text;
} }
const QString & MimeHtml::getCharset() const
{
return charset;
}
MimePart::Encoding MimeHtml::getEncoding() const
{
return encoding;
}
/* [2] --- */ /* [2] --- */
@ -69,27 +48,8 @@ MimePart::Encoding MimeHtml::getEncoding() const
void MimeHtml::prepare() void MimeHtml::prepare()
{ {
this->header = "Content-Type: text/html; charset=" /* !!! IMPORTANT !!! */
+ this->charset + "\n"; MimeText::prepare();
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";
} }
/* [3] --- */ /* [3] --- */

View File

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

View File

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

View File

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

View File

@ -15,11 +15,14 @@
*/ */
#include "mimepart.h" #include "mimepart.h"
#include "quotedprintable.h"
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimePart::MimePart() MimePart::MimePart()
{ {
cEncoding = _7Bit;
prepared = false;
} }
MimePart::~MimePart() MimePart::~MimePart()
@ -32,7 +35,7 @@ MimePart::~MimePart()
/* [2] Getters and Setters */ /* [2] Getters and Setters */
void MimePart::setContent(const QString & content) void MimePart::setContent(const QByteArray & content)
{ {
this->content = content; this->content = content;
} }
@ -52,11 +55,61 @@ const QString& MimePart::getHeader() const
return header; return header;
} }
const QString& MimePart::getContent() const const QByteArray& MimePart::getContent() const
{ {
return content; 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] --- */ /* [2] --- */
@ -64,9 +117,10 @@ const QString& MimePart::getContent() const
QString MimePart::toString() QString MimePart::toString()
{ {
if (!prepared)
prepare(); prepare();
return header + "\n" + content; return mimeString;
} }
/* [3] --- */ /* [3] --- */
@ -74,6 +128,75 @@ QString MimePart::toString()
/* [4] Protected methods */ /* [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] --- */ /* [4] --- */

View File

@ -25,11 +25,11 @@ class MimePart : public QObject
public: public:
/* [0] Enumerations */ /* [0] Enumerations */
enum Encoding { enum Encoding {
Base64,
_7Bit, _7Bit,
_8Bit _8Bit,
Base64,
QuotedPrintable
}; };
@ -47,13 +47,28 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
const QString& getHeader() const; 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 setHeader(const QString & header);
void addHeaderLine(const QString & line); 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] --- */ /* [2] --- */
@ -61,23 +76,29 @@ public:
QString toString(); QString toString();
virtual void prepare();
/* [3] --- */ /* [3] --- */
protected: protected:
/* [4] Protected members */ /* [4] Protected members */
QString header; QString header;
QString content; QByteArray content;
QString cId;
QString cName;
QString cType;
QString cCharset;
Encoding cEncoding;
QString mimeString;
bool prepared;
/* [4] --- */ /* [4] --- */
/* [5] Protected methods */
virtual void prepare();
/* [5] --- */
}; };
#endif // MIMEPART_H #endif // MIMEPART_H

View File

@ -18,11 +18,12 @@
/* [1] Constructors and Destructors */ /* [1] Constructors and Destructors */
MimeText::MimeText(const QString &text) MimeText::MimeText(const QString &txt)
{ {
this->text = text; this->text = txt;
this->charset = "utf-8"; this->cType = "text/plain";
this->encoding = _7Bit; this->cCharset = "utf-8";
this->cEncoding = _8Bit;
} }
MimeText::~MimeText() { } MimeText::~MimeText() { }
@ -37,31 +38,11 @@ void MimeText::setText(const QString & text)
this->text = 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 const QString & MimeText::getText() const
{ {
return text; return text;
} }
MimePart::Encoding MimeText::getEncoding() const
{
return encoding;
}
const QString & MimeText::getCharset() const
{
return charset;
}
/* [2] --- */ /* [2] --- */
@ -69,28 +50,11 @@ const QString & MimeText::getCharset() const
void MimeText::prepare() void MimeText::prepare()
{ {
this->header = "Content-Type: text/plain; charset=" this->content.clear();
+ this->charset + "\n"; this->content.append(text);
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";
/* !!! IMPORTANT !!! */
MimePart::prepare();
} }
/* [3] --- */ /* [3] --- */

View File

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