2

My overall goal is to have 1 single "SendIntranetEmail" function in our app code that sends out all of our emails in our intranet app. Right now they are all scattered.

Ideally I would like to pass this "SendIntranetEmail" function the Subject, Body, Footer, and recipients (to, cc (optional), & bcc (optional)), and from (optional). My hang up is how to pass the "SendIntranetEmail" function a list of recipients.

We are using ASP.NET/VB and i'm pulling the email address (from SQL and LDAP) using other functions. Both of these functions return the email address in data tables. So should I pass the "SendIntranetEmail" function an array, or can I send it a MailAddressCollection and/or MailAddress. What is the best way, and how? The array I can figure out. I was just hoping there is an easier way with the MailAddress/MailAddressCollection class.

Thanks Josh

EDIT - Also, I realize i can pass in the DT to the main function and loop thru the rows. But i'm wondering if there isn't a more efficient way using the Mail class.

4

2 回答 2

3

@Joshua,MailAddressCollection 可以正常工作。这是一个例子:

Public Class Class1

    Sub init()
        Dim dt As New DataTable

        'load DataTable here

        Dim addresses As New Net.Mail.MailAddressCollection

        For Each row As DataRow In dt.Rows
            addresses.Add(row("email").ToString())
            'You could also do this for a pretty email name
            'addresses.Add(String.Format("{0} <{1}>", row("name"), row("email")))
        Next

        sendEmails(addresses)

    End Sub

    Sub sendEmails(addresses As Net.Mail.MailAddressCollection)

        Dim mail As New Net.Mail.MailMessage()

        For Each address As Net.Mail.MailAddress In addresses
            mail.To.Add(address)
        Next

        'set message details

        'send mail

    End Sub

End Class
于 2013-10-24T01:59:35.947 回答
1

您可以执行您解释的任何一个选项,这是一个偏好问题(Array、MailAdressCollection、DataTable)。我会选择MailAddressCollection只是因为您在创建MailAddress. 然后,您可以在尝试将其发送到您的职能部门之前捕获并报告格式错误的电子邮件。

于 2013-07-26T13:22:36.890 回答