11

I am trying to send an email in ASP.NET using the System.Net.Mail.SmtpClient class.

However I am getting the following exception message:

The parameter 'addresses' cannot be an empty string. Parameter name: addresses

This is my send email code:

private void SendEmailUsingGmail(string toEmailAddress)
{
    try
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new NetworkCredential("keysketyyyy@gmail.com", 
         "sdsdasd");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        MailMessage message = new MailMessage();
        message.From = new MailAddress("keysketyyy@gmail.com");
        message.To.Add(toEmailAddress);
        message.Subject = "Write your email subject here";
        message.Body = "write the content of the email here";
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        Response.Write("Error occured: " + ex.Message.ToString());
    }
}

The exception is being caught in the SendEmailUsingGmail catch block.

This is the calling code:

protected void Button1_Click(object sender, EventArgs e)
{            
   string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
   SqlConnection mySQLconnection = new SqlConnection(connStr);
   if (mySQLconnection.State == ConnectionState.Closed)
   {
       mySQLconnection.Open();
       for (int i = 0; i < Repeater2.Items.Count; i++)
       {
           DropDownList DropDownListcontrol = ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4"));
           Label DocId = ((Label)Repeater2.Items[i].FindControl("DocId"));
                  
           SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection);
           cmd.CommandType = CommandType.StoredProcedure;

           cmd.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32((DocId.Text));

           cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = Convert.ToInt32(DropDownListcontrol.SelectedValue);
           cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = (Session["Login2"]);
            try
            {
                cmd.ExecuteNonQuery();
                string emailId = ((Label)Repeater2.Items[i].FindControl("Label2")).Text;
                SendEmailUsingGmail(emailId);
             }
             catch (Exception ex)
             {
                 Supvisor.Text=(ex.Message);
             }
             cmd.ExecuteNonQuery();
             //UPDATE APPPROVEID IN DOCUMENTINFO TABLE
             //DMSLIB.Doc myDoc = new DMSLIB.Doc();
             //myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), 
             Convert.ToInt32(DropDownListcontrol.SelectedValue));
        }

    }
    else
    {
         Supvisor.Text = "Error";
    }
    if (mySQLconnection.State == ConnectionState.Open)
    {
         mySQLconnection.Close();
    }
 }

When an admin approves / rejects a document the data is saved into database like this:

SeqNo   DocID   ApproveID   ApproveBy
82      20      3           john
83      21      1           john
84      18      2           kety
85      19      1           emel

I also send an email when admins click a button, the email is sent to respective email ids like this, as I show in a repeater table:

 DocID  DocName Uplaodedfile    UserEmail           DocType DepType ApproveID
 1      ABC     def.pdf         abcdef@gmail.com    pdf     hr      (In this i set dropdown values are (approve/reject/pending)

  
TylerH
  • 20,799
  • 66
  • 75
  • 101
user2931015
  • 219
  • 2
  • 8
  • 21

2 Answers2

9

Make sure to use this line (note the New MailAddress declaration):

message.To.Add(New MailAddress(toEmailAddress));

and verify the contents of the variable toEmailAddress at submission time. This error occurs when it is empty.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Nacho
  • 1,023
  • 1
  • 10
  • 26
  • when i add this it shows me error in this line ..Error 200 ) expected ,Error 201 Invalid expression term ')',Error 202 ; expected – user2931015 Nov 21 '13 at 17:49
  • What value have the variable "toEmailAddress"? – Nacho Nov 21 '13 at 17:52
  • in this line i call a label ...(Label)Repeater2.Items[i].FindControl("Label2")).Text; in html like this <%#DataBinder.Eval(Container.DataItem, "UserEmail")%> – user2931015 Nov 21 '13 at 17:58
  • Ok, I understand. Put a breakpoint in this line "message.To.Add(toEmailAddress);" from the SendEmailUsingGmail method, and tell me your value. – Nacho Nov 21 '13 at 18:15
  • Then this is your error, the "message.To.Add("");" is fail. You need verify the line "string emailId = ((Label)Repeater2.Items[i].FindControl("Label2")).Text;" in the Button1_Click. Chek the content of Label2. – Nacho Nov 21 '13 at 18:22
  • how i check the content??and so what i done to solve this issue – user2931015 Nov 21 '13 at 18:27
3

This exception is being thrown by the MailAddress constructor. It means you are trying to add an empty string as an email address, which is not valid.

In this case it means toEmailAddress is an empty string.

mattumotu
  • 1,436
  • 2
  • 14
  • 35