private void CreateScriptParts(Message message, string baseUrl)
        {
            var html = message.HTMLBody;

            //unlikey to have same js multiple times...
            foreach (Match m in Patterns.ScriptPattern.Matches(html))
            {
                var scriptUrl         = m.Groups["Url"].Value;
                var absoluteScriptUrl = _webRequestHelper.GetAbsoluteUrl(baseUrl, scriptUrl);
                var id = Guid.NewGuid().ToString();

                var scriptPart = message.BodyPart.AddBodyPart();
                scriptPart.ContentMediaType        = "text/javascript";
                scriptPart.Charset                 = "iso-8859-1";
                scriptPart.ContentTransferEncoding = "quoted-printable";
                scriptPart.Fields["urn:schemas:mailheader:content-id"].Value          = id;
                scriptPart.Fields["urn:schemas:mailheader:content-disposition"].Value = "inline";
                scriptPart.Fields.Update();

                var stream        = scriptPart.GetDecodedContentStream();
                var scriptStream  = (IStream)stream;
                var scriptContent = _webRequestHelper.GetContent(absoluteScriptUrl);
                scriptStream.Write(scriptContent.Content, scriptContent.Content.Length, IntPtr.Zero);
                stream.Flush();
                stream.Close();

                message.HTMLBody = message.HTMLBody.Replace(scriptUrl, "cid:" + id);
            }
        }
Example #2
0
        public byte[] GetMHTML(string url)
        {
            byte[] content;

            var htmlContent = _webRequestHelper.GetContent(url).Content;
            var htmlString  = new StringBuilder(Encoding.UTF8.GetString(htmlContent));

            var message = new MailMessage {
                From = new MailAddress("*****@*****.**")
            };

            var resources = new List <LinkedResource>();
            var urlCheck  = new Dictionary <string, string>();

            CreateParts(resources, urlCheck, url, htmlString, Patterns.ScriptPattern);
            CreateParts(resources, urlCheck, url, htmlString, Patterns.LinkPattern);
            CreateParts(resources, urlCheck, url, htmlString, Patterns.ImagePattern);

            var view = AlternateView.CreateAlternateViewFromString(htmlString.ToString(), new ContentType("text/html"));

            view.TransferEncoding = TransferEncoding.SevenBit;

            foreach (var resource in resources)
            {
                view.LinkedResources.Add(resource);
            }

            message.AlternateViews.Add(view);

            using (var ms = new MemoryStream())
            {
                message.Write(ms);
                content = new byte[ms.Length];

                var readBytes = 0;
                ms.Position = 0;
                while (readBytes < ms.Length)
                {
                    readBytes += ms.Read(content, 0, (int)ms.Length - readBytes);
                }
            }

            return(content);
        }