cc (mail) after say 6th email

108 Views Asked by At

I am trying to send multiple emails (>10) using Mailaddress class but apparently, it's not liking it. is there a way to attach emails after the 6th to CC?

or any other work around?

I have:

(<[email protected]>; <[email protected]>; <[email protected]>, <[email protected]>; <[email protected]>; \r\n\t<[email protected]>, <[email protected]>; <[email protected]>\r\n\TEXT)

I do Environment.NewLine, I replace < , > , \t and " " with "" (don't know any other better way to format it)

when I try to send it via mailaddress class I am getting an format error. but is working fine when the number of emails are less.

Solved:

            string to = "";
            string cc = "";
            int i = 0;
            foreach (string item in multiAddress.Split(','))
            {
                i += 1;
                if (i < 10)
                {
                    to += item + ",";
                }
                else
                {
                    cc += item + ",";
                }
            }
            to = to.Remove(to.Length - 1);
            cc = cc.Remove(cc.Length - 1);
3

There are 3 best solutions below

4
On BEST ANSWER

Why can't you use the regular way to send Email to multiple address? into a String, separeted by commas like this:

string recipients="[email protected],[email protected],[email protected]" etc..

I personally got an error when I tried to send to more than 9 recipients, so I wrote the following code snippet that after the 9th recipient it automatically moves the recipients to the CC field.

       var emailAddresses= "YourEmailAddresses";

        //conccatenat all the email addresses into one variable
        //if there is more than 9 recipients it moves them to the CC field
        string to="";
        string cc = "";
        int i = 0;
        foreach (string item in emailAddresses) {
            i += 1;
            if (i < 10) {
                to += item + ",";
            }
            else
            {
                cc += item + ",";
            }
         to = to.Remove(to.Length - 1);

If you use the loop make sure to remove the last comma on the string (because it adds a comma after each entry so you will have one extra after the last email).

1
On

You can use regular expressions to isolate mail addresses from others characters For example:

(<)(\w+@\w+\.\w+)(>)

Will match any mail address between angles.

        string source = "(<[email protected]>; <[email protected]>; <[email protected]>, <[email protected]>; <[email protected]>; \r\n\t<[email protected]>, <[email protected]>; <[email protected]>\r\nTEXT)";
        Regex regx = new Regex(@"(<)(\w+@\w+\.\w+)(>)");

        MatchCollection matches = regx.Matches(source);
        foreach (Match match in matches) {
            Console.WriteLine(match.Groups[2].Value);
        }

Here you are a running example.

0
On

You can also write a code to get the cc mail as follows with regular C# code using skip method

   public static string[] GetCCEmails()
   {
       const int limitToIncludeCC = 10 ;// no of email to be skipped
       string[] emailAdresses = new string[] { "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
       var ccmailIDs = emailAdresses.Skip(limitToIncludeCC);
       return ccmailIDs.ToArray();
    }

and assign this array to CC property of MailMessage class of System.Net.Mail.