Overview
Features
Download
Documentation
Community
Add-Ons & Services

Adding additional headers to Poco::Net::MailMessage

General discussion regarding the development of POCO for contributors.

Adding additional headers to Poco::Net::MailMessage

Postby bob » 14 Jan 2011, 23:00

I needed a way to set the 'Content-Id' on message parts to allow cid urls to work. The following patch allows adding additional headers to mime parts.

Code: Select all
Index: Net/include/Poco/Net/MailMessage.h
===================================================================
--- Net/include/Poco/Net/MailMessage.h   (revision 1431)
+++ Net/include/Poco/Net/MailMessage.h   (working copy)
@@ -161,6 +161,11 @@
   bool isMultipart() const;
      /// Returns true iff the message is a multipart message.

+   void setPartHeader( const std::string& part, const std::string& name, const std::string& value);
+      /// Adds the specified name/value pair to the part headers
+      ///
+      /// Passing an empty string as the value will delete the header value
+
   void addPart(const std::string& name, PartSource* pSource, ContentDisposition disposition, ContentTransferEncoding encoding);
      /// Adds a part/attachment to the mail message.
      ///
@@ -222,12 +227,14 @@
      /// consists only of ASCII characters.

protected:
+   typedef std::map<std::string, std::string> StringMap;
   struct Part
   {
      std::string             name;
      PartSource*             pSource;
      ContentDisposition      disposition;
      ContentTransferEncoding encoding;
+      StringMap            headers;
   };
   typedef std::vector<Part> PartVec;

Index: Net/src/MailMessage.cpp
===================================================================
--- Net/src/MailMessage.cpp   (revision 1431)
+++ Net/src/MailMessage.cpp   (working copy)
@@ -226,7 +226,23 @@
   _parts.push_back(part);
}

+void MailMessage::setPartHeader( const std::string& part, const std::string& name, const std::string& value)
+{
+   if ( !part.length() || !name.length() )
+      return;

+   for (PartVec::iterator it = _parts.begin(); it != _parts.end(); ++it)
+      if (part == it->name)
+      {   
+         if ( value.length() )
+            it->headers[ name ] = value;
+         else
+            it->headers.erase( name );
+
+         return;
+      }
+}
+
void MailMessage::addContent(PartSource* pSource, ContentTransferEncoding encoding)
{
   addPart("", pSource, CONTENT_INLINE, encoding);
@@ -335,6 +351,8 @@
   }
   else disposition = "inline";
   partHeader.set(HEADER_CONTENT_DISPOSITION, disposition);
+   for (StringMap::const_iterator it = part.headers.begin(); it != part.headers.end(); ++it)
+      partHeader.set(it->first, it->second);
   writer.nextPart(partHeader);
   writeEncoded(part.pSource->stream(), writer.stream(), part.encoding);
}
bob
 
Posts: 3
Joined: 14 Jan 2011, 22:16

Re: Adding additional headers to Poco::Net::MailMessage

Postby guenter » 22 Jan 2011, 18:48

For 1.4.1, I have implemented this in a different way: You can now specify additional header fields directly in the PartSource. PartSource has a headers() method that returns a reference to a MessageHeader object. You can set any header fields you like in that, and these will be included in the MIME header.
guenter
 
Posts: 1041
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Contributors

Who is online

Users browsing this forum: No registered users and 2 guests