Tuesday, May 16, 2006

System.Net.Mail.MailDefinition

Ok, not a big deal here, but something I rather liked. I wanted to create a generic way to pull an email template, replace key areas with standard things like first name, company information, and other details and a coworker let me know about the .net password recovery control that does something similar. After digging in a bit, I found that it was using the MailDefinition to invoke a CreateMailMessage passing in the collection of replacements.

It was very nice not to have to write the find/replace code (even though that would be simple). There are still a few steps left to wrap the email templates table, but the basics are there to the point that we can toss a new template in a table and execute replacements on it to send some notification email messages to our clients.

One gotcha, I kept running into the following error:


Unable to read data from the transport connection: net_io_connectionclosed.


As it turned out, my particular issue here was that relay was turned off on our email server for all clients. In short, I had to open a hole to allow a certain server to access the smpt server in order to send the email.

7 comments:

Anonymous said...

Hi,

I am having trouble with the passwordRecovery control...I keep getting an error concerning the replacements dictionary...how do I configure the email template and how do I replace the relative info?

Contact me on richardda@qaits.co.za

Anonymous said...

Thanks Chad. That one gets me everytime. I found almost a dozen links that either had no solution or said it was an incorrect host/smtp name. After googling io_connectionclosed and firewall, it brought back one single result: Your blog. From there I knew instantly. Thanks!!

Unknown said...

No problem. I'm glad to hear this helped!

Unknown said...

hi,
I have tha same problem. I do not know exactly, but I host my website on www.godaddy.com. So, I can not change the relay of mail server.
Anything I can do?

Thanks,
Dan

Unknown said...

Hi Dan,

Here is another post that has some instruction on relaying with GoDaddy. It is 2 years old, so I can't verify if it is still accurate.

http://moojjoo.blogspot.com/2007/05/godaddycom-systemnetmail-solution.html

Let me know if that did work for you.

Thanks,
Chad

NickS said...

Actually, the MailDefinition object is in the System.Web.UI.WebControls namespace... i.e. System.Web.UI.WebControls.MailDefinition

Unknown said...

Hi NickS,

Thanks for the update. Indeed the MailDefinition has moved. If you want to send smtp mail without using a control however, you must still use pieces from the System.Net.Mail namespace (http://msdn.microsoft.com/en-us/library/system.net.mail.aspx). Namely, you can use the System.Net.Mail.MailMessage class (http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx).

Cheers,
Chad