Click here to Skip to main content
15,898,607 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi sir,

I am uploading one excel file which have two cols one is for Full Name & the other is Email. Now i want send email to those who are in Excel sheet Emails. Then what should be my Send_Mail Action in Controller . Please Help.

This is my Email_Load Action Post code in controller. and which excel file uploading successfully.

C#
//Post
       [HttpPost, ActionName("Email_Load")]
       public ActionResult Email_Load_confirmed(HttpPostedFileBase file)
       {
           File_Model tz = new File_Model();

           try
           {
               if (file != null)
               {

                   file.SaveAs(Server.MapPath("~/App_Data/Uploads/Contacts.xlsx" + ".xlsx"));
                   ViewBag.Message = "file is uploaded successfully!";
                   _Stores Stores_ = new _Stores();
                   Basic_Helper _Basic_Helper = new Basic_Helper();
                   var Store = Stores_.Get_Store_Info_Prd(_Basic_Helper.Format_URL(Request.Url.Host.ToString()));
                   if (Store != null)
                   {
                       _Basic_Helper = null;
                       _Site_Info Site_Info = new _Site_Info();
                       string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/Uploads/Contacts.xlsx") + ";";
                       connectionString += "Extended Properties=Excel 8.0;";
                       // always read from the sheet1.
                       OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];");
                       OleDbConnection myConnection = new OleDbConnection(connectionString);
                       myConnection.Open();
                       myCommand.Connection = myConnection;
                       OleDbDataReader myReader = myCommand.ExecuteReader();
                       while (myReader.Read())
                       {
                           if (!myReader.GetValue(1).ToString().Equals(""))
                           {
                               Site_Info.Insert_Cust_Det_Newsletter(Store.ID, myReader.GetValue(0).ToString(), myReader.GetValue(1).ToString());
                           }
                       }
                       myReader.Close();
                       myCommand.Cancel();
                       myConnection.Close();
                       myConnection = null;
                       Site_Info = null;
                   }
                   Store = null;
                   Stores_ = null;
                   System.IO.File.Delete(Server.MapPath("~/App_Data/Uploads/Contacts.xlsx"));


               }
           }
           catch (Exception ex)
           {
               string t = ex.ToString();
               t = "";
               // Response.Write(ex.ToString());


           }

           return View();

       }

And this is my View Code For Send_Mail
HTML
<form method="POST" action="/cgi-bin/email">
                   <table>
                       <tr>
                           <td>From:*</td>
                           <td>
                               <input name="sender" value="admin@tradercart.com" placeholder="admin@tradercart.com" required></td>
                       </tr>
                       <tr>
                           <td>Subject:*</td>
                           <td>
                               <input name="sender" placeholder="Subject" required></td>
                       </tr>
                       <tr>
                           <td>Please type a message:</td>
                           <td>
                               <textarea name="content_message" rows="5" cols="60" placeholder="Here's some text."></textarea></td>
                       </tr>
                       <tr>
                           <td></td>
                           <td>
                               <input type="submit" value="Send">
                               <input type="reset" value="reset"></td>

                       </tr>
                   </table>
               </form>

Kindly Tell me what should be Send_Mail Action In controller?????
C#
// Post
        public ActionResult Send_Mails()
        {
             ??
        }

Please help me its will be highly appreciated

Thanks

Regards,

Tahir
Posted
Updated 10-Sep-15 14:26pm
v2
Comments
[no name] 10-Sep-15 5:24am    
Do you want to execute both functions perform at a time. That means when you are calling Email_Load_confirmed() , you want to execute Send_Mails() also?

If yes then there is no need to declare as public and return type as ActionResult of Send_Mails. You can declare like private bool Send_Mails(){}

If no then your assumption is correct one.
Member 11968031 10-Sep-15 5:43am    
i am not calling both, i just post my upload code for easy that relevant this you guys post the solution. I just want send emails to those emails which is excel file Named Emails.
[no name] 10-Sep-15 5:52am    
I am asking do you to perform both the file upload and send e-mail at one sort?
Member 11968031 10-Sep-15 7:22am    
i am perform both but not at a time, separately first upload a excel file then when ever need i want email send

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900