Email Attachment

fazlehasanfazlehasan Member Posts: 68
Our customer is using BC23.1

When I am trying to send an email with attachment (PDF), I am getting this error

"The Tenant Media does not exist. Identification fields and values: ID='{00000000-0000-0000-0000-000000000000}"


The error is generated in this line when I do the attachment:

EmailMsg.AddAttachment(FileNameForEmail, 'application/pdf', iStr);

where, iStrm is a variable of Type InStream;

I checked iStr has value,
and its generated from a BC Report,


oStrm := tempBlobcu.CreateOutStream();
if CustomerStatement.SaveAs('', ReportFormat::Pdf, oStrm) then begin
iStrm := tempBlobcu.CreateInStream();


Any suggestion would be appreciated.

Thanks.

Answers

  • markborgesmarkborges Member Posts: 170
    Hi, there's another thread that might help you. It has an accepted solution in it. It seems that the method utilized in that answer is to actually take the Blob InStream and convert it to Base64, and then use the SendMessage signature with the Base64 parameter.

    https://forum.mibuso.com/discussion/76181/save-report-as-pdf-and-send-as-e-mail

    Good luck!
    Marcelo Borges
    D365 Business Central Solutions Architect
    BC AL/NAV C/AL Developer
    BC Repositories.com
  • fazlehasanfazlehasan Member Posts: 68
    @markborges Thanks for your response!

    I tried the solution, I am now getting a different error.
    This time the error is:

    Microsoft.Dynamics.Nav.Runtime.NavInStream variable not initialized.

    This error occurred when I tried to convert the iStream.

    txtB64 := converter_.ToBase64(iStr, true);
    EmailMsg.AddAttachment(FileNameForEmail, 'application/pdf', txtB64);
  • markborgesmarkborges Member Posts: 170
    Hi @fazlehasan , can you share a bit more of your code? I need to see what type of initialization you have done prior to the faulty line.
    Marcelo Borges
    D365 Business Central Solutions Architect
    BC AL/NAV C/AL Developer
    BC Repositories.com
  • fazlehasanfazlehasan Member Posts: 68
    Thanks, I have looked back and found that I used a wrong variable for Initialization.

    oStrm := tempBlobcu.CreateOutStream();
    if CustomerStatement.SaveAs('', ReportFormat::Pdf, oStrm) then begin
    iStrm := tempBlobcu.CreateInStream();


    After correcting this, now I am back to this error:

    The Tenant Media does not exist. Identification fields and values: ID='{00000000-0000-0000-0000-000000000000}'

    Is there any extension that Business Central Require to do attachments or for handling Media? or is there any specific configuration related to email attachments?

    Thanks.
  • fazlehasanfazlehasan Member Posts: 68
    I found out my mistake,
    I was doing this before creating the Email.

    oStrm := tempBlobcu.CreateOutStream();
    if CustomerStatement.SaveAs('', ReportFormat::Pdf, oStrm) then begin
    iStrm := tempBlobcu.CreateInStream();
    gtFileName := FileMgt.InstreamExportToServerFile(iStrm, 'PDF');


    and that initialized the value of iStrm (I guess).
    After Commenting this line, the attachment started working.
Sign In or Register to comment.