Multiple recipients not working

Hello everybody…

i have a little problem and i can’t solve it by myself…

I have a List of Emails from a Gridview that i want to add to the “to”-Section. But i can not get it in there.

I use Visual Basic and this is what i have so far:

I try it now with a string…

        'Dim lst As List(Of MailAddress) = New List(Of MailAddress)()
        Dim Mailliste As String = ""
        For Each row As GridViewRow In GVEmailliste.Rows
            Mailliste = Mailliste & row.Cells(2).Text.Replace(";", ",")
            'lst.Add(New MailAddress(row.Cells(2).Text.Replace(";", ",")))
            Mailliste = Mailliste & (",")
        Next
        Mailliste = Mailliste.Substring(0, Mailliste.Length - 1)
        Mailliste = Mailliste.Replace(" ", String.Empty)

        Dim message As New MailMessage("Test@example.de", Mailliste)

Here i have only the first Mailadress in my Message…

How can i get all of them? Any ideas?

Thanks so far!!!

Hi Thorsten,

I presume that the row.Cells(2).Text contains something like this (emails separated with ; character):

"1_recipient@example.com;2_recipient@example.com;3_recipient@example.com"

If that’s the case, try using this:

Dim message As New MailMessage("Test@example.de", row.Cells(2).Text.Split(";"c))

Does this solve your issue?

Regards,
Mario

Thanks for your answer…

I think that will create on email for every recipient? in row.cells(2) is maybe

‘1_recipient@example.com;2_recipient@example.com’

. In row.cells(3) is

‘Test@example.de’

. There can be thousand recipients/rows.

I need one single email for all… Database is also not so good, i need to fix some issues first. There are german special caraters in mail adresses (sigh) like ö… I need to convert them first. Also find duplicates and sometimes a vbtab (wtf)…

After that i have everyting in a clean string (Mailliste) and also in a List (itemsList). I would prefer to use itemsList for the recipients

        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
        Dim Mailliste As String = ""
        For Each row As GridViewRow In GVEmailliste.Rows
            Dim encoded As String = row.Cells(2).Text
            row.Cells(2).Text = Context.Server.HtmlDecode(encoded)
            Mailliste = Mailliste & row.Cells(2).Text.Replace(";", ",")
            Mailliste = Mailliste & (",")
        Next
        Mailliste = Mailliste.Substring(0, Mailliste.Length - 1)
        Mailliste = Mailliste.Replace(" ", String.Empty)
        Mailliste = Mailliste.Replace(vbTab, String.Empty)

        'Dim message As New MailMessage("Test@example.de", List(lst)) ' Mailliste)

        Dim parts As String = Mailliste
        Dim itemsList As List(Of String)

        itemsList = (From s In parts.Split(",")
                     Select s).ToList()
        itemsList = itemsList.Distinct.ToList()

But how to use this list for “to:” in the email?

Just past that list to the MailMessage constructor as an array:

Dim message As New MailMessage("Test@example.de", itemsList.ToArray())

Hey! I get success now with the following:

        Mailliste = Mailliste.Replace(",", ";")

        Dim message As New MailMessage("sender@example.com", Mailliste.Split(";"c))

Thanks!!!

That works also! Man! I wasn’t that far from the solution a few hours ago :smiley:

Thank you very much!

Now i get a error because i one mailadress seems to be corrupt. Can i check it it its a correct email adress format before add to list?

Can you show me what that corrupted mail address looks like?
Perhaps you could create some kind of validation for it.

GemBox.Email does provide MailAddressValidator.Validate method (see Mail Address Validation example), but it does much more than just email syntax verification so this is probably an overkill for you.

So, maybe you could try using something like this:

Dim message As New MailMessage("sender@example.com")
For Each item In Mailliste.Split(";"c)
    Try
        message.To.Add(New MailAddress(item))
    Catch
    End Try
Next

Problem was a missing email adress. I selected rows where email is NOT NULL but in one row email was not NOT NULL or a blank. Zero spaces but not NOT NULL. That crashed the query.

But now - new order - i do not need the stored email adresses, i need to generate own mail adresses from a stored number. Thats easy, the blank email can now not occur anymore.