title: send mail with attachement in c# using outlook
body: <p>I m getting error when i m trying to send email to different Recipients with their respective attachment. i m using looping to change the Recipients and attachment but getting error. pls help to resolve this issue
  my code is</p>
  
  <pre><code>private void BtnEmail_Click(object sender, EventArgs e)
          {
              try
              {
                  string[] fileEntries = System.IO.Directory.GetFiles(txtPdfFiles.Text, "*.pdf");
                  Outlook.Application oApp = new Outlook.Application();
                  Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                  if (RtxtBox.Text == "")
                  {
                      MessageBox.Show("Please set Mail body text");
                      GrpMailBody.Visible = true;
                  }
                  else
                  {
                      oMsg.HTMLBody = RtxtBox.Text;
                  }
                  if (RtxtSubject.Text == "")
                  {
                      MessageBox.Show("Please Enter Mail Subject");
                      GrpMailBody.Visible = true;
                  }
                  else
                  {
                      oMsg.Subject = RtxtSubject.Text;
                  }
  
                  String sDisplayName = "MyAttachment";
                  int iPosition = (int)oMsg.Body.Length + 1;
                  int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                  Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
  
                  for (int i = 0; i &lt;= grvExcelData.RowCount; i++)
                  {
  
                      string EmaildID = grvExcelData.Rows[i].Cells[3].Value.ToString();
                      string sFileName = grvExcelData.Rows[i].Cells[5].Value.ToString()+".pdf";
                      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(EmaildID);
  
                      foreach (string fileName in fileEntries)
                      {
                          string fileN = "";
                          string xfileName;
  
                          xfileName=System.IO.Path.GetFileName(fileName);
                          if (xfileName == sFileName)
                          {
                              Outlook.Attachment oAttach = oMsg.Attachments.Add(@fileName, iAttachType, iPosition, sDisplayName); //getting error in this line
  
                          }
                          else
                          {
                          }
  
                      }
                      oRecip.Resolve();
                      oMsg.Send();
                      oRecip = null;
                      //oRecips = null;
                      oMsg = null;
                      oApp = null;
  
                  }
  </code></pre>