开发者

ASP.NET Send .htm file as email template

开发者 https://www.devze.com 2023-01-14 10:12 出处:网络
Hi c开发者_StackOverflow中文版an you send a .htm file as the html template using the mailMessage class? I cant seem to work out how to do it?Assuming you have created an html template that has markers

Hi c开发者_StackOverflow中文版an you send a .htm file as the html template using the mailMessage class? I cant seem to work out how to do it?


Assuming you have created an html template that has markers (e.g. [[[FROM]]]), you then need to use StringBuilder. For example:

public static string GetEmailTemplateContent(string path)
{
    string emailHtmlTemplate = "";
    StreamReader rdr = null;
    try
    {
        rdr = new StreamReader(path);
        emailHtmlTemplate = rdr.ReadToEnd();
    }
    catch (IOException ex)
    {
        throw new Exception(ex.Message.ToString());
    }
    finally
    {
        if (rdr != null)
        {
            rdr.Close();
            rdr.Dispose();
        }
    }
    return emailHtmlTemplate;
}

And then, you just need to replace the markers and fill the email body to the changed template.

string emailTemplate = GetEmailTemplateContent("C:\\somepath");
emailTemplate = emailTemplate.Replace("[[[FROM]]]", from);
MailMessage email = new MailMessage();
email.Body = emailText;


I recommend you to use the String or StringBuilder classes and make your own template subsystem. I mean, just assign the template content to the String, and replace occurrences of tokens by real values.

If you want an example, please see how Drupal offers variables like !username, and so on. If you need code examples, please don't you hesitate posting a comment here.

Hope this helps,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号