开发者

Dynamically entering values in a predefined email body in mvc 3 vb.net

I am writing a mvc 3 application and part of its function is to send out confirmation emails along with an assortment of other emails.. I have the body's for the different types of emails stored in a database table.. Getting the values out of the table and dropping them in a email body using System.Net.Mail isnt a problem at all... What I need to do is somehow put somekind of variable marker in the body which is in the database table and then somehow parse the body text when its dropped into the email body to find the variable marker and put the correct value in it's spot... Any Ideas??? Code snippet of what I have so far is below:

Keep in mind that this will be a rather large body and there are a few places values will have to be dropped in.. I was thinking of having it use something like this:

Long mail body ^^Name^^ has been approved for classes starting on ^^Date^^ at the ^^Place^^ located in ^^Location^^..

When the code loades the text from the body when ever it comes across a variable marked with ^^ it would put the correct information in its place...

            Dim _content As email = db.emails.Where(Function(f) f.ref_name = "Confirmation")
            D开发者_C百科im _body As String = _content.email_body.ToString
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            mail.Subject = "Testing Confirmation"
            mail.To.Add("developer@myemail.com")
            mail.From = New MailAddress("myemail@myemail.com")
            mail.Body = _body


Why not do exactly what you just described. In your database your email template would be something like:

Dear [[Name]],

This is just a sample message.

Then you would create a class to do the replacing:

Class EmailTemplateReplacer
   Public Function DoTheReplace(ByVal emailTemplate as String) as String
       return emailTemplate.Replace("[[Name]]", "whatever you want here")
   EndFunction
EndClass

Then just call it in your code:

Dim _content As email = db.emails.Where(Function(f) f.ref_name = "Confirmation")
Dim _body As String = _content.email_body.ToString

Dim replacer as New EmailTemplateReplacer()
_body = replacer.DoTheReplace(_body)

Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
mail.Subject = "Testing Confirmation"
mail.To.Add("developer@myemail.com")
mail.From = New MailAddress("myemail@myemail.com")
mail.Body = _body
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜