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

Community site session details

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

Encrypt Mail Attachments

(0) ShareShare
ReportReport
Posted on by 384

Dears Experts,

i am trying to send an encrypted PDF File using PdfSharp.

so far i managed to send the pdf file to my email. but i want to send it as a secured Pdf file with a password that i insert in my code.

the email is being sent from my x++ code using the  SysMailerMessageBuilder and i am using the PdfSharp classes for the security settings.

my problem is once i want to attach my Pdfsharp.pdfdocument into SysMailerMessageBuilder.addAttachement( Pdfsharp.pdfdocument) , the type is not supported since mailer messager supports  System.IO.MemoryStream,

Kindly advise what to do in order to send the Attached email as encrypted file.

Regards,

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    236,353 Most Valuable Professional on at
    RE: Encrypt Mail Attachments

    You must respect types of parameters - if a method expects an apple, you can't give it a trolleybus. In your case, you need to save the document to either a stream or a file.

    Try something this:

    using (MemoryStream stream = new MemoryStream())
    { 
        pdfDocument.Save(stream, true);
        // Plus may need to reset the position
        stream.Position = 0;
    }

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Encrypt Mail Attachments

    i am already using stream, is it possible to rewrite to above code to this below?

    System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);
    
                    
                       stream.Position = 0;
    
                       str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
    
                       _stream = stream;
    
                       pdfPages = Document.get_Pages();
    
                       Document.Save(_stream, true)
                       ...
                       ...
                        mailer.addAttachment(_stream, strFmt('%1%2','AAA', '.PDF'));

  • Suggested answer
    Martin Dráb Profile Picture
    236,353 Most Valuable Professional on at
    RE: Encrypt Mail Attachments

    Why do you write the data to the Stream you created from reportBytes? You ignore the data from reportBytes, thereofore it's likely a bug.

    By the way, you populate fileContentType but then you ignore it.

    You probably want something like this:

    str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
    
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
    	document.Save(stream, true);
    	stream.Position = 0;
    	mailer.addAttachment(_stream, fileName, fileContentType);
    }

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Encrypt Mail Attachments

    i faced an error saying : "Cannot save a PDF document with no pages." while saving the document

    this error occurs when the number of pages in the pdf document is null !

     i tried to add this :pdfpages =  PdfDocument.get_Pages();

    with no luck !

  • WillWU Profile Picture
    22,361 on at
    RE: Encrypt Mail Attachments

    Hi sylvesterPowerBi,

    Do you have any content in your PDF file? 

    Try 

    PdfPage page = new PdfPage();
    document.AddPage(page);

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Encrypt Mail Attachments

    Hi Will

    will try your code.thanks

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Encrypt Mail Attachments

    Hi Will after using your code, i am now able to save the document, but i faced this error  Cannot access a closed Stream on mail.addattachment() method below

    any idea why ? here's my code

                    mailer.setSubject(SysEmailMessageSystemTable.Subject " " mthName(mthOfYr(PayrollPayStatement.PaymentDate)));
                    mailer.setFrom(SysEmailParameters::find().SMTPUserName);
                    mailer.setBody(SysEmailMessageSystemTable.Mail " " mthName(mthOfYr(PayrollPayStatement.PaymentDate)));
                    mailer.addTo(Email);
                     fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
                    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                    {
                        Document.AddPage(pdfPages);
                        Document.Save(stream, true);
                        // Plus may need to reset the position
                        stream.Position = 0;
    
                        mailer.addAttachment(stream,fileName,fileContentType);
                    }

  • Suggested answer
    Martin Dráb Profile Picture
    236,353 Most Valuable Professional on at
    RE: Encrypt Mail Attachments

    You'll need Document.Save(stream, false); in this case.

    By the way, can you please remove the unnecessary spaces from your code next time (e.g. the extra 16 spaces at line 2 before mailer)?

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Encrypt Mail Attachments

    Using the below code i am now able to send the file but it's not coming incrypted

    PdfSharp.Pdf.IO.PdfPasswordProvider pass;
    stream = new System.IO.MemoryStream();
    stream.Write(reportBytes,0,reportBytes.Length);
    fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);
    stream.Position = 0;
    Document =PdfSharp.Pdf.IO.PdfReader::Open(stream,password,PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import,pass);
    Document.AddPage(pdfPages);
    Document.Save(stream);
    mailer.addAttachment(stream,fileName,fileContentType);

    is there anything missed ?

  • Martin Dráb Profile Picture
    236,353 Most Valuable Professional on at
    RE: Encrypt Mail Attachments

    You add reportBytes to the stream and then you're trying to overwrite it with something else on Save(). It's strange and easily dangerous. For example, you write 1000 bytes and then you overwrite first 800 bytes. The result won't make any sense.

    Use two streams - one for the input and one for the output.

    Also, don't forget to close the streams. That's the point of 'using' blocks in my code.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,028

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 579 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans