发送邮件时传递参数

时间:2013-07-16 09:40:17

标签: c# asp.net html-email

我有一个任务是在asp.net c#中建立休假管理应用程序,在休假申请中当员工填写所需的详细信息并按下申请按钮时,邮件应该发送给相应的经理说

“Hello {Manager},

{LoggedInUser}已请求{NofofDays}的{TypeofLeave}登录门户接受或拒绝

谢谢。“

我能够收到邮件,但我需要的是{Manager},{TypeoLeave},{NumofDays}和{LoggedInUser}我需要相应的经理名称,登录用户以及哪种类型o离开员工已经申请了多少天。它应该从数据库中提取。

例如:“Hello ABC,

XYZ请求病假5天,请登录门户接受或拒绝

谢谢。“

现在我正在获得如上所述的tsame结构。

请帮帮我。

protected void BtnApply_Click(object sender, EventArgs e)
    {
        MTMSDTO objc = new MTMSDTO();

        int Flag = 0;

        LblLogdInUser.Text = Session["EmpName"].ToString();
        objc.LoggedInUser = LblLogdInUser.Text;
        objc.TypeofLeave = DrpTypeofLeave.SelectedItem.Text;

        string date;
        date = Convert.ToDateTime(TxtBeginDate.Text).ToString("dd/MM/yyyy");

        DateTime dt = new DateTime();
        dt = Convert.ToDateTime(date);

        objc.BeginDate = dt;
        objc.EndDate = Convert.ToDateTime(TxtEndDate.Text);
        objc.Description = TxtDescription.Text;
        objc.NumofDays = Convert.ToInt32(TxtNumofDays.Text);
        //objc.EmpName = LblLogdInUser.Text;
        objc.Status = TxtStatus.Text;


        int X = obj.InsertLeave(objc);
        {
            if (X >= 0)
            {
                Flag = 1;
            }
            else
            {
                Flag = 0;
            }
        }

        if (Flag == 1)
        {
            LblSuccess.Visible = true;
            LblSuccess.Text = "Data Added Successfully and Leave Application Succesfully Sent";
            DrpTypeofLeave.ClearSelection();
            TxtBeginDate.Text = "";
            TxtEndDate.Text = "";
            TxtDescription.Text = "";
            TxtNumofDays.Text = "";
            TxtStatus.Text = "";
        }
        else
        {
            LblErr.Visible = true;
            LblErr.Text = "Failed To Add Data and Send Leave Request!!!";
        }

        objc.EmpName = Convert.ToString(Session["EmpName"]);
        DataSet GrdLH = obj.GrdLeaveHistory(objc);
        DataView GrdLeaveH = new DataView();
        GrdLeaveH.Table = GrdLH.Tables[0];
        GridViewLeaveHistory.DataSource = GrdLeaveH;
        GridViewLeaveHistory.DataBind();


        MailMessage message = new MailMessage();
        message.To.Add("abcer@abc.in");
        message.Subject = "Leave Request";
        message.From = new MailAddress("abc@gmail.com");
        message.IsBodyHtml = true;

        message.Body = "<span style = font-family:Arial,font-size:10pt>";
        message.Body += "Hello <b>{Manager}</b>,<br /><br />";
        message.Body += "{LoggedInUser}has requested {TypeofLeave} for {NumofDays}"; 
        message.Body += "kindly login to the portal to accept or reject it";
        message.Body += "<br />";
        message.Body += "<br />";
        message.Body += "Thank You.<br />";
        message.Body += "</span>";
        SmtpClient smtp = new SmtpClient("");
        smtp.Send(message);
    }

2 个答案:

答案 0 :(得分:1)

将您的代码更改为以下内容:[确保您拥有经理姓名,登录用户名,请假类型和NoOfDays存储在会话中或您喜欢的任何内容,但相应地更改变量]

  message.Body += "Hello <b>"+Session["Manager"].ToString()+"</b>,<br /><br />";
  message.Body += Session["LoggedInUser"].ToString()+"has requested"+
                  Session["TypeOfLeave"].ToString()+"for "+Session["NoOfDays"].ToString()+
  mesaage.Body += "kindly login to the portal to accept or reject it";

答案 1 :(得分:0)

我们可以在运行时创建html模板,而不是在所有占位符的文本或html文件中定义模板(如{ManagerName},{RequestedUser})。在发送邮件时,我们可以将带有密钥的字典作为{ManagerName},{RequestedUser}和值作为它们各自的数据库值。现在读取文件然后我们可以迭代Dictionary的键并用模板中的值替换键。完成这些操作后,我们创建了消息体。