I think this is probably a very simple question but I just couldn't find the solution!

I have a procedure that handles sending emails.
It currently only handles a single email recipient.
I want to extend it to handle multiple recipients.
I've worked out how you add email recipients to the MailMessage.To collection so now I want to modify my send email procedure to be able to incorporate this.
i.e. it needs to have a 'to' parameter that holds multiple 'name', 'emailAddress' items
I tried this with a Collection but couldn't make it work!

Ideally I'd like two procedures, one that takes a single email and the other that handles multiples

The calling procedure passes the name, emailAddress pairs somehow (array?, collection?) and the sendEmail procedure takes care of the details.

Recommended Answers

All 2 Replies

Hey, im pretty new myself, and i dont know wich language you use, but i would try this:

In App_Code you can add a .vb file with this code:

Public Structure NameAndMail
    Public name As String
    Public email As String
End Structure

And then make a List(Of NameAndMail) to pass as an argument that you can loop through.

Thanks for the suggestion. I don't understand the 'List(Of NameAndMail)' bit so I created an array of the new structure and pass that.

I'm using VS2005 and coding in VB

Here's my solution using two procedures, one for a single recipient and another for multiple recipients:

Any tips/suggestions on how to improve this gratefully received!

Calling Page (using multiple recipients in a grid view)

Dim names(gvRecipients.Rows.Count - 1) As Utilities.EmailDetails
        For i As Integer = 0 To gvRecipients.Rows.Count - 1
            names(i).name = gvRecipients.Rows.Item(i).Cells(0).Text
            names(i).email = gvRecipients.Rows.Item(i).Cells(2).Text
        Next

        If Not Utilities.sendMail(errMsg, mailFrom, names, mailSubject, mailBody, mailBcc) Then
            Master.displayMsg(errMsg)
            Return False
        End If

Utilities.vb

Public Structure EmailDetails
        Public name As String
        Public email As String
    End Structure

    Public Shared Function sendMail(ByRef errMsg As String, ByVal messageFrom As String, ByVal messageTo As String, ByVal subject As String, ByVal body As String, Optional ByVal bcc As String = "") As Boolean
        ' Single recipient
        Dim mailClient As SmtpClient = New SmtpClient()
        Try
            Dim mailMessage As MailMessage = New MailMessage(messageFrom, messageTo, subject, body)
            If Not (bcc = "") Then
                mailMessage.Bcc.Add(bcc)
            End If
            mailMessage.IsBodyHtml = True

            mailClient.Send(mailMessage)
            Return True

        Catch ex As SmtpException
            errMsg = ex.Message
            Return False

        Catch ex As Exception
            errMsg = ex.Message
            If ex.InnerException IsNot Nothing Then
                errMsg &= "(" & ex.InnerException.ToString & ")"
            End If
            Return False

        End Try
    End Function

    Public Shared Function sendMail(ByRef errMsg As String, ByVal messageFrom As String, ByVal messageTo() As EmailDetails, ByVal subject As String, ByVal body As String, Optional ByVal bcc As String = "") As Boolean
        ' Multiple recipients
        Dim mailClient As SmtpClient = New SmtpClient()
        Try

            Dim mailMessage As New MailMessage
            Dim mailAddress As MailAddress


            For i As Integer = 0 To messageTo.GetUpperBound(0)
                mailAddress = New Net.Mail.MailAddress(messageTo(i).email, messageTo(i).name)
                mailMessage.To.Add(mailAddress)
            Next

            Dim mFrom As MailAddress = New MailAddress(messageFrom)
            mailMessage.From = mFrom

            mailMessage.Subject = subject
            mailMessage.Body = body

            If Not (bcc = "") Then
                mailMessage.Bcc.Add(bcc)
            End If
            mailMessage.IsBodyHtml = True

            mailClient.Send(mailMessage)
            Return True

        Catch ex As SmtpException
            errMsg = ex.Message
            Return False

        Catch ex As Exception
            errMsg = ex.Message
            If ex.InnerException IsNot Nothing Then
                errMsg &= "(" & ex.InnerException.ToString & ")"
            End If
            Return False

        End Try

    End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.