web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Attach the attachments from email after it has been sent

(0) ShareShare
ReportReport
Posted on by 50

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

I have the same question (0)
  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    What if you use File::sendFileToUser() with attachment.ContentStream? Do you get the data?

  • Atiraj Profile Picture
    50 on at

    Yes, I got the data

  • Atiraj Profile Picture
    50 on at

    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!

  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 650 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 287 Super User 2026 Season 1

#3
Martin Dráb Profile Picture

Martin Dráb 225 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans