Unable to send an email to multiple addresses/recipients using C#

前端 未结 6 950
陌清茗
陌清茗 2020-12-01 11:09

I am using the below code, and it only sends one email - I have to send the email to multiple addresses.

For getting more than one email I use:

strin         


        
相关标签:
6条回答
  • 2020-12-01 11:18

    This function validates a comma- or semicolon-separated list of email addresses:

    public static bool IsValidEmailString(string emailAddresses)
    {
        try
        {
            var addresses = emailAddresses.Split(',', ';')
                .Where(a => !string.IsNullOrWhiteSpace(a))
                .ToArray();
    
            var reformattedAddresses = string.Join(",", addresses);
    
            var dummyMessage = new System.Net.Mail.MailMessage();
            dummyMessage.To.Add(reformattedAddresses);
            return true;
        }
        catch
        {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 11:18

    This code I use for send multiple mail for to, bcc and cc

    MailMessage email = new MailMessage();
    Attachment a = new Attachment(attach);
    email.From = new MailAddress(from);//De
    string[] Direcciones;
    char[] deliminadores = { ';' };
    
    //Seleccion de direcciones para el parametro to
    Direcciones = to.Split(deliminadores);
    foreach (string d in Direcciones)
    email.To.Add(new MailAddress(d));//Para
    
    //Seleccion de direcciones para el parametro CC
    Direcciones = CC.Split(deliminadores);
    foreach (string d in Direcciones)
    email.CC.Add(new MailAddress(d));
    
    //Seleccion de direcciones para el parametro Bcc
    Direcciones = Bcc.Split(deliminadores);
    foreach (string d in Direcciones)
    enter code here`email.Bcc.Add(new MailAddress(d));
    
    0 讨论(0)
  • 2020-12-01 11:25

    The problem is that you are supplying a list of addresses separated by semi-colons to the MailMessage constructor when it only takes a string representing a single address:

    A String that contains the address of the recipient of the e-mail message.

    or possibly a list separated by commas (see below).

    Source

    To specify multiple addresses you need to use the To property which is a MailAddressCollection, though the examples on these pages don't show it very clearly:

    message.To.Add("one@example.com, two@example.com"));
    

    The e-mail addresses to add to the MailAddressCollection. Multiple e-mail addresses must be separated with a comma character (",").

    MSDN page

    so creating the MailMessage with a comma separated list should work.

    0 讨论(0)
  • 2020-12-01 11:26

    You are also allowed to pass MailMessage.To.Add()a comma separated list of valid RFC 822 e-mail addresses:

    Nathaniel Borenstein <nsb@bellcore.com>, Ned Freed <ned@innosoft.com>
    

    So the code would be:

    message.To.Add("Nathaniel Borenstein <nsb@bellcore.com>, Ned Freed <ned@innosoft.com>");
    

    Note: Any code released into public domain. No attribution required.

    0 讨论(0)
  • 2020-12-01 11:27

    This is what worked for me. (recipients is an Array of Strings)

    //Fuse all Receivers
    var allRecipients = String.Join(",", recipients);
    
    //Create new mail
    var mail = new MailMessage(sender, allRecipients, subject, body);
    
    //Create new SmtpClient
    var smtpClient = new SmtpClient(hostname, port);
    
    //Try Sending The mail
    try
    {
        smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
        Log.Error(String.Format("MailAppointment: Could Not Send Mail. Error = {0}",ex), this);
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 11:41

    To send to multiple recipients I set up my recipient string with a comma as my separator.

    string recipient = "foo@bar.com,foo2@bar.com,foo3@bar.com";
    

    Then to add the recipients to the MailMessage object:

    string[] emailTo = recipient.Split(',');
    for (int i = 0; i < emailTo.GetLength(0); i++)
        mailMessageObject.To.Add(emailTo[i]);
    
    0 讨论(0)
提交回复
热议问题