.Net System.Mail.Message adding multiple “To” addresses

前端 未结 8 1366
悲&欢浪女
悲&欢浪女 2020-12-30 18:56

EDIT: This question is pointless, except as an exercise in red herrings. The issue turned out to be a combination of my idiocy (NO ONE was being emailed as the hos

相关标签:
8条回答
  • 2020-12-30 19:25

    You can do this either with multiple System.Net.Mail.MailAddress objects or you can provide a single string containing all of the addresses separated by commas

    0 讨论(0)
  • 2020-12-30 19:28

    You could try putting the e-mails into a comma-delimited string ("person1@domain.com, person2@domain.com"):

    C#:

    ArrayList arEmails = new ArrayList();
    arEmails.Add("person1@domain.com");
    arEmails.Add("person2@domain.com");
               
    string strEmails = string.Join(", ", arEmails);
    

    VB.NET if you're interested:

    Dim arEmails As New ArrayList
    arEmails.Add("person1@domain.com")
    arEmails.Add("person2@domain.com")
    
    Dim strEmails As String = String.Join(", ", arEmails)
    
    0 讨论(0)
  • 2020-12-30 19:35

    I wasn't able to replicate your bug:

    var message = new MailMessage();
    
    message.To.Add("user@example.com");
    message.To.Add("user2@example.com");
    
    message.From = new MailAddress("test@example.com");
    message.Subject = "Test";
    message.Body = "Test";
    
    var client = new SmtpClient("localhost", 25);
    client.Send(message);
    

    Dumping the contents of the To: MailAddressCollection:

    MailAddressCollection (2 items)
    DisplayName User Host Address

    user example.com user@example.com
    user2 example.com user2@example.com

    And the resulting e-mail as caught by smtp4dev:

    Received: from mycomputername (mycomputername [127.0.0.1])
         by localhost (Eric Daugherty's C# Email Server)
         3/8/2010 12:50:28 PM
    MIME-Version: 1.0
    From: test@example.com
    To: user@example.com, user2@example.com
    Date: 8 Mar 2010 12:50:28 -0800
    Subject: Test
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: quoted-printable
    
    Test
    

    Are you sure there's not some other issue going on with your code or SMTP server?

    0 讨论(0)
  • 2020-12-30 19:38
    private string FormatMultipleEmailAddresses(string emailAddresses)
        {
          var delimiters = new[] { ',', ';' };
    
          var addresses = emailAddresses.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    
          return string.Join(",", addresses);
        }
    

    Now you can use it like

    var mailMessage = new MailMessage();
    mailMessage.To.Add(FormatMultipleEmailAddresses("test@gmail.com;john@rediff.com,prashant@mail.com"));
    
    0 讨论(0)
  • 2020-12-30 19:42

    Thanks for spotting this I was about to add strings thinking the same as you that they'd get added to end of collection. It appears the params are:

    msg.to.Add(<MailAddress>) adds MailAddress to the end of the collection
    msg.to.Add(<string>) add a list of emails to the collection

    Slightly different actions depending on param type, I think this is pretty bad form i'd have prefered split methods AddStringList of something.

    0 讨论(0)
  • 2020-12-30 19:43

    Add multiple System.MailAdress object to get what you want.

    0 讨论(0)
提交回复
热议问题