0
votes

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.

2
each one gets the message three times That sounds wrong. Please specify exactly what happens. - SLaks
After I added the msg.dispose() the messages sent twice, first the program sends the emails and then sends them again. When I debug step by step the for i that reads the array runs twice. Thank you for your time - user3483354

2 Answers

0
votes
mystring += s(i).ToString + ", "

You just appended the next message inside the loop, without clearing the old one.

0
votes

Problem solved, rewrote the code into a new Project in visual studio, using arrays to store and call the email addresses one by one.

Fill the array...

         For Each r In dt
            emails(j) = r.email
            j = j + 1
         Next

Read the array and send emails one by one...

 For i As Integer = 0 To (x - 1)
        Mail.Subject = "Subject"
        Mail.To.Clear()
        Mail.To.Add(emails(i).ToString)
        Mail.From = New MailAddress("EMAIL ADDRESS")
        Mail.Body = myString.ToString()
        Mail.IsBodyHtml = True

        Dim SMTP As New SmtpClient("SMTP SERVER")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential(TextBox1.Text,TextBox2.Text)
        SMTP.Port = "PORT NUMBER"
        SMTP.Send(Mail)
        MsgBox("Message sent to: " + emails(i).ToString())
    Next