{"id":1986,"date":"2013-09-26T18:39:16","date_gmt":"2013-09-26T16:39:16","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1986"},"modified":"2014-04-15T14:22:38","modified_gmt":"2014-04-15T12:22:38","slug":"remove-attachments-from-email","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/remove-attachments-from-email","title":{"rendered":"Remove attachments from email"},"content":{"rendered":"<div class=\"well\">\n<ul>\n<li><a href =\"\/blog\/replace-attachments-in-email-message\">Replace attachments in email message<\/a><\/li>\n<li>Remove attachments from email<\/li>\n<\/ul>\n<\/div>\n<p>First, there is one thing you must be aware of: <strong>neither POP3 nor IMAP protocol provide a way to remove attachments from existing emails<\/strong>. This is because email stored on the server is immutable. With IMAP protocol you can copy email message to different folder, you can apply some flags (\\SEEN) to it, but you can&#8217;t change any part of the message.<\/p>\n<p>The second important thing is, that attachments are not stored separately from the message text and headers &#8211; they are embedded inside the email.<\/p>\n<p>Nevertheless Mail.dll provides an easy way to remove attachments from existing email message.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = new MailBuilder().CreateFromEml(eml);\r\nemail.RemoveAttachments();\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.RemoveAttachments()\r\n<\/pre>\n<p><em>RemoveAttachments <\/em>method has an overloaded version, that allows you to <strong>skip visual elements<\/strong> (content-disposition: inline) or\/and alternative email representations:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail email = new MailBuilder().CreateFromEml(eml);\r\n\r\nAttachmentRemoverConfiguration configuration = new AttachmentRemoverConfiguration();\r\nconfiguration.RemoveVisuals = false;\r\n\r\nemail.RemoveAttachments(configuration);\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\n\r\nDim email As IMail = New MailBuilder().CreateFromEml(eml)\r\n\r\nDim configuration As New AttachmentRemoverConfiguration()\r\nconfiguration.RemoveVisuals = False\r\n\r\nemail.RemoveAttachments(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 without attachments, and finally <strong>uploading <\/strong>it, and removing the original message:<\/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.RemoveAttachments();\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.RemoveAttachments()\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 First, there is one thing you must be aware of: neither POP3 nor IMAP protocol provide a way to remove attachments from existing emails. This is because email stored on the server is immutable. With IMAP protocol you can copy email message to different folder, you [&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-1986","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\/1986"}],"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=1986"}],"version-history":[{"count":11,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"predecessor-version":[{"id":4023,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1986\/revisions\/4023"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}