Accessing Resource File Key and Email In ASP.Net Application


Today we'll discuss about to access the resource file content or we can say any string in the ASP.NET. In these days I am working, on a project and in that I have to access the content stored in the resource file which is created in the App_GlobalResource (ASP.NET Folder).

In that context, we'll proceed here with the following sections:

  • Create ASP.NET Application
  • Connect ASP.NET to Excel
  • Working With App_GlobalResource
  • Emailing

Create ASP.NET Application

In this section, we'll create an application by using the following steps:

Step 1: Create a new ASP.NET Application with an Empty Project Template as named SendExcelApp

Step 2: Now create a Web Form and add the following code in the body:

<table style="height: 299px; width: 389px">


        <td>Select File:</td>

        <td><asp:FileUpload ID="FileUploadExcel" runat="server" meta:resourcekey="FileUploadExcelResource1" /></td>



        <td colspan="2" class="auto-style1"><asp:Button ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" meta:resourcekey="BtnUploadResource1" /></td>




        <td><asp:TextBox ID="TxtSubject" runat="server" meta:resourcekey="TxtSubjectResource1"></asp:TextBox></td>



        <td colspan="2">

            <asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="False" Height="78px" Width="349px" meta:resourcekey="DataGridViewResource1">


                    <asp:TemplateField HeaderText="Name" meta:resourcekey="TemplateFieldResource1">


                            <asp:Label ID="LblName" runat="server" Text='<%# Bind("F1") %>' meta:resourcekey="LblNameResource1"></asp:Label>



                    <asp:TemplateField HeaderText="Email ID" meta:resourcekey="TemplateFieldResource2">


                            <asp:Label ID="LblEmail" runat="server" Text='<%# Bind("F2") %>' meta:resourcekey="LblEmailResource1"></asp:Label>









            <asp:Button ID="BtnSendMail" runat="server" Text="SendMail" OnClick="BtnSendMail_Click" meta:resourcekey="BtnSendMailResource1" />



            <asp:Label ID="LblMessage" runat="server" meta:resourcekey="LblMessageResource1"></asp:Label>




Connect ASP.NET to Excel

In this section we'll connect the Excel file to the application. Proceed with the following steps:

Step 1: Create a folder named Data in the project

Step 2: Add a class named DAL and replace the code with the code below:

using System;

using System.Data;

using System.Data.OleDb;


namespace SendExcelApp


    public class DAL


        OleDbDataAdapter DbAdap;

        DataTable dt;

        internal object Get_File(string MyFile)


            string connString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=(Your Application Path)/Data/{0} ;Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\";", MyFile);


            OleDbConnection DbCon = new OleDbConnection(connString);


            DbAdap = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", DbCon);

            dt = new DataTable();


            return dt;




Note: You must have the Office driver to access the Excel file. Please refer Excel Connectivity

Step 3: In the WebForm.cs file, we'll upload the Excel file and bind it to the Grid View. Proceed with the following sections:

  • Add the following code above the Page_Load()

    string MyFile;

    DAL obj = new DAL();

  • Modify the Page_Load() event with the code below:

    protected void Page_Load(object sender, EventArgs e)


        LblMessage.Visible = false;

        BtnSendMail.Enabled = false;


  • Now we'll create an event for the Upload button which is used to upload the Excel file and then bind it to the Grid View by using the following code:

    protected void BtnUpload_Click(object sender, EventArgs e)



        BtnSendMail.Enabled = true;



    void UploadFile()


        HttpPostedFile PostedFile = Request.Files["FileUploadExcel"];


        if (PostedFile != null && PostedFile.ContentLength > 0)


            MyFile = Path.GetFileName(PostedFile.FileName);

            PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));





            LblMessage.Text = "Missing File";

            LblMessage.Visible = true;




    private void Get_Data(string MyFile)


        DataGridView.DataSource = obj.Get_File(MyFile);



Step 4: Run the application and after uploading the Excel file the web form will look like the following screenshot:

Get Excel in WebForm

Working With App_GlobalResource

In this section, we'll add an ASP.NET Folder named App_GlobalResource and in which we'll add a Resource1.resx file to create a string. Proceed the steps given below:

Step 1: Just right click on your project to add an App_GlobalResource

Adding App_GloabalResource in WebApp

Note: The folder is already exist on my project, so that it's unable to show.

Step 2: Now, add a Resources file named Resource1.resx

Adding resource File

Step 3: Now Rename the string as MyTemplate and add an html code in the value as shown in the following screenshot:

Working with Resource File

Step 4: Now in this step we'll add the code to read the key (MyTemplate) and replace it with the UserName and subject. Create the following method in your class :

private string ReadTemplate(string name, string subject)


    string resourceName = (string)HttpContext.GetGlobalResourceObject("Resource1", "MyTemplate");

    string mystring = "";

    mystring = resourceName;

    mystring = mystring.Replace("$$Member$$", name);

    mystring = mystring.Replace("$$Subject$$", TxtSubject.Text);

    return mystring;


In the above code, we mentioned the key and the resource name in the GetGlobalResourceObject() and store it in the string varibale.


Now in this section we'll use the body of the resource key as a email body and email it. Proceed with the following steps:

Step 1: At first we'll configure the Web.Config file. Modify the Web.Config file with the following code:



  <add key="FromMail" value="Your-Email-ID"/>

  <add key="Password" value="Your-Email-Password"/>

  <add key="Host" value=""/>


Step 2: Create an event for SendMail Button and code to email with the help of following code:

protected void BtnSendMail_Click(object sender, EventArgs e)





void Send_Mail()




        string Pass, FromEmailid, HostAdd;

        foreach (GridViewRow gr in DataGridView.Rows)


            HostAdd = ConfigurationManager.AppSettings["Host"].ToString();

            FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();

            Pass = ConfigurationManager.AppSettings["Password"].ToString();


            Label LblName = gr.FindControl("LblName") as Label;

            Label LblMail = gr.FindControl("LblEmail") as Label;


            string Name = LblName.Text;

            string Mail = LblMail.Text;

            string subject = TxtSubject.Text;


            SmtpClient client = new SmtpClient();

            MailMessage msg = new MailMessage();


            NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);


            client.Host = HostAdd;

            client.Port = 25;

            client.UseDefaultCredentials = false;

            client.Credentials = credentials;

            client.EnableSsl = true;

            MailAddress from = new MailAddress(FromEmailid);

            msg.IsBodyHtml = true;

            msg.Subject = subject;

            msg.Body = ReadTemplate(Name, subject);


            msg.From = from;


            LblMessage.Text = "Email Send Successfully";

            LblMessage.Visible = true;




    catch (Exception Ex)


        LblMessage.Text = Ex.Message;

        LblMessage.Visible = true;



Step 3: Run the application. At first select the Excel file to upload.

Binding Excel File in ASP.NET

Now click on the SendMail button.

Emailing in ASP.NET


So far this article will help you to connect the Excel file to the ASP.NET Application. You can also access the Resource file Key stored in the App_GlobalResource in the web application and use the html body as an email body and emailing concept. Happy Coding and thanks for reading.