Dhenn
Dhenn

Reputation: 295

Sending Email to multiple recipients in VB.NET

I'd like to ask if sending email to multiple recipients in vb.net is possible?

What I mean here is I want to create a program that sends email to multiple recipients, unlike the usual program which the email address , the subject and other email attributes are hard coded, what I want is hopefully the email address, message body, is came from the database.

So basically if i will going to call a form to send the email, it will fetch the data from the database server providing me a list of recipients and their other details.

Can someone show me how to do that? I would be grateful if someone could show me how to do it. Thanks!

Upvotes: 1

Views: 15145

Answers (1)

Amen Ayach
Amen Ayach

Reputation: 4338

It's to simple :

Public Function Snd(ByVal frm As String, ByVal tooo As List(Of String), _
                        ByVal pass As String, ByVal hst As String, _
                        ByVal sbj As String, ByVal bdy As String, _
                        ByVal lAtt As List(Of String)) As String


        Dim ml As New MailMessage()

        ml.From = New MailAddress(frm)
        For Each sA As String In tooo
            ml.To.Add(sA)
        Next

        ml.Subject = sbj
        ml.Body = bdy
        For Each sA As String In lAtt
            ml.Attachments.Add(New Attachment(sA))
        Next
        '"smtp.gmail.com" for gmail
        '"smtp.live.com"   for hotmail
        Dim SMTPServer As New SmtpClient(hst)
        SMTPServer.Port = 587
        SMTPServer.Credentials = New System.Net.NetworkCredential(frm, pass)
        SMTPServer.EnableSsl = hst <> "smtp.mail.yahoo.com"

        Try
            SMTPServer.Send(ml)
            Return "Sent"
        Catch ex As SmtpException
            Return ex.Message
        End Try
        Return ""
    End Function

Check this : sending email using smtp

Upvotes: 2

Related Questions