Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the MailDefinition class require a System.Web.UI.Control?

When creating a MailMessage object by calling the "CreateMailMessage" method on the MailDefinition class, the third parameter is an object of type System.Web.UI.Control.

MailDefinition mail = new MailDefinition();

ListDictionary replacements = new ListDictionary();
replacements.Add("<%myname%>", "John");

mail.BodyFileName = "~/App_Data/Emails/SomeEmail.txt";
mail.From = "[email protected]";
mail.Subject = "Hello";

MailMessage message = mail.CreateMailMessage("[email protected],", replacements, );

Why is that?
And in the case that I don't have an object of that type, what should I pass instead? Just a new Control object?

Control control = new Control();

UPDATE

I would highly recommend using Razor to build email templates. It has great syntax, works great, and doesn't have any weird dependencies!

like image 748
John B Avatar asked Apr 13 '09 14:04

John B


2 Answers

Usually you just pass this as the control.

MailMessage message = mail.CreateMailMessage("[email protected],", replacements, this);

As for the reason why, here is what MSDN says:

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.

like image 158
Andrew Hare Avatar answered Oct 21 '22 11:10

Andrew Hare


The CreateMailMessage function internally uses the specified Control to query its AppRelativeTemplateSourceDirectory property and its OpenFile method to read the contents of the body (specified in the BodyFileName property of the MailDefinition).

Seems poor design and unnecessary tight coupling for me.

like image 35
György Balássy Avatar answered Oct 21 '22 10:10

György Balássy