{"id":4294,"date":"2013-09-27T15:07:07","date_gmt":"2013-09-27T13:07:07","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=4294"},"modified":"2014-04-15T14:22:44","modified_gmt":"2014-04-15T12:22:44","slug":"replace-attachments-in-email-message","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/replace-attachments-in-email-message","title":{"rendered":"Replace attachments in email message"},"content":{"rendered":"<div class=\"well\">\n<ul>\n<li>Replace attachments in email message<\/li>\n<li><a href =\"\/blog\/remove-attachments-from-email\">Remove attachments from email<\/a><\/li>\n<\/ul>\n<\/div>\n<p>Attachments are not stored separately from message text and headers &#8211; they are embedded inside an email message. This, along with inefficient Base64 encoding is the most important reason of email messages being large in size. Mail.dll provides an easy way to replace attachments in existing messages:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = new MailBuilder().CreateFromEml(eml);\r\nemail.ReplaceAttachments();\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = New MailBuilder().CreateFromEml(eml)\r\nemail.ReplaceAttachments()\r\n<\/pre>\n<p>Each attachment will be replaced with the following text information: &#8220;This file (&#8216;[FileName]&#8217;) containing [Size] bytes of data was removed.&#8221;. Thus making email much smaller in size.<\/p>\n<p><em>ReplaceAttachments<\/em>method has an overloaded version, that allows you to <strong>skip visual elements<\/strong> (content-disposition: inline) or\/and alternative email representations. It also allows to specify text template and custom <em>Tag<\/em>, that can be used, for example, to create a custom url. This url can point to a place to which attachment was moved.<\/p>\n<p>Within the template you can use [FileName], [Size] and [Tag] as template placeholders.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = Limilabs.Mail.Fluent.Mail\r\n    .Text(&quot;body&quot;)\r\n    .AddAttachment(new byte&#x5B;] { 1, 2, 3 })\r\n    .SetFileName(&quot;report.pdf&quot;)\r\n    .Create();\r\n\r\nAttachmentReplacerConfiguration configuration = new AttachmentReplacerConfiguration();\r\nconfiguration.ReplaceVisuals = false;\r\nconfiguration.Tag = \r\n    att =&gt; &quot;http:\/\/example.com\/&quot; + email.MessageID + &quot;\/&quot; + att.FileName;\r\nconfiguration.Template = \r\n    &quot;Attachment &#x5B;FileName] removed. You can download it here: &#x5B;Tag]&quot;;\r\n\r\nemail.ReplaceAttachments(configuration);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim email As IMail = Limilabs.Mail.Fluent.Mail _\r\n    .Text(&quot;body&quot;) _\r\n    .AddAttachment(New Byte() {1, 2, 3}) _\r\n    .SetFileName(&quot;report.pdf&quot;) _\r\n    .Create()\r\n\r\nDim configuration As New AttachmentReplacerConfiguration()\r\nconfiguration.ReplaceVisuals = False\r\nconfiguration.Tag = Function(att)\r\n    Return &quot;http:\/\/example.com\/&quot; + email.MessageID + &quot;\/&quot; + att.FileName\r\n    End Function\r\nconfiguration.Template = _\r\n    &quot;Attachment &#x5B;FileName] removed. You can download it here: &#x5B;Tag]&quot;\r\n\r\nemail.ReplaceAttachments(configuration)\r\n<\/pre>\n<p>The following example illustrates the full process of <strong>downloading <\/strong>email from IMAP server,<br \/>\ncreating new email, with the same information, but with all attachments replaced, <strong>uploading<\/strong> this message, and deleting original one:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing(Imap imap = new Imap())\r\n{\r\n    imap.ConnectSSL(&quot;imap.example.org&quot;);\r\n    imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    imap.SelectInbox();\r\n\r\n    foreach (long uid in imap.GetAll())\r\n    {\r\n        var eml = imap.GetMessageByUID(uid);\r\n        IMail email = new MailBuilder().CreateFromEml(eml);\r\n        if (email.Attachments.Count &gt; 0)\r\n        {\r\n            email.ReplaceAttachments();\r\n\r\n            imap.UploadMessage(email);\r\n\r\n            imap.DeleteMessageByUID(uid);\r\n        }\r\n    }\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing imap As New Imap()\r\n   imap.ConnectSSL(&quot;imap.example.org&quot;)\r\n   imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n   imap.SelectInbox()\r\n\r\n   For Each uid As Long In imap.GetAll()\r\n      Dim eml = imap.GetMessageByUID(uid)\r\n      Dim email As IMail = New MailBuilder().CreateFromEml(eml)\r\n      If email.Attachments.Count &gt; 0 Then\r\n         email.ReplaceAttachments()\r\n\r\n         imap.UploadMessage(email)\r\n\r\n         imap.DeleteMessageByUID(uid)\r\n      End If\r\n   Next\r\n   imap.Close()\r\nEnd Using\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Replace attachments in email message Remove attachments from email Attachments are not stored separately from message text and headers &#8211; they are embedded inside an email message. This, along with inefficient Base64 encoding is the most important reason of email messages being large in size. Mail.dll provides an easy way to replace attachments in existing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[74,15,33,28,77,57],"class_list":["post-4294","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","tag-c","tag-email-component","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4294"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=4294"}],"version-history":[{"count":11,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4294\/revisions"}],"predecessor-version":[{"id":4529,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4294\/revisions\/4529"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=4294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=4294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=4294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}