1

I'm a beginner in ASP.net MVC and I have a problem when I want to send my view by email with my controller. I do not arrive to send it, I just arrive send some text

About what I have already tried, I'm gonna show you with the code.

(If my problem is not understandable tel me and I explain differently)


// Page MailHelper.cs
// The tools for sending mail

        public static void SendMail(string body)
        {
            try
            {
                var mailto = ConfigurationManager.AppSettings["mailto"].Split(';');

                MailMessage mail = new MailMessage();
                mail.IsBodyHtml = true;
                SmtpClient SmtpServer = new SmtpClient(); 

                mail.From = new MailAddress("SendVersionsRobot@trysomething.fr");
                foreach (var m in mailto)   
                    mail.To.Add(m);    
                mail.Subject = "try send Mail";  
                mail.Body = body;     
                SmtpServer.Send(mail);   
            }
            catch (Exception ex)
            {

            }
        }

// Page MailController.cs
using....

namespace MvcMovie.Controllers
{
    public class MailController : Controller
    {
        public ActionResult pageDeTest()
        {
            MailHelper.SendMail(pageDeTest());
            return View();
        }
    }


}


<div id="demo"></div>


<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $.getJSON("..\\..\\jsconfig.json", function (data) {
        $('#demo').html(JSON.stringify(data));
    });

    /*console.log("try");*/
</script>


  • 1
    Possible duplicate of [How to render an ASP.NET MVC view as a string?](https://stackoverflow.com/questions/483091/how-to-render-an-asp-net-mvc-view-as-a-string) – CodeCaster Jul 11 '19 at 15:09
  • Thanks for your answer CodeCaster but I don't understand how does it works on this post..so if someone can help me – Alexandre Bretelle Jul 12 '19 at 08:57

1 Answers1

0

Currently my page MailController.cs had evolve like that:

namespace MvcMovie.Controllers { public class MailController : Controller { public ActionResult pageDeTest() { string myString = RenderPartialViewToString(MailHelper.SendMail()); MailHelper.SendMail(myString); return View(); }

    protected string RenderPartialViewToString(ControllerContext context, string viewName, object model)
    {
        var controller = context.Controller;

        if (string.IsNullOrEmpty(viewName))
            viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }