System.NullReferenceException:未将对象引用设置为对象的实例。 (发送)

时间:2016-05-16 06:02:53

标签: c#

(请记住我是初学者,但仍然几乎不懂语言) 按钮的功能是向特定的电子邮件发送消息,我使用Web服务进行消息传递,但此错误不断弹出,  这是按钮的代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        Msg.Service M = new Msg.Service();
        M.InsertNewMessage(Session["Email"].ToString(), "lovly_b@hotmail.com", this.TextBox2.Text.ToString());
        this.Label4.Text = "the massege had sent succesfully";
        this.Label4.Visible = true;
    }

这是错误页面中突出显示的行:

M.InsertNewMessage(Session["Email"].ToString(), "lovly_b@hotmail.com", this.TextBox2.Text.ToString());

以下是InsertNewMessage函数的代码:

[WebMethod]
    public string InsertNewMessage(string sender, string To, string message)
    {
        DateTime d = DateTime.Now;
        message m = new message(sender, To, message, d);
        m.SaveToDB();
        return "Inserted successfully";
    }

1 个答案:

答案 0 :(得分:0)

可议会话[“电子邮件”]为空,您应该将其填充到某个地方