-
Notifications
You must be signed in to change notification settings - Fork 1
Embedding templates in your domain
RazorMail supports three types of templates.
- A string
- A physical file
- An embedded resource
The examples below demonstrate the different RazorMailMessage template options in action.
###String Example
var message = new RazorMailMessage("String Template Example");
message.Templates.Add("This is a RazorMail test by @Model.Name");
message.Set.Name = "William Duffy";
###Physical File Example This example uses a hard coded file path, however most likely the path would be loaded by a file handler configurable outwith your domain, specific to the environment the code is running in. Templates.Add() requires an absolute file path.
var message = new RazorMailMessage("Physical File Template Example");
message.Templates.Add("C:\\\Files\\\Templates\\\PhysicalFileExample.cshtml");
message.Set.Name = "William Duffy";
###Embedded Resource Example In order for a resource to be embedded in your domain you must alter the propeties of the file. Set the 'Build Action' property to 'Embedded Resource'. The resource name will be Assembly.Folders.File.Extension. For example a file named Test.cshtml, in the project 'MyProject' folder '/Resources/Examples' would be MyProject.Resources.Examples.Test.cshtml. RazorMail only needs the part AFTER the Assembly. In the example above this would be Resources.Examples.Test.cshtml. Notice MyProject. is not included here.
IMPORTANT: The resource must be embedded within the assembly that calls the RazorMailMessage.AddTemplate() function.
var message = new RazorMailMessage("Embedded Resource Example");
message.Templates.Add("Resources.Email.EmbeddedResourceExample.cshtml");
message.Set.Name = "William Duffy";
Should embedded resources be located within a different assembly you can pass a reference to it via the constructor.
var message = new RazorMailMessage("Embedded Resource Example", Assembly.GetAssembly(typeof(YourClass)));
message.Templates.Add("Resources.Email.EmbeddedResourceExample.cshtml");
message.Set.Name = "William Duffy";