void EmailComposerInterface::setMessage( const QMailMessage &mail ) { if (mail.multipartType() == QMailMessagePartContainer::MultipartNone) { if (mail.hasBody()) setText( mail.body().data(), mail.contentType().content() ); } else { // The only type of multipart message we currently compose is Mixed, with // all but the first part as out-of-line attachments int textPart = -1; for ( uint i = 0; i < mail.partCount(); ++i ) { QMailMessagePart &part = const_cast<QMailMessagePart&>(mail.partAt(i)); if (textPart == -1 && part.contentType().type().toLower() == "text") { // This is the first text part, we will use as the forwarded text body textPart = i; } else { QString attPath = part.attachmentPath(); QMailMessage::AttachmentsAction action = QMailMessage::LinkToAttachments; // Detach the part data to a temporary file if necessary if (attPath.isEmpty()) { if (part.detachAttachment(Qtopia::tempDir())) { attPath = part.attachmentPath(); action = QMailMessage::CopyAttachments; // Create a content object for the file QContent doc(attPath); if (part.hasBody()) { QMailMessageContentType type(part.contentType()); if (doc.drmState() == QContent::Unprotected) doc.setType(type.content()); } doc.setName(part.displayName()); doc.setRole(QContent::Data); doc.commit(); // This needs to be removed after composition m_temporaries.append(doc); } } if (!attPath.isEmpty()) attach(attPath, action); } } if (textPart != -1) { const QMailMessagePart& part = mail.partAt(textPart); setText( part.body().data(), part.contentType().content() ); } } }
bool SmsClient::addMail(const QMailMessage& mail) { QList<QMailAddress> smsRecipients = separateSmsAddresses(mail.recipients()); Q_ASSERT(smsRecipients.count() > 0); QString smsBody = formatOutgoing(mail.subject(),mail.body().data()); foreach (const QMailAddress& recipient, smsRecipients) { if(smsAddress(recipient)) { if(!validSmsAddress(recipient)) { QString temp = "<qt>" + tr("Invalid SMS recipient specified for\n " "mail with subject:\n%1\n" "NO mail has been sent.") .arg( mail.subject() ) + "</qt>"; emit errorOccurred(0,temp); return false; } else { //address is valid, queue for sending // Extract the phone number from the e-mail address. RawSms msg; msg.msgId = mail.id(); msg.number = QPhoneNumber::resolveLetters( recipient.address() ); msg.body = smsBody; if (mail.contentType().content().toLower() == "text/x-vcard") msg.mimetype = QLatin1String("text/x-vCard"); else msg.mimetype = QLatin1String("text/plain"); smsList.append( msg ); } } } return true; }
void tst_QMailMessagePart::testSerialization() { QByteArray data; QByteArray type; type = "text/plain; charset=us-ascii"; data = "P1: This is a plain text part."; QMailMessagePart p1; p1.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), QMailMessageBody::SevenBit, QMailMessageBody::RequiresEncoding)); p1.setContentID("P1"); p1.setContentLocation("After the header"); p1.setContentDescription("The first part"); QCOMPARE( p1.contentType().toString().toLower(), QByteArray("Content-Type: text/plain; charset=us-ascii").toLower() ); QCOMPARE( p1.transferEncoding(), QMailMessageBody::SevenBit ); type = "text/html; charset=UTF-8"; data = "<html>P2: This is a HTML part</html>"; QMailMessageContentType ct(type); ct.setName("P2"); QMailMessageContentDisposition cd(QMailMessageContentDisposition::Inline); cd.setFilename("p2.html"); QMailMessagePart p2; p2.setBody(QMailMessageBody::fromData(data, ct, QMailMessageBody::EightBit, QMailMessageBody::RequiresEncoding)); p2.setContentDisposition(cd); QCOMPARE( p2.contentType().toString().toLower(), QByteArray("Content-Type: text/html; charset=UTF-8; name=P2").toLower() ); QCOMPARE( p2.transferEncoding(), QMailMessageBody::EightBit ); QMailMessage m; m.setTo(QMailAddress("*****@*****.**")); m.setFrom(QMailAddress("*****@*****.**")); m.setDate(QMailTimeStamp("Fri, 22 Jun 2007 11:34:47 +1000")); m.setMultipartType(QMailMessagePartContainer::MultipartAlternative); m.appendPart(p1); m.appendPart(p2); QCOMPARE( m.contentType().toString().toLower(), QByteArray("Content-Type: multipart/alternative").toLower() ); QCOMPARE( m.transferEncoding(), QMailMessageBody::NoEncoding ); const QByteArray expected( "To: [email protected]" CRLF "From: [email protected]" CRLF "Date: Fri, 22 Jun 2007 11:34:47 +1000" CRLF "Content-Type: multipart/alternative; boundary=\"" BOUNDARY "\"" CRLF "MIME-Version: 1.0" CRLF CRLF "This is a multipart message in Mime 1.0 format" CRLF CRLF "--" BOUNDARY CRLF "Content-Type: text/plain; charset=us-ascii" CRLF "Content-Transfer-Encoding: 7bit" CRLF "Content-ID: <P1>" CRLF "Content-Location: After the header" CRLF "Content-Description: The first part" CRLF CRLF "P1: This is a plain text part." CRLF "--" BOUNDARY CRLF "Content-Type: text/html; charset=utf-8; name=P2" CRLF "Content-Transfer-Encoding: 8bit" CRLF "Content-Disposition: inline; filename=p2.html" CRLF CRLF "<html>P2: This is a HTML part</html>" CRLF "--" BOUNDARY "--" CRLF ); QByteArray serialized = m.toRfc2822(); QCOMPARE( serialized, expected ); QMailMessage m2 = QMailMessage::fromRfc2822(serialized); QByteArray repeat = m.toRfc2822(); QCOMPARE( serialized, repeat ); }