Skip to content
wduffy edited this page Jun 10, 2011 · 3 revisions

There are two key elements to sending a RazorMail.

  1. A RazorMailMessage which is responsible for rendering the final email body
  2. An IRazorMailSender which asks a RazorMailMessage to render its templates and emails the result via SMTP

The default IRazorMailSender is RazorMailSender, which requires a minimum of two arguments to construct.

  1. sender: The MailAddress to be used as the sender for any messages it handles. This is NOT the from address, however, if a from address is not specified it will be used for this purpose.
  2. baseUri: The uri that represents the location that will handle all relative links on emails. Any rooted or relative urls are resolved to base urls using this before sending.

A Simple Example: Requires smtp handling on the active machine

var mailsender = new RazorMailSender(new MailAddress("business@domain.com", "Business Name"), HttpContext.Request.Url);
var message = new RazorMailMessage("Test Message");

message.From = new MailAddress("from@domain.com", "From Name");
message.To.Add("recipient@domain.com", "Recipient Name");

message.Templates.Add("This is a RazorMail test by @Model.Name");
message.Set.Name = "William Duffy";

mailsender.Send(message);

Custom SMTP Settings Example

var smtp = new SmtpClient("smtp.domain.com") { Credentials = new NetworkCredential("username", "password") };
var mailsender = new RazorMailSender(new MailAddress("business@domain.com", "Business Name"), HttpContext.Request.Url, null, smtp);
var message = new RazorMailMessage("Test Message");

message.From = new MailAddress("from@domain.com", "From Name");
message.To.Add("recipient@domain.com", "Recipient Name");

message.Templates.Add("This is a RazorMail test by @Model.Name");
message.Set.Name = "William Duffy";

mailsender.Send(message);