【问题标题】:The parameter 'addresses' cannot be an empty string参数“地址”不能为空字符串
【发布时间】:2013-11-21 17:30:11
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: c# asp.net system.net.mail


    【解决方案1】:

    试试

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

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

    【讨论】:

    • 当我添加它时,它在这一行显示错误..错误 200) 预期,错误 201 无效的表达式术语 ')',错误 202;预计
    • 变量“toEmailAddress”有什么值?
    • 在这一行中我称之为标签 ...(Label)Repeater2.Items[i].FindControl("Label2")).Text;在 html 中像这样
    • 好的,我明白了。在这一行“message.To.Add(toEmailAddress);”中放置一个断点从 SendEmailUsingGmail 方法,告诉我你的价值。
    • 那么这是你的错误,"message.To.Add("");"是失败。您需要验证“string emailId = ((Label)Repeater2.Items[i].FindControl("Label2")).Text;”行在 Button1_Click 中。检查 Label2 的内容。
    【解决方案2】:

    MailAddress 构造函数抛出此异常。这意味着您正在尝试添加一个空字符串作为电子邮件地址,这是无效的。

    在这种情况下,这意味着toEmailAddress 是一个空字符串。

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 2017-12-30
      • 1970-01-01
      • 2018-09-24
      • 2014-12-18
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多