SMTP Mail codeunit AddAttachmentStream
Magno
Member Posts: 168
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...
I have attachments in a blob field for which I create an instream, but all I get is an empty file in the email...
0
Answers
-
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.0 -
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. } }0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 328 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions