{"id":701,"date":"2014-05-14T22:00:15","date_gmt":"2014-05-14T21:00:15","guid":{"rendered":"http:\/\/coding.moris.org\/?p=701"},"modified":"2021-11-19T20:50:59","modified_gmt":"2021-11-19T20:50:59","slug":"hacking-ssl-support-into-smtpop-dll","status":"publish","type":"post","link":"https:\/\/priscimon.net\/coding\/2014\/05\/14\/hacking-ssl-support-into-smtpop-dll\/","title":{"rendered":"Hacking SSL support into smtpop.dll"},"content":{"rendered":"\n<p>We use <em>smtpop.dll<\/em> in one application to retrieve email from a POP3 mailbox. Today we had to connect to a mailbox over SSL, which <em>smtpop.dll<\/em> does not support.<\/p>\n\n\n\n<p>Our code to retrieve email is abstracted behind a fa\u00c3\u00a7ade class, so I expected to simply substitute a new library for <em>smtpop.dll<\/em> and to call new methods. However, the tight coupling of the fa\u00c3\u00a7ade to the interface of <em>smtpop.dll<\/em> meant that we needed the replacement to also expose the exact same interface.<\/p>\n\n\n\n<p>After trying several things, I resigned to create a new class with the code from the decompilation of <em>smtpop.dll<\/em>. Fortunately, only two methods, <code>Open<\/code> and <code>Quit<\/code>, had to be changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>namespace ClassLibrary4\n{\n    using System.IO;\n    using System.Net.Security;\n    using System.Net.Sockets;\n\n    using SmtPop;\n\n    public class Pop3ClientWithSsl : POP3Client\n    {\n        #region Fields\n\n        private SslStream sslStream;\n\n        #endregion\n\n        #region Constructors and Destructors\n\n        public Pop3ClientWithSsl()\n        {\n            this.UseSsl = true;\n        }\n\n        #endregion\n\n        #region Public Properties\n\n        public bool UseSsl { get; set; }\n\n        #endregion\n\n        #region Public Methods and Operators\n\n        public new int Open(string hostname, int port, string username, string password)\n        {\n            if (this.UseSsl)\n            {\n                return this.OpenWithSsl(hostname, port, username, password);\n            }\n\n            return base.Open(hostname, port, username, password);\n        }\n\n        public new string Quit()\n        {\n            try\n            {\n                return base.Quit();\n            }\n            finally\n            {\n                this.m_streamReader.Close();\n                this.m_streamWriter.Close();\n                if (this.UseSsl)\n                {\n                    this.sslStream.Close();\n                }\n            }\n        }\n\n        #endregion\n\n        #region Methods\n\n        private int OpenWithSsl(string hostname, int port, string username, string password)\n        {\n            this.m_host = hostname;\n            this.m_port = port;\n            this.m_user = username;\n            this.m_tcpClient = new TcpClient(hostname, port);\n\n            this.m_netStream = this.m_tcpClient.GetStream();\n            this.sslStream = new SslStream(this.m_netStream, false);\n            this.sslStream.AuthenticateAsClient(hostname);\n\n            this.m_streamReader = new StreamReader(this.sslStream);\n            this.m_streamWriter = new StreamWriter(this.sslStream) { AutoFlush = true };\n\n            string welcome = this.m_streamReader.ReadLine();\n            if (welcome != null &amp;&amp; welcome.StartsWith(+OK))\n            {\n                return this.SendLogin(username, password);\n            }\n\n            this.m_error = welcome;\n            return -1;\n        }\n\n        #endregion\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>The methods of class <code>POP3Client<\/code> were not virtual, but some of the class members were in <code>protected<\/code> scope and were accessible in the new class. I rewrote the <code>Open<\/code> and <code>Quit<\/code> methods as <code>new<\/code> methods, which made them no longer polymorphic, thus forcing us to replace calls to <code>POP3Client<\/code> with calls to <code>Pop3ClientWithSsl<\/code> everywhere in the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use smtpop.dll in one application to retrieve email from a POP3 mailbox. Today we had to connect to a mailbox over SSL, which smtpop.dll does not support. Our code to retrieve email is abstracted behind a fa\u00c3\u00a7ade class, so I expected to simply substitute a new library for smtpop.dll and to call new methods. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-701","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3I4g9-bj","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":36,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/701\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/media?parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/categories?post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/tags?post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}