{"id":25303672,"url":"https://github.com/xavierjefferson/snork.serializablemail","last_synced_at":"2025-04-07T02:35:36.151Z","repository":{"id":38135852,"uuid":"238752498","full_name":"xavierjefferson/Snork.SerializableMail","owner":"xavierjefferson","description":"A drop-in replacement library for System.Net.Mail.MailMessage with objects that can be serialized as well as sent out with SmtpClient.","archived":false,"fork":false,"pushed_at":"2022-06-23T00:41:36.000Z","size":1353,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T20:38:37.135Z","etag":null,"topics":["csharp","mail","mailmessage"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xavierjefferson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-06T18:08:08.000Z","updated_at":"2022-06-22T23:44:55.000Z","dependencies_parsed_at":"2022-09-18T01:30:32.466Z","dependency_job_id":null,"html_url":"https://github.com/xavierjefferson/Snork.SerializableMail","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xavierjefferson%2FSnork.SerializableMail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xavierjefferson%2FSnork.SerializableMail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xavierjefferson%2FSnork.SerializableMail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xavierjefferson%2FSnork.SerializableMail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xavierjefferson","download_url":"https://codeload.github.com/xavierjefferson/Snork.SerializableMail/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247583107,"owners_count":20961970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","mail","mailmessage"],"created_at":"2025-02-13T07:39:50.263Z","updated_at":"2025-04-07T02:35:36.120Z","avatar_url":"https://github.com/xavierjefferson.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Snork.SerializableMail\n[![Latest version](https://img.shields.io/nuget/v/Snork.SerializableMail.svg)](https://www.nuget.org/packages/Snork.SerializableMail/) \n\nThis is a library for C#.  In normal conditions, the following objects aren't serializable:\n\n - System.Net.Mail.Message\n - System.Net.Mail.MailAddress\n - System.Net.Mail.Attachment\n - System.Net.Mail.AddressCollection\n - System.Net.Mime.ContentDisposition\n - System.Net.Mime.ContentType\n\nThis means that if you're interested in serializing the contents of a mail message to binary, XML, or JSON, you're normally going to be out of luck.\n\nSnork.SerializableMail does implicit conversions from the built-in .NET types to instances of substitute classes that will serialize just fine.   This includes addresses, text encoding settings, and even the attachments.\n\nThese substitute classes also do implicit conversions **back** to the objects in the System.Net.Mail namespace, with all properties, so it's just as easy to deserialize any serialized messages and send them using `SmtpClient.Send`.\n\n## Sample Code (needs Newtonsoft.JSON package)\n    using System;\n    using System.IO;\n    using System.Net.Mail;\n    using System.Text;\n    using System.Xml.Serialization;\n    using Newtonsoft.Json;\n    using Snork.SerializableMail;\n    \n    namespace SampleApplication\n    {\n        internal class Program\n        {\n            private static void Main(string[] args)\n            {\n                //build a message\n                var mailMessage = new MailMessage(\"fromme@somewhere.com\", \"toyou@somewhere.com\");\n                mailMessage.Subject = \"This is the subject\";\n                mailMessage.Body = \"This is the body\";\n                mailMessage.IsBodyHtml = true;\n                mailMessage.Headers[\"X-Something-Here\"] = \"something we don't need\";\n    \n                //create a memory stream for the attachment\n                var memoryStream = new MemoryStream();\n                using (var streamWriter = new StreamWriter(memoryStream, Encoding.Unicode, 1000, true))\n                {\n                    streamWriter.Write(\"This is the attachment contents\");\n                }\n                //move pointer back to zero\n                memoryStream.Position = 0;\n                var attachment = new Attachment(memoryStream, \"somefile.txt\", \"text/plain\");\n                attachment.ContentDisposition.Parameters[\"abc\"] = \"def\";\n                mailMessage.Attachments.Add(attachment);\n    \n                Console.WriteLine(\"JSON Version:\");\n                Console.WriteLine(SerializeToJson(mailMessage));\n                Console.WriteLine();\n                Console.WriteLine(\"XML Version:\");\n                Console.WriteLine(SerializeToXml(mailMessage));\n\n                //do an implicit conversion here\n                SerializableMailMessage sendMe = mailMessage;\n                using(var client = new SmtpClient()){\n                    //this will convert sendMe *back* to System.Net.MailMessage.\n                    client.Send(sendMe);\n                }\n                Console.ReadLine();\n\n            }\n    \n            private static string SerializeToXml(MailMessage mailMessage)\n            {\n                //perform implicit conversion between types here\n                SerializableMessage newMessage = mailMessage;\n                var xmlSerializer = new XmlSerializer(typeof(SerializableMessage));\n                using (var stringWriter = new StringWriter())\n                {\n                    xmlSerializer.Serialize(stringWriter, newMessage);\n                    return stringWriter.ToString();\n                }\n            }\n    \n            private static string SerializeToJson(MailMessage mailMessage)\n            {\n                //perform implicit conversion between types here\n                SerializableMessage newMessage = mailMessage;\n                return JsonConvert.SerializeObject(newMessage, Formatting.Indented);\n            }\n        }\n    }\n\n## JSON output\n    {\n      \"Headers\": {\n        \"X-Something-Here\": \"something we don't need\"\n      },\n      \"Body\": \"This is the body\",\n      \"Subject\": \"This is the subject\",\n      \"From\": {\n        \"DisplayNameCodePage\": 65001,\n        \"Address\": \"fromme@somewhere.com\",\n        \"DisplayName\": \"\"\n      },\n      \"To\": [\n        {\n          \"DisplayNameCodePage\": 65001,\n          \"Address\": \"toyou@somewhere.com\",\n          \"DisplayName\": \"\"\n        }\n      ],\n      \"ReplyToList\": [],\n      \"CC\": [],\n      \"Bcc\": [],\n      \"BodyTransferEncoding\": -1,\n      \"IsBodyHtml\": true,\n      \"HeadersCodePage\": null,\n      \"BodyCodePage\": 20127,\n      \"Sender\": null,\n      \"DeliveryNotificationOptions\": 0,\n      \"Attachments\": [\n        {\n          \"Name\": \"somefile.txt\",\n          \"NameEncodingCodePage\": null,\n          \"ContentDisposition\": {\n            \"Parameters\": {\n              \"abc\": \"def\"\n            },\n            \"FileName\": null,\n            \"DispositionType\": \"attachment\",\n            \"Inline\": false,\n            \"Boundary\": null,\n            \"MediaDisposition\": null,\n            \"CreationDate\": \"0001-01-01T00:00:00\",\n            \"ModificationDate\": \"0001-01-01T00:00:00\",\n            \"Size\": -1,\n            \"ReadDate\": \"0001-01-01T00:00:00\"\n          },\n          \"ContentId\": \"0cccface-1b83-45c7-9802-e77c471c1587\",\n          \"ContentType\": {\n            \"Name\": \"somefile.txt\",\n            \"CharSet\": null,\n            \"Boundary\": null,\n            \"MediaType\": \"text/plain\"\n          },\n          \"ContentBytes\": \"//5UAGgAaQBzACAAaQBzACAAdABoAGUAIABhAHQAdABhAGMAaABtAGUAbgB0ACAAYwBvAG4AdABlAG4AdABzAA==\",\n          \"TransferEncoding\": 1\n        }\n      ],\n      \"Priority\": 0,\n      \"SubjectCodePage\": null\n    }\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxavierjefferson%2Fsnork.serializablemail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxavierjefferson%2Fsnork.serializablemail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxavierjefferson%2Fsnork.serializablemail/lists"}