Raushan Raj

Raushan Raj

  • NA
  • 23
  • 11.7k

How can I send generic list<T> in the body of outlook mail

Apr 15 2017 1:15 AM
I am using C# for my problem.
I have an Excel file which has many worksheets. From "Fisrt Sheet", I am looking for a character "x" which will be present in some cells of a specific column (occurence of "x" will be in one specific column only, in different cells of that column). I am looking for "x" and extracting the corresponding row's details in a generic list (with naming the headers of extracted field). Now, I have to send this generic list<t> in "tabular format" in body of the mail via Outlook.
I am stuck with sending of list in tabular format in the mail body.
Please help me with my problem
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Data;  
  7. using Excel = Microsoft.Office.Interop.Excel;  
  8. using Outlook = Microsoft.Office.Interop.Outlook;  
  9.   
  10. namespace xlsm  
  11. {  
  12.     class New  
  13.     {  
  14.         static void Main(sting[] args)  
  15.         {  
  16.         sting st;   
  17.             long rCnt, cCnt;  
  18.             long rows = 0, columns = 0;  
  19.   
  20.             Excel.Application xlApp;  
  21.             Excel.Workbook xlWorkBook;  
  22.             Excel.Worksheet xlWorkSheet;  
  23.             Excel.Range rng;  
  24.   
  25.             xlApp = new Excel.Application();  
  26.             xlWorkBook = xlApp.Workbooks.Open(@"F:\Doc_Excel", 0, true, 5, """"true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t"falsefalse, 0, true, 1, 0);  
  27.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["First Sheet"];  
  28.   
  29.   
  30.             rng = xlWorkSheet.UsedRange;  
  31.             rows = rng.Rows.Count;  
  32.             columns = rng.Columns.Count;  
  33.   
  34.             List<Memo> lst = new List<Memo>();  
  35.   
  36.             for (rCnt = 1; rCnt < rows; rCnt++)  
  37.             {  
  38.                 for (cCnt = 1; cCnt < columns; cCnt++)  
  39.                 {  
  40.                       
  41.                     if ((rng.Cells[rCnt, cCnt] as Excel.rng).Value2 != null)  
  42.                     {  
  43.                         st = (rng.Cells[rCnt, cCnt] as Excel.rng).Value2.Tosting();  
  44.                         if (st == "x")  
  45.                         {  
  46.                             Memo ms = new Memo();  
  47.                               
  48.                             ms.MemoName = (rng.Cells[rCnt, 1] as Excel.rng).Value2.Tosting();  
  49.                             ms.Type = (rng.Cells[rCnt, 2] as Excel.rng).Value2.Tosting();  
  50.                             ms.Ext = (rng.Cells[rCnt, 3] as Excel.rng).Value2.Tosting();  
  51.                             ms.Seller = (rng.Cells[rCnt, 4] as Excel.rng).Value2.Tosting();  
  52.                             ms.Warehouse = (rng.Cells[rCnt, 5] as Excel.rng).Value2.Tosting();  
  53.                             lst.Add(ms);    
  54.   
  55.                         }  
  56.                     }  
  57.                      
  58.                 }  
  59.             }  
  60.   
  61.             try  
  62.             {  
  63.                   
  64.                 Outlook.Application oApp = new Outlook.Application();                 
  65.                 Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);  
  66.                 oMsg.HTMLBody = "";  
  67.                 oMsg.Subject = "Memo contents as required.";                 
  68.                 Outlook.Recipients oRecims = (Outlook.Recipients)oMsg.Recipients;                 
  69.                 Outlook.Recipient oRecip = (Outlook.Recipient)oRecims.Add("[email protected]");  
  70.                 oRecip.Resolve();                 
  71.                 oMsg.Send();  
  72.                   
  73.                 oRecip = null;  
  74.                 oRecims = null;  
  75.                 oMsg = null;  
  76.                 oApp = null;  
  77.             }  
  78.             catch (Exception ex)  
  79.             {  
  80.             }  
  81.             xlWorkBook.close(truenullnull);  
  82.             xlApp.Quit();  
  83.   
  84.             Marshal.ReleaseComObject(xlWorkSheet);  
  85.             Marshal.ReleaseComObject(xlWorkBook);  
  86.             Marshal.ReleaseComObject(xlApp);  
  87.         }          
  88.   
  89.     }  
  90.     public class Memo  
  91.     {  
  92.         public string MemoName { getset; }  
  93.         public string Type { getset; }  
  94.         public string Ext { getset; }  
  95.         public string Seller { getset; }  
  96.         public string Warehouse { getset; }  
  97.     }  

Answers (7)