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

5

14

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!

Stew answered 13/4, 2009 at 14:44 Comment(1)
Why aren't you using MailMessage message = new MailMessage()?Photography
M
5

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.

Minutiae answered 13/4, 2009 at 14:51 Comment(2)
And what if I'm not running this from an ASPX page, but from an APP_CODE class?Stew
Then you need to pass in a reference to the current control into the code that is creating the mail message.Minutiae
H
3

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.

Harmonie answered 26/1, 2012 at 11:19 Comment(2)
Indeed! But with the advent of the Razor templating engine, it doesn't really matter anymore :P razorengine.codeplex.com in case you've been living under a rock :)Stew
@JohnBubriski Can I use RazorEngine in ASP.NET WebForms application ?Giamo
M
2

You can also just do this:

MailMessage message = this.Mail.CreateMailMessage("[email protected]", dictionary, new System.Web.UI.Control());
Malloch answered 26/2, 2015 at 20:59 Comment(0)
B
1

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".

Binion answered 28/7, 2010 at 0:25 Comment(0)
E
0

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.

Excitant answered 13/4, 2009 at 15:1 Comment(2)
Basically, I'm using the MailDefinition class for its replacement abilities.Stew
Razor engine IMO is the new best practice way.Illustrator

© 2022 - 2024 — McMap. All rights reserved.