Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi this is my coding for sending an email

I want to send a recepient as like

abc@yahoo.com;cdef@gmail.com;jdfdf@yahoo.com
abc@yahoo.com,bcd@yahoo.com


like in recepients i want to put semicolon or comma but both want to accept in coding..how to do this any idea?

C#
protected static string mailSend(string FromAddress,string password,string[] ToAddress,string[] CcAddress,string[] BccAddress,string MessageBody,string Subject,string[] FileAttachment) 
        {
            try
            {
                MailMessage obj = new MailMessage();
                SmtpClient serverobj = new SmtpClient();

                serverobj.Credentials = new NetworkCredential(FromAddress, Password);
                serverobj.Port = 25;
                serverobj.Host = "smtp.gmail.com";
                serverobj.EnableSsl = true;
                obj = new MailMessage();
                obj.From = new MailAddress(FromAddress, FromAddress, System.Text.Encoding.UTF8);
                obj.Priority = MailPriority.High;
                obj.Subject = Subject.Trim();
                string date = DateTime.Now.ToString();
                obj.Body = MessageBody;
                obj.IsBodyHtml = true;


                if (ToAddress != null)
                {
                    foreach (string toAddr in ToAddress)
                        obj.To.Add(new MailAddress(toAddr));
                }



                if (CcAddress != null)
                {
                    foreach (string ccAddr in CcAddress)
                        obj.CC.Add(new MailAddress(ccAddr));
                }
                else
                    CcAddress = null;



                if (BccAddress != null)
                {
                    foreach (string bccAddr in BccAddress)
                        obj.Bcc.Add(new MailAddress(bccAddr));
                }
                else
                    BccAddress = null;


                if (Subject == string.Empty)
                    Subject = string.Empty;

                if (MessageBody == string.Empty)
                    MessageBody = string.Empty;


                if (Attachments != null && Attachments.Length > 0)
                    foreach (string _Attachment in Attachments)
                        obj.Attachments.Add(new System.Net.Mail.Attachment(_Attachment));
                else
                    Attachments = null;



                dbquery.InsertMail(FromAddress, ToAddress, CcAddress, BccAddress, Subject, MessageBody, Attachments);
                serverobj.Send(obj);
                return "Mail Sent Successfully";
            }

            catch (Exception ex)
            {
                return ex.Message;
            }
        }
Posted
Updated 30-Aug-11 20:09pm
v2

1 solution

The e-mail addresses to add to the MailAddressCollection. Multiple e-mail addresses must be separated with a comma character (",").
http://msdn.microsoft.com/en-us/library/ms144695(v=VS.90).aspx[^]

So, you can pass comma separated addresses. For semicolon, just replace it with comma and pass.
 
Share this answer
 
Comments
suryaswathi 31-Aug-11 3:20am    
string to = "sugunajcse@yahoo.com;sugunajcse@gmail.com";

if ((to != ""))
{
string[] strToArray;
strToArray = to.Split(';');


int ToInt = strToArray.Length;
int Tocount = 0;
while ((Tocount < ToInt))
{
obj.To.Add(new MailAddress(strToArray[Tocount]));
Tocount = (Tocount + 1);
}
}

that address collection also gives error.am trying like this but in to address
give error
recepient want to spercify like this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900