コード例 #1
0
ファイル: EmailTests.cs プロジェクト: mcshaz/SimPlanner
        public void WriteMail(EmailBase template)
        {
            HttpContext.Current = new HttpContext(
                new HttpRequest(null, "https://sim-planner.com", null),
                new HttpResponse(new StringWriter())
                );

            // User is logged in
            HttpContext.Current.User = TestICal.GetTestAllRolesPrincipal();

            using (var msg = new MailMessage("*****@*****.**", HttpContext.Current.User.Identity.Name))
            {
                string dir = Path.GetFullPath("TestEmails");
                Directory.CreateDirectory(dir);
                msg.CreateHtmlBody(template);
                using (SmtpClient client = new SmtpClient("mysmtphost"))
                {
                    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    client.PickupDirectoryLocation = dir;
                    client.Send(msg);
                }
                string fileName = ((object)template).GetType().Name;
                File.WriteAllText(Path.Combine(dir, fileName + ".txt"), msg.Body);
                var htmlStream = msg.AlternateViews.First(av => av.ContentType.MediaType == MediaTypeNames.Text.Html).ContentStream;
                using (var fileStream = File.Create(Path.Combine(dir, fileName + ".html")))
                {
                    htmlStream.Seek(0, SeekOrigin.Begin);
                    htmlStream.CopyTo(fileStream);
                }
            }

        }
コード例 #2
0
ファイル: MailExtensions.cs プロジェクト: mcshaz/SimPlanner
        public static void CreateHtmlBody(this MailMessage message, EmailBase template)
        {
            var html = template.TransformText();

            message.Subject = ((LayoutTemplate)template.Layout)?.Title ?? "Sim-planner email";

            message.Body = message.Subject + new string('-', message.Subject.Length) + "\r\n\r\n" + HtmlToText.ConvertHtml(html);

            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html));
        }