c# – 参数’addresses’不能是空字符串

前端之家收集整理的这篇文章主要介绍了c# – 参数’addresses’不能是空字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 System.Net.Mail.SmtpClient类发送电子邮件asp.net.

但是我收到以下异常消息:

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

这是我发送的电子邮件代码

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

异常被捕获在SendEmailUsingGmail catch块中.

这是调用代码

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

管理员批准/拒绝文档时,数据会像这样保存到数据库

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

现在我也发送电子邮件
管理员点击按钮然后电子邮件发送到相应的电子邮件ID
像这样我在转发表中显示

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)

解决方法

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

并验证变量“toEmailAddress”的内容.

原文链接:/csharp/91448.html

猜你在找的C#相关文章