Search code examples
c#formatmailaddress

cc (mail) after say 6th email


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);

Solution

  • 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).