I am working on a mailing system, sending the same email one by one, but I do not want to use bcc because the information sent has different information queried from the database. I query the database and add all the email addresses into an array, then with the array I call a function to send the email sending the address and the html page reference as parameters. My problem is that the message repeats itself as many times as their are email recipients. For example for 3 recipients each one gets the message three times. I keep using msg.to.clear() to clear the recipients but the program still calls the function three times.
Protected Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click SendHTMLMail() End Sub
Public Sub SendHTMLMail()
Dim toaddress As String = "" Dim red As String = (Server.MapPath("~/HTMLPage.htm")) Dim ta As New DataSet1TableAdapters.pruebasTableAdapter Dim dt As DataSet1.pruebasDataTable Dim r As DataSet1.pruebasRow Dim x As Integer = ta.conteo() Dim s(x - 1) As String Dim y = x Dim z = 0 dt = ta.GetDataBy1() Dim mystring = "" For Each r In dt s(z) = r.email z = z + 1 Next For i As Integer = 0 To (y - 1) sendEmailtoaddress(s(i), red) MsgBox("sent" + s(i).ToString) mystring += s(i).ToString + ", " Next Label1.Text = mystring 'Label1.Text = "Enviando mensaje!" 'Label1.Text += "Mensaje enviado exitosamente!"
End Sub
Shared Function sendEmailtoaddress(ByVal toaddress As String, ByVal serverpath As String) As Integer
Dim x As Integer Dim reader As New StreamReader(serverpath) Dim readFile As String = reader.ReadToEnd() Dim myString As String = "" myString = readFile Dim Msg As New MailMessage() 'Dim fromMail As New MailAddress("[email protected]") Dim fromMail As New MailAddress("[email protected]") ' Sender e-mail address. Msg.From = fromMail ' Recipient e-mail address. Dim emailad As New MailAddress(toaddress) Msg.To.Clear() Msg.To.Add(emailad) ' Subject of e-mail Msg.Subject = "Bufete González Corrales" Msg.Body = myString.ToString() Msg.IsBodyHtml = True Dim sSmtpServer As String = "" 'sSmtpServer = "smtp.live.com" sSmtpServer = "smtp.gmail.com" Dim a As New SmtpClient() a.Host = sSmtpServer a.Port = 587 ' a.Credentials = New System.Net.NetworkCredential("[email protected]", "PASSWORD") a.Credentials = New System.Net.NetworkCredential("[email protected]", "PASSWORD") a.EnableSsl = True a.Send(Msg) reader.Dispose() Return x
I added the a.dispose() function at the end of the mail sending function and now I get only two copies of the message. I think that the For i As Integer = 0 To (y - 1)... is being called twice.
The program sends the first 3 emails and then resends them.
each one gets the message three times
That sounds wrong. Please specify exactly what happens. - SLaks