在VB.NET中向多个收件人发送电子邮件

时间:2012-03-05 05:23:45

标签: vb.net .net-3.5 system.net.mail

我想询问是否可以向vb.net中的多个收件人发送电子邮件?

我在这里的意思是我想创建一个向多个收件人发送电子邮件的程序,这与电子邮件地址,主题和其他电子邮件属性是硬编码的通常程序不同,我想要的是希望电子邮件地址,邮件身体,来自数据库。

所以基本上如果我要打电话给表单发送电子邮件,它将从数据库服务器获取数据,为我提供收件人列表及其他详细信息。

有人可以告诉我该怎么做吗?如果有人能告诉我该怎么做,我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

这很简单:

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

请检查:sending email using smtp