can someone please help me sort out this code so that each user gets a different email with different details
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim username As String = "user@email.com"
Dim password As String = "Password"
Dim subject = "Confirm Contact"
Dim smtpclient As SmtpClient = New SmtpClient()
Dim mail As MailMessage = New MailMessage()
Dim fromaddress As MailAddress = New MailAddress("user@email.com")
smtpclient.Host = "smtp.gmail.com"
smtpclient.Port = 587
mail.From = fromaddress
Dim dt As New DataTable("UserTable")
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT empid,UserFirstName,UserLastName,Usertelephone,Useremail from employee ", con)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
End Using
For Each row As DataRow In dt.Rows
Dim messageBody = File.ReadAllText(Server.MapPath("~/email1.html"))
messageBody = messageBody.Replace("<%UserFirstName%>", Convert.ToString(row("UserFirstName"))).Replace("<%UserPhonenumber%>", Convert.ToString(row("Usertelephone")))
With mail
mail.To.Add(Convert.ToString(row("Useremail")))
mail.Subject = subject
mail.Body = messageBody
End With
mail.IsBodyHtml = True
smtpclient.EnableSsl = True
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpclient.Credentials = New System.Net.NetworkCredential(username, password)
Next
smtpclient.Send(mail)
End Sub
End Class
email1.html
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
Dear <%UserFirstName%>
These are the details we have found
<%UserFirstName%><%UserPhonenumber%>
Please click here to confirm or correct our records <br /><a href="http://localhost/userconfirmation.aspx?empid=" & empid >Click Here</a>
I did include it in the loop...it sends 2 emails to 1st email address(which is not right as it should get just 1 email) and and 2nd email to 2nd email address(which is correct)
I am sure this is to do with loop but i dont understand whats wrong
For Each row As DataRow In dt.Rows
Dim username As String = "user@email.com"
Dim password As String = "Password"
Dim subject = "Confirm Contact"
Dim smtpclient As SmtpClient = New SmtpClient()
Dim mail As MailMessage = New MailMessage()
Dim fromaddress As MailAddress = New MailAddress("user@email.com")
smtpclient.Host = "smtp.gmail.com"
smtpclient.Port = 587
mail.From = fromaddress
Dim messageBody = File.ReadAllText(Server.MapPath("~/email1.html"))
messageBody = messageBody.Replace("<%UserFirstName%>", Convert.ToString(row("UserFirstName"))).Replace("<%UserPhonenumber%>", Convert.ToString(row("Usertelephone")))
With mail
mail.To.Add(Convert.ToString(row("Useremail")))
mail.Subject = subject
mail.Body = messageBody
End With
mail.IsBodyHtml = True
smtpclient.EnableSsl = True
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpclient.Credentials = New System.Net.NetworkCredential(username, password)
smtpclient.Send(mail)
Next
I did include it in the loop...it sends 2 emails to 1st email address(which is not right as it should get just 1 email) and and 2nd email to 2nd email address(which is correct)
The statement above and stepping through the code with the Visual Studio debugger should clearly indicate the logical bug. Not to mention I explained the bug in my previous post. Being there is only 10 minutes between my post and your next post, I'm under
the impression you are not taking the time to debug and just want the answer given to you.
The problem is you are adding to the TO address collection on each and every loop.
With mail
mail.To.Add(Convert.ToString(row("Useremail")))
mail.Subject = subject
mail.Body = messageBody
End With
Try clearing the TO address after sending the email.
mail.To.Clear()
Or take a moment to review your code and rethink the design.
Member
200 Points
692 Posts
help sorting this code which gives wrong results
Feb 01, 2017 10:49 AM|Lexi85|LINK
Hello All
I have below code which send email to list of users in the table with their own <Username>the table has 2 records
Empid1 Usename Phone email
1 abc 8247 user1@email.com
2 def 9876 user2@email.com
The code below sends mail, but it send the same email to both email addresses in the table..it says
Dear def to both emails...and send email with To: user1@email.com and cc as user2@email.com
can someone please help me sort out this code so that each user gets a different email with different details
email1.html
All-Star
30711 Points
11175 Posts
Re: help sorting this code which gives wrong results
Feb 01, 2017 11:05 AM|mgebhard|LINK
Have you tried stepping through the code using the Visual Studio debugger? The logical error are very obvious and should pop out while your debugging.
https://msdn.microsoft.com/en-us/library/y740d9d3.aspx
Member
200 Points
692 Posts
Re: help sorting this code which gives wrong results
Feb 01, 2017 11:17 AM|Lexi85|LINK
I did include it in the loop...it sends 2 emails to 1st email address(which is not right as it should get just 1 email) and and 2nd email to 2nd email address(which is correct)
I am sure this is to do with loop but i dont understand whats wrong
All-Star
30711 Points
11175 Posts
Re: help sorting this code which gives wrong results
Feb 01, 2017 11:29 AM|mgebhard|LINK
The statement above and stepping through the code with the Visual Studio debugger should clearly indicate the logical bug. Not to mention I explained the bug in my previous post. Being there is only 10 minutes between my post and your next post, I'm under the impression you are not taking the time to debug and just want the answer given to you.
The problem is you are adding to the TO address collection on each and every loop.
Try clearing the TO address after sending the email.
Or take a moment to review your code and rethink the design.