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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Send email with attachment using System.IO.Stream

(0) ShareShare
ReportReport
Posted on by 570

Hi All,

Environment D365Fo

Requirement : Export the data from  D365fo  in .TxT format and need to send the email with downloads file.

NOTE: I can able to download the file in .TXT format and I can able to sent the email with attachment  also.

PROBELM  : email sent with  attachment BUT WITH OUT DATA .

Below display my code, please check it and give some solution to send the attachment with data.

without use Azure blob storage, I need to send the attachment

        System.IO.Stream        stream;
        TextStreamIo            io;
        System.IO.StreamReader  reader;
        XML                     fileContent;
        JDRF_AWS_TmpTable JDRF_AWS_TmpTable;
        io = TextStreamIo::constructForWrite();


        /////////email process code
        System.IO.Stream workbookStream = new System.IO.MemoryStream();
        SysMailerSMTP   mailer = new SysMailerSMTP();
        SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
        SysEmailParameters parameters = SysEmailParameters::find();
              

      
        io.writeExp(['Description','MainAccount','BusinessUnit','Events','LegalEntity','TransDate','Actuals']);
        io.outFieldDelimiter(',');
        while select * from jdrf_AWS_TmpTable
        {
            io.writeExp(['"'+jdrf_AWS_TmpTable.Description+'"',jdrf_AWS_TmpTable.MainAccount,jdrf_AWS_TmpTable.BusinessUnit,jdrf_AWS_TmpTable.Event,jdrf_AWS_TmpTable.LegalEntity, jdrf_AWS_TmpTable.TransDate,
                jdrf_AWS_TmpTable.Actuls]);


        }

        stream = io.getStream();
        stream.Position = 0;
        reader = new System.IO.StreamReader(stream);
        filecontent = reader.ReadToEnd();
             
        File::SendStringAsFileToUser(fileContent, 'MyTextFile');       

        if (parameters.SMTPRelayServerName)
        {
            mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
                parameters.SMTPPortNumber,
                parameters.SMTPUserName,
                SysEmailParameters::password(),
                parameters.SMTPUseNTLM);
        }
        else
        {
            warning("SERVER NOT FOUND");
        }


        builder.setFrom(SysEmailParameters::find().SMTPUserName);
        builder.addTo("my email id");
        //builder.addCc(“CC address”);  
       
        builder.addAttachment(workbookStream,'MyTextFile.txt','application/vnd.openxmlformats.opendocument.text');
       
        builder.setSubject("Email subjectl");
        SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

  

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Please always use Insert > Insert Code (in the rich-formatting view) to paste source code. Let me do it for you this time:

    /////////email process code
    System.IO.Stream workbookStream = new System.IO.MemoryStream();
    SysMailerSMTP   mailer = new SysMailerSMTP();
    SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
    SysEmailParameters parameters = SysEmailParameters::find();
    
    TextStreamIo io = TextStreamIo::constructForWrite();
    io.writeExp(['Description','MainAccount','BusinessUnit','Events','LegalEntity','TransDate','Actuals']);
    io.outFieldDelimiter(',');
    
    JDRF_AWS_TmpTable jdrf_AWS_TmpTable;
    while select * from jdrf_AWS_TmpTable
    {
    	io.writeExp(['"' jdrf_AWS_TmpTable.Description '"',
    		jdrf_AWS_TmpTable.MainAccount,
    		jdrf_AWS_TmpTable.BusinessUnit,
    		jdrf_AWS_TmpTable.Event,
    		jdrf_AWS_TmpTable.LegalEntity,
    		jdrf_AWS_TmpTable.TransDate,
    		jdrf_AWS_TmpTable.Actuls]);
    }
    
    System.IO.Stream stream = io.getStream();
    stream.Position = 0;
    System.IO.StreamReader reader = new System.IO.StreamReader(stream);
    XML filecontent = reader.ReadToEnd();
    	 
    File::SendStringAsFileToUser(fileContent, 'MyTextFile');       
    
    if (parameters.SMTPRelayServerName)
    {
    	mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
    		parameters.SMTPPortNumber,
    		parameters.SMTPUserName,
    		SysEmailParameters::password(),
    		parameters.SMTPUseNTLM);
    }
    else
    {
    	warning("SERVER NOT FOUND");
    }
    
    builder.setFrom(SysEmailParameters::find().SMTPUserName);
    builder.addTo("my email id");
    
    builder.addAttachment(workbookStream, 'MyTextFile.txt', 'application/vnd.openxmlformats.opendocument.text');
    
    builder.setSubject("Email subjectl");
    SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

    It seems that you never write any data to the stream you use for the attachment, i.e. workbookStream.

  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    You are assigning values to 'stream' variable but sending the mail with 'workbookStream' variable.

  • Suggested answer
    mhdshb1 Profile Picture
    1,250 on at

    Hi 

    As mentioned by the experts, just replace the following line 

    builder.addAttachment(workbookStream, 'MyTextFile.txt', 'application/vnd.openxmlformats.opendocument.text');
    

    with this

    builder.addAttachment(stream, 'MyTextFile.txt', 'application/vnd.openxmlformats.opendocument.text');
    

    Since workbookstream is empty. 

    Regards, 

    M

  • TonyAx Profile Picture
    570 on at

    Hi Mohammad

    Thanks for reply, I replace the code but still am getting with out data.

    builder.addAttachment(stream,'MyTextFile.txt','application/vnd.openxmlformats.opendocument.text');

  • Verified answer
    mhdshb1 Profile Picture
    1,250 on at

    Hi,

    Yeah your are right, i tried it and it was sending me empty attachment. 

    So i resolved it by changing

     

    stream.Position = 0;

    with

     

    stream.Seek(0, System.IO.SeekOrigin::Begin);

    Plus i do not know why you are trying to read the stream and download it.

    So the code is 

    /////////email process code
    System.IO.Stream workbookStream = new System.IO.MemoryStream();
    SysMailerSMTP   mailer = new SysMailerSMTP();
    SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
    SysEmailParameters parameters = SysEmailParameters::find();
    
    TextStreamIo io = TextStreamIo::constructForWrite();
    io.writeExp(['Description','MainAccount','BusinessUnit','Events','LegalEntity','TransDate','Actuals']);
    io.outFieldDelimiter(',');
    
    JDRF_AWS_TmpTable jdrf_AWS_TmpTable;
    while select * from jdrf_AWS_TmpTable
    {
    	io.writeExp(['"' jdrf_AWS_TmpTable.Description '"',
    		jdrf_AWS_TmpTable.MainAccount,
    		jdrf_AWS_TmpTable.BusinessUnit,
    		jdrf_AWS_TmpTable.Event,
    		jdrf_AWS_TmpTable.LegalEntity,
    		jdrf_AWS_TmpTable.TransDate,
    		jdrf_AWS_TmpTable.Actuls]);
    }
    
    System.IO.Stream stream = io.getStream();
    stream.Seek(0, System.IO.SeekOrigin::Begin);
    
    if (parameters.SMTPRelayServerName)
    {
        mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
        parameters.SMTPPortNumber,
        parameters.SMTPUserName,
        SysEmailParameters::password(),
        parameters.SMTPUseNTLM);
    }
    else
    {
        warning("SERVER NOT FOUND");
    }
    
    builder.setFrom(SysEmailParameters::find().SMTPUserName);
    builder.addTo("my email id");
    
    builder.addAttachment(stream, 'MyTextFile.txt', 'application/vnd.openxmlformats.opendocument.text');
    
    builder.setSubject("Email subjectl");
    SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

    Regards,

    M

  • TonyAx Profile Picture
    570 on at

    Thank you Mohammad, its working .

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 540 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans