开发者

ASP.NET Send .htm file as email template

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜