Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4120760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:50+00:00 2026-05-20T23:15:50+00:00

I would like to generate emails using the Razor view engine. From what I’ve

  • 0

I would like to generate emails using the Razor view engine.

From what I’ve read, I can use these OS projects to do that from my website:

  • Postal
  • ActionMailer.Net
  • MvcMailer

However, all these use example where the user submits a form and an email is sent from the contoller. When creating the email a ControllerContext seems to be required. I am hoping to do the generating within my “Service” layer at periodic intervals, so I doubt I have access to the ControllerContext.

Is this doable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:15:50+00:00Added an answer on May 20, 2026 at 11:15 pm

    Check this out. I’m using it with templates as embedded resources like this (but can be modified to locate templates anywhere):

    public static class MailSender
    {
        static MailSender()
        {
            CompilerServiceFactory = new DefaultCompilerServiceFactory();
            TemplateService = new TemplateService(CompilerServiceFactory.CreateCompilerService(), typeof(MailTemplate<>));
        }
    
        private static ICompilerServiceFactory CompilerServiceFactory { get; set; }
        private static TemplateService TemplateService { get; set; }
    
        private static ITemplate<T> GetTemplate<T>(T model)
        {
            string path = typeof(T).FullName;
    
            var assembly = Assembly.GetExecutingAssembly();
    
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
            {
                if (stream == null)
                    throw new FileNotFoundException("Mail template not found");
    
                using (var reader = new StreamReader(stream))
                {
                    string source = reader.ReadToEnd();
    
                    return TemplateService.GetTemplate<T>(source, model);
                }
            }
        }
    
        public static void Send<T>(string to, T model)
        {
            Send(new string[] { to }, new string[] { }, new string[] { }, model);
        }
        public static void Send<T>(string to, string cc, string bcc, T model)
        {
            Send(new string[] { to }, new string[] { cc }, new string[] { bcc }, model);
        }
        public static void Send<T>(string[] to, string[] cc, string[] bcc, T model)
        {
            var template = (MailTemplate<T>)GetTemplate<T>(model);
    
            template.Execute();
    
            Trace.WriteLine(string.Format("To: {0} Subject: {1}{3}Body: {2}", string.Join(",", to), template.Subject, template.Result, Environment.NewLine), "Mail");
    #if !LOCAL
            using (var message = new MailMessage())
            using (var client = new SmtpClient())
            {
                if (!string.IsNullOrWhiteSpace(template.From))
                    message.From = new MailAddress(template.From);
    
                message.To.Add(string.Join(",", to));
    
                if (cc != null && cc.Length > 0)
                    message.CC.Add(string.Join(",", cc));
    
                if (bcc != null && bcc.Length > 0)
                    message.Bcc.Add(string.Join(",", bcc));
    
                message.Subject = template.Subject;
    
                message.Body = template.Result;
                message.IsBodyHtml = true;
    
                client.Send(message);
            }
    #endif
        }
    }
    
    public abstract class MailTemplate<TModel> : TemplateBase<TModel>
    {
        public string From { get; set; }
        public string Subject { get; set; }
    }
    

    model:

    public class Contact
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Text { get; set; }
    }
    

    template:

    @inherits Feedback.Lib.Mail.MailTemplate<Feedback.Lib.Mail.Templates.Contact>
    @{
        From = string.Format("{0} <{1}>", Model.Name, Model.Email);
        Subject = "Contact request - " + From; 
    }
    
    @Model.Text
    

    I have for each template it’s own model, so its easy to locate them via class name. Also I was unable to make intellisense work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use the Razor View Engine outside of ASP.NET MVC to generate
I would like to generate a patch with all commits from a local branch.
I would like to generate the mysql-password hash from js. I know the method
I am creating a function that I would like to generate random strings from
I am using Ruby on Rails 3.1.0 and I would like to properly generate
We would like to use Selenium to test email content generated from our website.
I would like to use OpenId on my site to allow user registration using
I would like to generate email inside a Struts2 application, using Freemarker. As I
I would like to generate POJO, XML for a given database. Database: ( contents
i would like to generate a very simple report with some images and text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.