AL code for send email with multiple recipient with attachment from sharing path

 To send an email with multiple recipients and attachments from a shared path using AL code in Microsoft Dynamics 365 Business Central, you can modify the previous example by using the `FileManagement` codeunit to copy the attachments from the shared path to a temporary folder before attaching them to the email. Here's an example:


```AL

PROCEDURE SendEmailWithMultipleRecipientsAndAttachmentsFromSharedPath@1()

VAR

  EmailMgt@1: Codeunit 400;

  EmailMessage@2: Record 441;

  Recipients@3: Record 442;

  Attachment@4: Record 443;

  FileManagement@5: Codeunit 419;

  TempFolderPath@6: Text[250];

  AttachmentPath@7: Text[250];

BEGIN

  EmailMessage.INIT;

  EmailMessage.Subject := 'Test Email with Attachments';

  EmailMessage.Body := 'This is a test email sent to multiple recipients with attachments.';

  

  Recipients.INIT;

  Recipients."E-mail" := 'recipient1@example.com';

  Recipients.Insert;

  Recipients."E-mail" := 'recipient2@example.com';

  Recipients.Insert;

  Recipients."E-mail" := 'recipient3@example.com';

  

  // Shared path of the attachments

  AttachmentPath := '\\server\share\attachments\';

  

  // Create a temporary folder to store the attachments

  TempFolderPath := FileManagement.GetTempPath;

  FileManagement.CreateDirectory(TempFolderPath);

  

  // Attachments

  Attachment.INIT;

  Attachment."File Name" := 'attachment1.pdf';

  Attachment."Display Name" := 'Attachment 1';

  

  // Copy attachment from shared path to temporary folder

  FileManagement.CopyFile(AttachmentPath + Attachment."File Name", TempFolderPath + Attachment."File Name");

  

  EmailMgt.AddAttachmentFromFile(EmailMessage, TempFolderPath + Attachment."File Name", Attachment."Display Name");

  

  Attachment.INIT;

  Attachment."File Name" := 'attachment2.docx';

  Attachment."Display Name" := 'Attachment 2';

  

  // Copy attachment from shared path to temporary folder

  FileManagement.CopyFile(AttachmentPath + Attachment."File Name", TempFolderPath + Attachment."File Name");

  

  EmailMgt.AddAttachmentFromFile(EmailMessage, TempFolderPath + Attachment."File Name", Attachment."Display Name");

  

  EmailMgt.Send(EmailMessage, Recipients);

  

  // Delete temporary folder and its contents

  FileManagement.DeleteDirectory(TempFolderPath, TRUE);

END;

```


In this example, we use the `FileManagement` codeunit to perform file-related operations. First, we set the shared path of the attachments in the `AttachmentPath` variable. Then, we create a temporary folder using `GetTempPath` method and `CreateDirectory` method of the `FileManagement` codeunit.


Next, we initialize the `Attachment` record and set the file name and display name for each attachment. We copy each attachment from the shared path to the temporary folder using the `CopyFile` method of the `FileManagement` codeunit.


After that, we use the `AddAttachmentFromFile` function of the `EmailMgt` codeunit to attach the files from the temporary folder to the email message.


Finally, we send the email using the `Send` function of the `EmailMgt` codeunit. After sending the email, we delete the temporary folder and its contents using the `DeleteDirectory` method of the `FileManagement` codeunit.


Remember to replace the email addresses with the actual email addresses of the recipients you want to send the email to, and update the shared path and file names of the attachments accordingly.


Ensure that the shared path is accessible from


 the server running Business Central and that the necessary permissions are set for accessing the shared folder.

No comments:

Post a Comment