embedding variables in email body

W

William Gower

I am creating an email message to be sent from a web page. How do I embed
variables like strFirstName, strLastName etc. into the body

I would like it to say.

Last Name : strLastName ex. Gower
First Name: strFirstName ex. Bill

Dim strFirstName As String = CType(Request.QueryString("FirstName"), String)

Dim strLastName As String = CType(Request.QueryString("LastName"), String)

Dim objMailMessage As Mail.MailMessage

SmtpMail.SmtpServer = "mailhub.mysite.com"

objMailMessage = New Mail.MailMessage

objMailMessage.From = "(e-mail address removed)"

objMailMessage.To = "(e-mail address removed)"

objMailMessage.Subject = "A Member has made the following changes to their
personal information"

objMailMessage.Body = ""

SmtpMail.Send(objMailMessage)
 
C

Carl Prothman [MVP]

William said:
I am creating an email message to be sent from a web page. How do I
embed variables like strFirstName, strLastName etc. into the body

objMailMessage.Body = "Dear " & _
Server.HtmlEncode(Request.QueryString("FirstName")) & " " & _
Server.HtmlEncode(Request.QueryString("LastName")) & ",\n" & _
"How do you want your Spam sent to you today? ;-)"
 
M

MWells

As Carl pointed out, you're just smithing a string and setting the .Body
property.

If you have a lengthy body, you might benefit from using the
System.Text.StringBuilder class instead, doing a bunch of .Append () and
..AppendFormat () 's to create your body, and then set the email object's
..Body to the StringBuilder.ToString () result;

using System.Text;

StringBuilder sb = new StringBuilder ();
sb.Append ("Dear {0} {1},\n",
Request.QueryString("FirstName"),
Request.QueryString("LastName"));
sb.Append ("\n");
sb.Append ("How do you want your Spam sent to you today? ;-)");
objMailMessage.Body = sb.ToString ();

Make sure to do HtmlEncodes and to do your necessary HTML formatting if you
want the message body to be HTML.

An alternative is to use a product like AspNetEmail;

http://www.aspnetemail.com/

Out of the box, this gives you two alternatives to building your email body
in code;

1) built-in mail-merging through templates.

2) the ability to send a webpage as the email body content. This lets you
design and code your email body as a standard ASPX page, complete with
databound controls, calculations, etc. When you send the message,
AspNetEmail makes a request to your page and generates the email content on
the fly.


/// M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top