SMTP Mail codeunit AddAttachmentStream

MagnoMagno Member Posts: 168
edited 2013-02-27 in NAV Three Tier
Did anyone ever use the AddAttachmentStream function already?
I have attachments in a blob field for which I create an instream, but all I get is an empty file in the email...
There are no bugs, only random undocumented features...
---
My Blog: http://NAV-Magno.be

Answers

  • MagnoMagno Member Posts: 168
    In the mean time, I logged the error on PartnerSource, because it seems the DotNet SMTP Assembly has a flaw in some cases.

    I'll let you know, if I know anything more.
    There are no bugs, only random undocumented features...
    ---
    My Blog: http://NAV-Magno.be
  • MagnoMagno Member Posts: 168
    A little more explanation of the problem:

    when SMTP is used to send notifications with attachments using a stream, if the stream is created from a record that is local and the send procedure is called from another function the attachment file arrives empty.

    if the record is not local, or if the send function is called from the same function, or both together, the notification arrives with the attachment without problem.

    I created attached test codeunit to proof my point to MS. Test1 fails, the other tests work as expected. MS already verified there should not be any limitation.
    OBJECT Codeunit 50000 test SMTP
    {
      OBJECT-PROPERTIES
      {
        Date=19/02/13;
        Time=11:59:34;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                MyFirstFile := 'd:\temp\testfile.txt';
                MyFirstFileName := 'testfile.txt';
                MySecondFile := 'd:\temp\testfile_2.txt';
                MySecondFileName := 'testfile_2.txt';
                MyFromName := 'MyFromName';
                MyFromAddress := 'from@email.com';
                MyToAddress := 'to@email.com';
                MySubject := 'Attachment Test %1';
                MyBody := 'Testing different attachment handling';
    
                // Test 1: Different functions, as much local as possible
                ResetVars;
                MyTestNo := 1;
                Test1_step1;
                Test1_step2;
                Test1_step3;
    
                // Test 2: All in 1 function, again as much local as possible
                ResetVars;
                MyTestNo := 2;
                Test2;
    
                // Test 3: Send in same function as add Attachment
                ResetVars;
                MyTestNo := 3;
                Test3_step1;
                Test3_step2;
    
                // Test 4: Same as test 1, tempblob in global
                ResetVars;
                MyTestNo := 4;
                Test4_step1;
                Test4_step2;
                Test4_step3;
    
                // Test 5: Same as test 4, both files through stream
                ResetVars;
                MyTestNo := 5;
                Test5_step1;
                Test5_step2;
                Test5_step3;
              END;
    
      }
      CODE
      {
        VAR
          MyTestNo@1013 : Integer;
          MyFirstFile@1002 : Text;
          MyFirstFileName@1000 : Text;
          MySecondFile@1004 : Text;
          MySecondFileName@1012 : Text;
          MyFromAddress@1007 : Text;
          MyFromName@1008 : Text;
          MyToAddress@1011 : Text;
          MySubject@1009 : Text;
          MyBody@1010 : Text;
          "---"@1006 : Integer;
          SMTPMail@1005 : Codeunit 400;
          MyBuffer@1003 : TEMPORARY Record 99008535;
          MyBuffer3@1001 : TEMPORARY Record 99008535;
    
        PROCEDURE ResetVars@10();
        BEGIN
          CLEAR(SMTPMail);
          MyBuffer.RESET;
          MyBuffer.DELETEALL;
          MyBuffer3.RESET;
          MyBuffer3.DELETEALL;
        END;
    
        PROCEDURE Test1_step1@1();
        VAR
          MyFile@1002 : File;
          MyFileStream@1001 : InStream;
          AttachmentStream@1000 : OutStream;
        BEGIN
          // runner
          MyFile.OPEN(MySecondFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer.INSERT;
          MyFile.CLOSE;
        END;
    
        PROCEDURE Test1_step2@2();
        VAR
          MyBuffer2@1000 : TEMPORARY Record 99008535;
          MyFileStream2@1001 : InStream;
        BEGIN
          // smtp mail
          SMTPMail.CreateMessage(MyFromName,MyFromAddress,MyToAddress,STRSUBSTNO(MySubject,MyTestNo),MyBody,TRUE);
          SMTPMail.AddAttachment(MyFirstFile);
    
          // getattachments
          MyBuffer.FINDFIRST;
          MyBuffer.CALCFIELDS(Blob);
          MyBuffer2.TRANSFERFIELDS(MyBuffer);
          MyBuffer2.Blob := MyBuffer.Blob;
          MyBuffer2.INSERT;
          MyBuffer2.FINDFIRST;
          MyBuffer2.CALCFIELDS(Blob);
          MyBuffer2.Blob.CREATEINSTREAM(MyFileStream2);
          SMTPMail.AddAttachmentStream(MyFileStream2,MySecondFileName);
        END;
    
        PROCEDURE Test1_step3@3();
        BEGIN
          SMTPMail.TrySend;
        END;
    
        PROCEDURE Test2@4();
        VAR
          MyFile@1002 : File;
          MyFileStream@1001 : InStream;
          AttachmentStream@1000 : OutStream;
          MyBuffer2@1003 : TEMPORARY Record 99008535;
          MyFileStream2@1004 : InStream;
        BEGIN
          // runner
          MyFile.OPEN(MySecondFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer.INSERT;
          MyFile.CLOSE;
    
          // smtp mail
          SMTPMail.CreateMessage(MyFromName,MyFromAddress,MyToAddress,STRSUBSTNO(MySubject,MyTestNo),MyBody,TRUE);
          SMTPMail.AddAttachment(MyFirstFile);
    
          // getattachments
          MyBuffer.FINDFIRST;
          MyBuffer.CALCFIELDS(Blob);
          MyBuffer2.TRANSFERFIELDS(MyBuffer);
          MyBuffer2.Blob := MyBuffer.Blob;
          MyBuffer2.INSERT;
          MyBuffer2.FINDFIRST;
          MyBuffer2.CALCFIELDS(Blob);
          MyBuffer2.Blob.CREATEINSTREAM(MyFileStream2);
          SMTPMail.AddAttachmentStream(MyFileStream2,MySecondFileName);
          SMTPMail.TrySend;
        END;
    
        PROCEDURE Test3_step1@7();
        VAR
          MyFile@1002 : File;
          MyFileStream@1001 : InStream;
          AttachmentStream@1000 : OutStream;
        BEGIN
          // runner
          MyFile.OPEN(MySecondFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer.INSERT;
          MyFile.CLOSE;
        END;
    
        PROCEDURE Test3_step2@6();
        VAR
          MyBuffer2@1001 : TEMPORARY Record 99008535;
          MyFileStream2@1000 : InStream;
        BEGIN
          // smtp mail
          SMTPMail.CreateMessage(MyFromName,MyFromAddress,MyToAddress,STRSUBSTNO(MySubject,MyTestNo),MyBody,TRUE);
          SMTPMail.AddAttachment(MyFirstFile);
    
          // getattachments
          MyBuffer.FINDFIRST;
          MyBuffer.CALCFIELDS(Blob);
          MyBuffer2.TRANSFERFIELDS(MyBuffer);
          MyBuffer2.Blob := MyBuffer.Blob;
          MyBuffer2.INSERT;
          MyBuffer2.FINDFIRST;
          MyBuffer2.CALCFIELDS(Blob);
          MyBuffer2.Blob.CREATEINSTREAM(MyFileStream2);
          SMTPMail.AddAttachmentStream(MyFileStream2,MySecondFileName);
          SMTPMail.TrySend;
        END;
    
        PROCEDURE Test4_step1@9();
        VAR
          MyFile@1002 : File;
          MyFileStream@1001 : InStream;
          AttachmentStream@1000 : OutStream;
        BEGIN
          // runner
          MyFile.OPEN(MySecondFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer.INSERT;
          MyFile.CLOSE;
        END;
    
        PROCEDURE Test4_step2@8();
        VAR
          MyFileStream3@1000 : InStream;
        BEGIN
          // smtp mail
          SMTPMail.CreateMessage(MyFromName,MyFromAddress,MyToAddress,STRSUBSTNO(MySubject,MyTestNo),MyBody,TRUE);
          SMTPMail.AddAttachment(MyFirstFile);
    
          // getattachments
          MyBuffer.FINDFIRST;
          MyBuffer.CALCFIELDS(Blob);
          MyBuffer3.TRANSFERFIELDS(MyBuffer);
          MyBuffer3.Blob := MyBuffer.Blob;
          MyBuffer3.INSERT;
          MyBuffer3.FINDFIRST;
          MyBuffer3.CALCFIELDS(Blob);
          MyBuffer3.Blob.CREATEINSTREAM(MyFileStream3);
          SMTPMail.AddAttachmentStream(MyFileStream3,MySecondFileName);
        END;
    
        PROCEDURE Test4_step3@5();
        BEGIN
          SMTPMail.TrySend;
        END;
    
        PROCEDURE Test5_step1@16();
        VAR
          MyFile@1002 : File;
          MyFileStream@1001 : InStream;
          AttachmentStream@1000 : OutStream;
        BEGIN
          // runner
          MyFile.OPEN(MyFirstFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer."Primay Key" := 1;
          MyBuffer.INSERT;
          MyFile.CLOSE;
    
          MyFile.OPEN(MySecondFile);
          MyFile.CREATEINSTREAM(MyFileStream);
          MyBuffer.Blob.CREATEOUTSTREAM(AttachmentStream);
          COPYSTREAM(AttachmentStream,MyFileStream);
          MyBuffer."Primay Key" := 2;
          MyBuffer.INSERT;
          MyFile.CLOSE;
        END;
    
        PROCEDURE Test5_step2@15();
        VAR
          MyFileStream3@1000 : InStream;
        BEGIN
          // smtp mail
          SMTPMail.CreateMessage(MyFromName,MyFromAddress,MyToAddress,STRSUBSTNO(MySubject,MyTestNo),MyBody,TRUE);
    
          // getattachments
          IF MyBuffer.FINDSET THEN
            REPEAT
              MyBuffer.CALCFIELDS(Blob);
              MyBuffer3.TRANSFERFIELDS(MyBuffer);
              MyBuffer3.Blob := MyBuffer.Blob;
              MyBuffer3.INSERT;
            UNTIL MyBuffer.NEXT = 0;
          IF MyBuffer3.FINDFIRST THEN
            REPEAT
              MyBuffer3.CALCFIELDS(Blob);
              MyBuffer3.Blob.CREATEINSTREAM(MyFileStream3);
              IF MyBuffer3."Primay Key" = 1 THEN
                SMTPMail.AddAttachmentStream(MyFileStream3,MyFirstFileName)
               ELSE
                SMTPMail.AddAttachmentStream(MyFileStream3,MySecondFileName);
            UNTIL MyBuffer3.NEXT = 0;
        END;
    
        PROCEDURE Test5_step3@14();
        BEGIN
          SMTPMail.TrySend;
        END;
    
        BEGIN
        END.
      }
    }
    
    
    There are no bugs, only random undocumented features...
    ---
    My Blog: http://NAV-Magno.be
Sign In or Register to comment.