Welcome

Welcome to my blog. Here you can find few tips about Microsoft Dynamics NAV.

Thursday, 16 February 2012

Read an Email in outlook through automation in Navision

we are all going to know about Reading an Email through automation in Navision

1) How to read an unread message  and filter based on sendername in the Inbox:
              Variables:
              1.outlookapplication-automation-'Microsoft Outlook 12.0 Object Library'.Application
              2.outlookemail-automation-''Microsoft Outlook 12.0 Object Library'.MailItem
              3.outlooknamespace-automation-Microsoft Outlook 12.0 Object Library'.NameSpace
              4.outlookitems-automation-Microsoft Outlook 12.0 Object Library'.Items
              5.outlookMAPIFolder-automation-Microsoft Outlook 12.0 Object Library'.MAPIFolder
              6.findcriteria-text-250
             7.endofloop-integer
             8. i - integer.
            9.Filenew-File
           10.Objout-Outstream

IF ISCLEAR(outlookapplication) THEN
  CREATE(outlookapplication);

outlooknamespace:=outlookapplication.GetNamespace('MAPI');
outlooknamespace.Logon('','',TRUE,FALSE);

outlookMAPIFolder:=outlooknamespace.GetDefaultFolder(6);
outlookitems:=outlookMAPIFolder.Items;
findcriteria:='[sendername] = ' 'Karthik Durairaj' ' ';
findcriteria:=findcriteria+' AND [Unread] = True';
outlookitems:=outlookMAPIFolder.Items.Restrict(findcriteria);


i:=1;
endofloop:=outlookitems.Count;
WHILE i<=endofloop DO
BEGIN
  outlookemail:=outlookitems.Item(i);
  IF CONFIRM(outlookemail.Subject) THEN
  BEGIN
   // will write the body of the email in a file
    Filenew.CREATE('d:\mailmessages1.txt');
    Filenew.CREATEOUTSTREAM(Objout);
    Objout.WRITETEXT(outlookemail.Body);
    Filenew.CLOSE;
     i:=i+1;
  END
END;

2)How to read an unread message in the subfolder:

outlooknamespace:=outlookapplication.GetNamespace('MAPI');
outlooknamespace.Logon('','',TRUE,FALSE);

outlookMAPIFolder:=outlooknamespace.GetDefaultFolder(6);
foldersnew:=outlookMAPIFolder.Folders; // where foldersnew=automation-Microsoft Outlook 12.0 Object Library'.Folders
foldercount:=foldersnew.Count;//where foldercount-integer
FOR i:=1 TO foldercount DO
BEGIN
subfolder:=foldersnew.Item(i);//subfolder-automation -Microsoft Outlook 12.0 Object Library'.MAPIFolder
IF subfolder.Name='Help Desk' THEN //"Help Desk' is my subfolder of Inbox
BEGIN
  outlookitems:=subfolder.Items;
  findcriteria:='[Unread] = True';
  outlookitems:=subfolder.Items.Restrict(findcriteria);
  j:=1;
  endofloop:=outlookitems.Count;
  WHILE j<=endofloop DO
  BEGIN
    outlookemail:=outlookitems.Item(j);
    IF CONFIRM(outlookemail.Subject) THEN
    BEGIN
      Filenew.CREATE('d:\mailmessages1.txt');
      Filenew.CREATEOUTSTREAM(Objout);
      Objout.WRITETEXT(outlookemail.Body);
      Filenew.CLOSE;
          j:=j+1;
    END;
  END;
END;
END;


No comments:

Post a Comment