简体   繁体   中英

MVC4 : Save as html and email it

I know i am asking for information here but seems like a relevant question and i am getting no ideas how to do it . please help.

Problem: We have requirements to send an email with a html attachment.

Sending E-mail part is taken care of as a part of our APT but the problem is with the html attachment.

Requirement: we have to send the current view which is displayed on the UI as a html file.

How can i get the full html in the controller from the view?

  Attachment = new AttachmentContract
            {
                Path = AppSettings.NoticeAttachmentFilePath,
                FileName = "filenamehtml",
                Content = <<here we want to set that html>>
            }

You could try using something like this:

https://github.com/smsohan/MvcMailer

Found out the solution guys with the help of some other question here's what i am doing

 Attachment = new AttachmentContract
        {
            Path = AppSettings.NoticeAttachmentFilePath,
            FileName = "filenamehtml",
            Content = testmethod()
        }

 private string TestMethod(ModelData modelData)
    {

        ViewData.Model = modelData;
        StringWriter output;
        using (output = new StringWriter())
        {
            var result = ViewEngines.Engines.FindView(this.ControllerContext, "EmailAttachment", null);
            var viewContext = new ViewContext(this.ControllerContext, result.View, this.ViewData, this.ControllerContext.Controller.TempData, output);
            result.View.Render(viewContext, output);
            result.ViewEngine.ReleaseView(this.ControllerContext, result.View);
        }

        return output.ToString();
    }

Where EmailAttachment is a view i am going to fill in my logic using the same model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM