I create a functionality to send email. After it has been sent, I want to attach the attachment files that user sent with email back to record. And here is my code.
public static void AddToLog(Common _originatingRecord , System.Net.Mail.MailMessage _message = null) { System.Net.Mail.AttachmentCollection attachmentCollection; System.Net.Mail.Attachment attachment; System.Collections.IEnumerator enumerator; if(_message) { attachmentCollection = _message.Attachments; enumerator = attachmentCollection.GetEnumerator(); while(enumerator.MoveNext()) { attachment = enumerator.Current; DocumentManagement::attachFile( _originatingRecord.TableId , _originatingRecord.RecId , _originatingRecord.DataAreaId , "File" , attachment.ContentStream , attachment.Name , System.Web.MimeMapping::GetMimeMapping(attachment.Name) , System.IO.Path::GetFileNameWithoutExtension(attachment.Name)); } } }
DocuRef record is created without file content.
What's wrong in my code
That you're at the end of the stream was my suspicious that I wanted to confirm by the previous test. It's suprising that it worked there.
public static void AddToLog(Common _originatingRecord , System.Net.Mail.MailMessage _message = null) { System.Net.Mail.AttachmentCollection attachmentCollection; System.Net.Mail.Attachment attachment; System.Collections.IEnumerator enumerator; if(_message) { attachmentCollection = _message.Attachments; enumerator = attachmentCollection.GetEnumerator(); while(enumerator.MoveNext()) { using(System.IO.MemoryStream stream = attachment.ContentStream) using(System.IO.StreamWriter writer = new System.IO.StreamWriter(stream)) { writer.Flush(); stream.Position = 0; DocumentManagement::attachFileForReference( _originatingRecord.TableId , _originatingRecord.RecId , _originatingRecord.DataAreaId , "File" , stream , attachment.Name , System.Web.MimeMapping::GetMimeMapping(attachment.Name) , System.IO.Path::GetFileNameWithoutExtension(attachment.Name)); } } } }
I add some code and Now It's work!
Yes, I got the data
What if you use File::sendFileToUser() with attachment.ContentStream? Do you get the data?
André Arnaud de Cal...
291,996
Super User 2025 Season 1
Martin Dráb
230,853
Most Valuable Professional
nmaenpaa
101,156