从Excel工作表中提取电子邮件地址并通过outlook发送

时间:2016-12-01 14:47:27

标签: c#

我正在尝试从第3列的excel表中提取电子邮件 当我尝试运行它时,它只需要第一个电子邮件ID并通过outlook发送消息,然后获取第二个电子邮件地址并打印它但不发送消息。有谁能够帮我? 在此先感谢..

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create COM Objects. Create a COM object for everything that is referenced
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\bhavna.parmar\Documents\Addressbook.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            // Create the Outlook application.
            //Outlook.Application oApp = new Outlook.Application();
            Outlook.Application oApp = new Outlook.Application();
            // Create a new mail item.
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            Outlook.Recipients oRecips = oMsg.Recipients;

            try {

                //iterate over the rows and columns and print to the console as it appears in the file
                //excel is not zero based!!
                for (int i = 1; i <= rowCount; i++)
                {
                    for (int j = 1; j <= colCount; j++)
                    {
                        //new line
                        if (j == 1)
                            Console.Write("\r\n");

                        //write the value to the console
                        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                            //Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");

                            if (j == 3)
                            {
                                for (j = 3, i = i + 1; i <= rowCount; i++)
                                {
                                    Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
                                    //abc = xlRange.Cells[i,j].get
                                    List<string> maillist = new List<string>(); 
                                    maillist.Add(xlRange.Cells[i, j].Value2.ToString());
                                    MailMessage obj_MailMsg = new MailMessage();
                                    foreach (string to in maillist)

                                                //foreach(string to in xlRange.Cells[i,j].Value2.ToString())
                                    {
                                         obj_MailMsg.To.Add(to);
                                        //maillist.Add(xlRange.Cells[i, j].Value2.ToString());
                                        Outlook.Recipient oTORecip = oRecips.Add(xlRange.Cells[i, j].Value2.ToString());
                                        oTORecip.Type = (int)Outlook.OlMailRecipientType.olTo;
                                        oTORecip.Resolve();

                                    }                                   

                                    // Set HTMLBody. 
                                    //add the body of the email
                                    oMsg.HTMLBody = "Hello, your message body will go here!!";
                                    //Add an attachment.
                                    String sDisplayName = "MyAttachment";
                                    int iPosition = (int)oMsg.Body.Length + 1;
                                    int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                                    //now attached the file
                                    Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\Users\bhavna.parmar\Documents\Book1.xlsx", iAttachType, iPosition, sDisplayName);
                                    //Subject line
                                    oMsg.Subject = "Your Subject will go here.";
                                    oMsg.Send();
                                    Console.WriteLine("Message sent");

                                    // Clean up.
                                    oRecips = null;
                                    oRecips = null;

                                    oMsg = null;
                                    oApp = null;


                                }
                            }



                    }
                }
            }
            catch (System.Exception ex)
            {

                //status(ex.Message, Color.Red);

            }//end of catch

            //cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:
            //  never use two dots, all COM objects must be referenced and released individually
            //  ex: [somthing].[something].[something] is bad

            //release com objects to fully kill excel process from running in the background
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
            //Console.Write("sdjakkas");


        }
    }
}

0 个答案:

没有答案