Download attachment Outlook

RPA

New member
Joined
May 15, 2019
Messages
1
Programming Experience
Beginner
Hello everyone
I need help
this code download the attachment of an email by giving him the subject of the email
C#:
/*
This Business Object makes use of the Microsoft.Office.Interop.Outlook assembly, 
it's referenced in the code options tab of the BO properties, as an external reference
*/
DataTable edbFnames = new DataTable();
edbFnames.Columns.Add(new DataColumn("File Name", typeof(String))); 
int index = 1;
DateTime edate = DateTime.Now.Date;
if( ddate != DateTime.MinValue)
edate = ddate;
Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application(); 
Folder root = Application.Session.DefaultStore.GetRootFolder() as Folder; //Gets the root folder
Folders childFolders = root.Folders; //Gets the child folders of the root folder
if (childFolders.Count > 0)
{
    foreach (Folder childFolder in childFolders) //Iterates through ROOT's child folders
    {
       if (childFolder.FolderPath.Contains(inboxF)) //Finds the Inbox folder
       {             
   var fi = childFolder.Items;
   if (fi != null)
{
    foreach (Object item in fi) //Iterates through Inbox's objects
                {
                    try
                    {
                        MailItem mi = (MailItem)item; //Cast Objects to Mails
                        if(mi.Subject.Contains(SMC) && mi.CreationTime.Date.Equals(edate) && mi.UnRead) //Look for the mail that regards all the conditions
{
mi.UnRead = false;
if(mi.Attachments.Count > 0) //Check if the mail has attachments
                              for (int i = 1; i <= mi.Attachments.Count; i++) //Iterates throught attachments
  {
  if(mi.Attachments.FileName.ToLower().Contains("xlsx"))
  {
  mi.Attachments.SaveAsFile(dPath + mi.Attachments.FileName); //Save the excel attachment in a given path
  edbFnames.Rows.Add(dPath + mi.Attachments.FileName); //Store the excel file name in a DataTable(Collection)
  }
index++;
  }
}                          
                    }
                    catch{}
                }
             }
        }
    }
}
EDB = edbFnames;
I need to modify it so that it downloads the attachment of a specific account, it can have more than one account configured in outlook, sorry for my bad English
Thanks
 
Back
Top Bottom