1

I want to send text, title of username into an email that I have but how do I add these options so I can call them over to the html file a bit like this here.

MailDefinition oMailDefinition = new MailDefinition();
oMailDefinition.BodyFileName = "~/img/emailskabelon/NewPassword.html";
oMailDefinition.From = Mail;

Dictionary<string, string> oReplacements = new Dictionary<string, string>();
oReplacements.Add("<<name>>", name);
oReplacements.Add("<<password>>", password);

I would therefore like to add some of the values that I would like to have in my html file.

It might be that I should send text, username and other things to the html file and I would like that option.

public async static void Sendmail(string email, string name, string Title, string htmlContent)
    {
        var api = AzureName;
        var client = new SendGridClient(api);
        var from = new EmailAddress(Mail, nameFrom);
        var to = new EmailAddress(email, name);
        var plainTextContent = Regex.Replace(htmlContent, "<[^>]*>", "");
        var msg = MailHelper.CreateSingleEmail(from, to, Title, plainTextContent, htmlContent);
        var response = client.SendEmailAsync(msg);
        await Task.Delay(4255);
    }
Jesper
  • 63
  • 9
  • Have you heard of Razor? It might be a bit overkill but it's a way of generating html file from a special html like template and passing in a view model, that way you get intellisense when creating the view and the rendering engine is already written for you. – Jamie Twells Feb 12 '18 at 21:48
  • No, I do not really know what you're talking about. Do you have a link? @JamieTwells – Jesper Feb 12 '18 at 21:49
  • Well it's quite a simple concept, you create a cshtml file which is just an html file really, same syntax, but you can also write stuff like: `

    @model.User.FirstName @model.User.LastName

    ` etc, then make a model object in C#, like `var model = new MyModel{ User = new User { FirstName = "Jamie", LastName = "Twells" } }` then do something like (I forget the exact syntax) `var myHtml = Razor.Render("path/to/razor/file", model);` and it gives you the html as a string with the variables replaced.
    – Jamie Twells Feb 12 '18 at 22:11
  • Maybe this helps? https://stackoverflow.com/a/37296765/1830205 – Jamie Twells Feb 12 '18 at 22:12
  • 1
    @JamieTwells https://stackoverflow.com/questions/40912375/return-view-as-string-in-net-core is it also good? – Jesper Feb 12 '18 at 22:29

0 Answers0