Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Here my code.when i enter email address in from,to,cc and bcc field my web app send the email.but when i only enter email address in from,to and make cc and bcc field empty it give me error

Exception Details: System.ArgumentException: The parameter 'address' cannot be an empty string.
Parameter name: address

Source Error:


Line 89:             foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
Line 90:             {
Line 91:                 mail.CC.Add(new MailAddress(emailCC));
Line 92:             }
Line 93: 



here my code please help me a google alot but i can not find the


C#
private void SendEmail()
    {
        MailMessage mail = new MailMessage();

        if (txtTo.Text != null)
        {
            char[] splitterrr = { ';' };
            foreach (string emailTo in txtTo.Text.ToString().Split(splitterrr))
            {
                mail.To.Add(new MailAddress(emailTo));
            }
        }
        //mail.To.Add(EmailAddress);
        mail.From = new MailAddress(txtFrom.Text.ToString());
        mail.Subject = txtSubject.Text;
        mail.Body = edtrMessage.Content.ToString();
        if (txtCc.Text != null)
        {

            char[] splitter = { ';' };
            foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
            {
                mail.CC.Add(new MailAddress(emailCC));
            }

        }
        else if (null == txtCc.Text || txtCc.Text.Length == 0 || txtCc.Text == String.Empty)
        {
            mail.CC.Add(new MailAddress(txtCc.Text.ToString()));
        }
        if (txtBcc.Text != null)
        {

            char[] splitterr = { ';' };
            foreach (string emailBB in txtBcc.Text.ToString().Split(splitterr))
            {
                mail.Bcc.Add(new MailAddress(emailBB));
            }
        }
        else if (null == txtBcc.Text || txtBcc.Text.Length == 0 || txtBcc.Text == String.Empty)
        {
            mail.Bcc.Add(new MailAddress(txtBcc.Text.ToString()));
        }
        HttpFileCollection files = Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            mail.Attachments.Add(new Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType));

        }

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential(txtFrom.Text.ToString(), txtPass.Text.ToString());
        smtp.EnableSsl = true;
        smtp.Send(mail);
        
    }
Posted
Updated 30-Jun-13 16:48pm
v2
Comments
[no name] 30-Jun-13 22:51pm    
"please help me a google alot", google what? The error message is telling you exactly what the problem is and you are even told which line of code it is happening on. So, what is the problem? Make sure the address is not an empty string before you try and add it.

Expanding on Amits solution:

C#
foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
{
    if(!string.IsNullOrWhitespace(emailCC)){
        mail.CC.Add(new MailAddress(emailCC.Trim()));
    }
}


or, you can catch it earlier by using one of the .Split overloads:

C#
foreach (string emailCC in txtCc.Text.ToString().Split(splitter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
 
Share this answer
 
Before adding address Check for its existance. Try this:
C#
foreach (string emailCC in txtCc.Text.ToString().Split(splitter))
{
    if(emailCC.Trim() != ""){
        mail.CC.Add(new MailAddress(emailCC.Trim()));
    }
}


--Amit
 
Share this answer
 

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