Methods for Sending Formatted (HTML) E-mails in .Net
It is common for websites to send you various e-mail messages: Account activation, password reset, etc.
In the past, I built a system where I generate xml markup in an XmlDocument
and then use an XslCompliedTransform
to convert the xml into an html e-mail. The downside to this is that XSLT's are neither fun nor easy to alter (at least for me).
Recently, I borrowed an idea from a past co-worker where I use data driven subscriptions in SSRS along with a database table as a message queue. The data drive subscription runs every 10 minutes and sends out all the e-mails in the queue, which are reports sent as WebArchive so that they are rendered in the e-mail message and not sent as an attachment. This works great, but feels a little like overkill. The benefit I see is that someone can open the report in BIDS and change the layout without having to have too much programming experience.
What are some ways you guys have sent html formatted e-mails out to users from your applicatio开发者_Python百科ns?
This might be overkill for what you are doing but how I generally solve this issue is to store html and text templates for each of the emails I want to send out in the database. The templates have replacement codes for the dynamic data. ex:
<--Replacement.FirstName-->
Then in the .net service responsible for sending out my emails I get the templates and I get the values for the queued emails that need to go out and make the necessary replacements. At the end I use a RegEx.replace to strip out any remaining replacement codes (if any).
For the actual sending of the emails I use the System.Net.Mail namespace as it allows you to send multi-part messages so if the end user's client only supports plain text they don't see all my html tags since I send out a text and html version of the email.
Even though it isn't the most elegant solution I like it because I generally make a nice front end to manage the html/text templates in the database so that the email designers can manage these. Also I generally list the available replacement codes so they can just click a button to add the relevant replacement code to the template.
You might want to investigate using this class too:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition.aspx
You can pass a dictionary with key / values.
It's the same used by the .NET Login Controls (like ForgotPassword)
There is some code examples here:
http://www.go4answers.com/Example/systemnetmail-html-template-problem-88358.aspx
精彩评论