Adding variables in HTML emails
I want to send emails with variables embedded in them to change the information within as needed. First I wrote basic HTML email code without variables and it worked. Then I tried to add variables but it didn't work.
This is the code that didn't work (just one of the ways I tried to add a variable):
Code:
Try
Dim emailmessage As New MailMessage
emailmessage.To.Add("to email address")
emailmessage.From = New MailAddress("from email address")
emailmessage.Subject = ("Arrival Date")
emailmessage.IsBodyHtml = True
Dim Username as string = "Bill"
Dim NewString As String = "<html>
<body>
<p>Dear " + UserName + ", </p>
<p>I will be arriving on the 5th</p>
<p>Sincerely,<br>-Sam</br></p>
</body>
</html>
"
Dim SMTP As New SmtpClient()
SMTP.Host = "SMTP.Gmail.com"
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New Net.NetworkCredential("email from", "password")
SMTP.Send(emailmessage)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I also tried <% Username %> in the non-working code as well
This code does work but is strictly text:
Code:
Try
Dim emailmessage As New MailMessage
emailmessage.To.Add("to email address")
emailmessage.From = New MailAddress("from email address")
emailmessage.Subject = ("Arrival Date")
emailmessage.IsBodyHtml = False
Dim Username As String = "Bill"
Dim Body As String = "Hi " + Username + ", I'll be arriving on the 5th"
"
emailmessage.Body = Body
Dim SMTP As New SmtpClient()
SMTP.Host = "SMTP.Gmail.com"
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New Net.NetworkCredential("email from", "password")
SMTP.Send(emailmessage)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I have been working on this all afternoon and evening and have searched the internet with no solution.
Please point me in the right direction.
Thanks
Re: Adding variables in HTML emails
In your first block of code you are never assigning a value to emailmessage.Body
Re: Adding variables in HTML emails
Quote:
Originally Posted by
vbcub
I have been working on this all afternoon and evening and have searched the internet with no solution.
That's incredible, given how bleedingly obvious the issue is. This is a perfect demonstration that you need to be far more systematic in what you're doing. When things didn't work, what you should have done is stepped away from the code and listed all the things that your code needed to do, then gone back to the code and checked whether it actually did them. If you had done that then you would have solved the problem in minutes, if not seconds. Look at your second code snippet:
Code:
Try
Dim emailmessage As New MailMessage
emailmessage.To.Add("to email address")
emailmessage.From = New MailAddress("from email address")
emailmessage.Subject = ("Arrival Date")
emailmessage.IsBodyHtml = False
Dim Username As String = "Bill"
Dim Body As String = "Hi " + Username + ", I'll be arriving on the 5th"
"
emailmessage.Body = Body
Dim SMTP As New SmtpClient()
SMTP.Host = "SMTP.Gmail.com"
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New Net.NetworkCredential("email from", "password")
SMTP.Send(emailmessage)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Ignoring the fact that you have a spurious double-quote in there that would prevent that code compiling, you assign the body text to a variable and you assign that variable to the Body property of the MailMessage. Now let's take a look at your first code snippet, shall we?
Code:
Try
Dim emailmessage As New MailMessage
emailmessage.To.Add("to email address")
emailmessage.From = New MailAddress("from email address")
emailmessage.Subject = ("Arrival Date")
emailmessage.IsBodyHtml = True
Dim Username as string = "Bill"
Dim NewString As String = "<html>
<body>
<p>Dear " + UserName + ", </p>
<p>I will be arriving on the 5th</p>
<p>Sincerely,<br>-Sam</br></p>
</body>
</html>
"
Dim SMTP As New SmtpClient()
SMTP.Host = "SMTP.Gmail.com"
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New Net.NetworkCredential("email from", "password")
SMTP.Send(emailmessage)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
You assign the body text to a variable and...? That should have been bleedingly obvious. We often see what we expect to see rather than what's there - we're all prone to that at times - and that's why we have to be systematic about debugging.