Thursday, October 6, 2011

Multiple attachments (Outlook Email with Dynamics NAV)

Sending emails with more than one attachment via Outlook. 


Check example the code.



01 ) 
  Define Global ;
  intArrayIndex as Integer;
  ListFileNames Text 250 with two Dimension(array length)
     
      intArrayIndex := 1;
      AddToList(EmailFilename); //Example :- E://Email/PurchaseOrder.pdf
      AddToList(EmailFilename2); //Example :-E://Email/SalesOrder.pdf
   

02) Define New Function AddtoList() with Parameter FIleNameLoc




Function contains





IF ListFileNames[intArrayIndex] = '' THEN
    ListFileNames[intArrayIndex] := FileNameLoc
ELSE BEGIN
    intArrayIndex += 1;
    ListFileNames[intArrayIndex] := FileNameLoc;
END;


03) Create New Function in Mail Codeunit as NewMessage2


Define Local Variables



Name                 DataType Length


Stop           Boolean
TempString         Text 250
Position         Integer
Separator         Text 1
Length         Integer
intArrayIndex Integer




Function Contains.



IF ISCLEAR(OApplication) THEN
  CREATE(OApplication);


IF (NOT OApplication.Logon(TRUE,'','',FALSE,FALSE)) THEN BEGIN
  OApplication.Logoff;
  EXIT
END;


IF ISCLEAR(OSendMail) THEN
  CREATE(OSendMail);


ErrorNo := 0;
ErrorDesc := '';


OSendMail."To" := ToName;
OSendMail.CC := CCName;
OSendMail.Subject := Subject;
OSendMail.BodyFormat := 2;
MailGUIDValue := CREATEGUID;
OSendMail.SetUserProperty(GetMailGUIDFieldName,1,FORMAT(MailGUIDValue));


IF ISCLEAR(BSTRConverterBody) THEN
  CREATE(BSTRConverterBody);


IF Body <> '' THEN BEGIN
  BSTRConverterBody.ResetBSTR;
  BSTRConverterBody.AppendNextStringPortion(Body);
END;
OSendMail.Body := BSTRConverterBody;


IF ISCLEAR(BSTRConverterAttachFileName) THEN
  CREATE(BSTRConverterAttachFileName);


//Modified Code Sameera --> BEgin
intArrayIndex := 1;
IF AttachFileName[intArrayIndex] <> '' THEN
 REPEAT
  Stop := FALSE;
  Separator := ';';
  REPEAT
   Length :=STRPOS(AttachFileName[intArrayIndex],Separator);
   IF Length = 0 THEN BEGIN
    TempString := AttachFileName[intArrayIndex];
    Stop := TRUE;
   END
   ELSE BEGIN
    TempString := COPYSTR(AttachFileName[intArrayIndex], 1 , Length-1);
    AttachFileName[intArrayIndex] := COPYSTR(AttachFileName[intArrayIndex], Length + 1);
   END;


   BSTRConverterAttachFileName.ResetBSTR;
   BSTRConverterAttachFileName.AppendNextStringPortion(TempString);
   OAttachments := OSendMail.Attachments;
   OAttachment := OAttachments.Add(BSTRConverterAttachFileName);
  UNTIL Stop;
  intArrayIndex := intArrayIndex + 1
 UNTIL (intArrayIndex = 41) OR (AttachFileName[intArrayIndex] = '');
//Sameera -> End


OSendMail.OpenDialog := OpenDialog;


MailSent := OSendMail.Send;
ErrorNo := OSendMail.ErrorStatus;
ErrorDesc := OSendMail.ErrorDescription;
OApplication.Logoff;




04 ) Calling New Email Function in Mail CodeUnit (397)


   Mail.NewMessage2(EmailAdd,'',Subject,BodyText,ListFileNames,FALSE)