Skip to main content

Notifications

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

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: Attach the attachments from email after it has been sent

    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.

  • Atiraj Profile Picture
    Atiraj 50 on at
    RE: Attach the attachments from email after it has been sent

    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!

  • Atiraj Profile Picture
    Atiraj 50 on at
    RE: Attach the attachments from email after it has been sent

    Yes, I got the data

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: Attach the attachments from email after it has been sent

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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,996 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,853 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans