2011年10月6日 星期四

MailDefinition 好用的 HTML template Mail body 產生器

http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates

這個 .net class 可以用HTML template file 產生出 HTML 格式的 Mail message,除了拿來寄mail外,也可以拿來產生HTML 套版結果,如果要用在非ASPX的專案(如WCF),需要參考下面的文章,動點小手腳。


http://forums.silverlight.net/t/110773.aspx/1

"The owner parameter indicates which control is the parent of the MailDefinition control. It determines which directory to search for the text file specified in the BodyFileName property."
I found a solution. Using a class file (.cs) rather than an WebForm (.aspx page) I used MailDefinition successfully. The key was to create a control (System.Web.UI.Control) that referenced the .BodyFileName. So the code for this is: (where md is an instance of MailDefinition, and message is an instance of MailMessage)
 Control control = new Control();

control.AppRelativeTemplateSourceDirectory = control.ResolveUrl(md.BodyFileName);

message = md.CreateMailMessage("test@test.com", replacements, control);
Which works, but I haven't tested it on the Internet yet.

沒有留言: