views:

847

answers:

3

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();
+2  A: 

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.

Andrew Hare
And what if I'm not running this from an ASPX page, but from an APP_CODE class?
SkippyFire
Then you need to pass in a reference to the current control into the code that is creating the mail message.
Andrew Hare
A: 

It sounds like you may not need to use the MailDefinition class at all if you're not binding to any controls. To simply send an email over smtp, you should use a System.Net.Mail.SmtpClient with a System.Net.Mail.MailMessage.

Todd Benning
Basically, I'm using the MailDefinition class for its replacement abilities.
SkippyFire
A: 

I have been using new LiteralControl() for the 3rd parameter because my messages are being sent from a Workflow. It works. But I do not have an answer for "Why".

Scott Rippey