Send email with attached putting in Dynamics Ax queue By code X++

Create the static method in the class, running on server

server static void insertMailToQueue(
    SysEmailId      _emailId,
    str             _emailAddr,
    str             _custName,
    str             _quotationId,
    container       attachments = conNull(),
    Map             _mappings   = null,
    LanguageId      _language = 'pt-br'
    )
{
    // Adapted from SysEmailTable::sendMail

    #SysMailer
    LanguageId              languageId;

    SysEmailTable           table = SysEmailTable::find(_emailId);
    SysEmailMessageTable    message;

    str                     messageBody;
    container               data;
    container               embeddedBinaryData;

    Filename                filePath;
    Filename                filename;
    Filename                fileExtension;

    List                    list;
    ListEnumerator          enumerator;
    Filename                htmlDecodedFileName;

    int                     i = 1;
    int                     maxAttachmentSize;

    BinData                 binData;

    SysOutgoingEmailTable   outgoingEmailTable;
    SysOutgoingEmailData    outgoingEmailData;

    SysEmailBatch           batch = SysEmailBatch::construct();
    FileIOPermission        fileIOPermission;

    SysEmailParameters      emailParams = SysEmailParameters::find();
    str                     attachFilename;
    str                     attachAttachmentName;
    int                     attachmentIDx;

    ;

    maxAttachmentSize = emailParams.MaxEmailAttachmentSize;

    //maxAttachmentSize in megabytes
    if (maxAttachmentSize < 1)
        maxAttachmentSize = #maxAttachmentSizeDefault;

    if (_language)
        languageId = _language;
    else
        languageId = table.DefaultLanguage;

    message = SysEmailMessageTable::find(_emailId, languageId);

    if (!message)
        message = SysEmailMessageTable::find(_emailId, table.DefaultLanguage);

    if (!message)
        throw error(strFmt("@SYS74260", _language));

    if (message.LayoutType == SysEmailLayoutType::StaticLayout)
        messageBody = message.Mail;
    else
        throw error(strFmt("Layout type '%1' in email %2 is not supported by this function",
            message.LayoutType,
            message.EmailId));

    messageBody = SysLabel::resolveLabels(messageBody, languageId);

    ttsbegin;

    outgoingEmailTable.EmailItemId      = EventInbox::nextEventId();
    outgoingEmailTable.TemplateId       = table.EmailId;
    outgoingEmailTable.IsSystemEmail    = NoYes::No;
    outgoingEmailTable.Sender           = table.SenderAddr;
    outgoingEmailTable.SenderName       = table.SenderName;
    outgoingEmailTable.Recipient        = _emailAddr;

    //note: first do the xml transform if needed, params are substitued after that
    messageBody = SysEmailMessage::stringExpand(messageBody, SysEmailTable::htmlEncodeParameters(_mappings));
    messageBody = strReplace(messageBody, '\n', '<br>');

    messageBody = strFmt(messageBody, _custName, _quotationId, "Company Name");

    [outgoingEmailTable.Message, data] = SysEmailTable::embedImages(messageBody);

    list        = List::create(data);
    enumerator  = list.getEnumerator();
    while (enumerator.moveNext())
    {
        htmlDecodedFileName = SysEmailTable::htmlDecode(enumerator.current());

        fileIOPermission = new FileIOPermission(htmlDecodedFileName,'r');
        fileIOPermission.assert();
        //BP Deviation Documented
        if (WinAPIServer::fileExists(htmlDecodedFileName) &&
            //BP Deviation Documented
            (WinAPIServer::fileSize( htmlDecodedFileName) < (maxAttachmentSize * 1000000)) &&
            SysEmailTable::isFromAttachmentsFolder(htmlDecodedFileName))
        {
            binData = new BinData();
            //BP Deviation Documented
            binData.loadFile(htmlDecodedFileName);
            embeddedBinaryData = binData.getData();

            outgoingEmailData.EmailItemId       = outgoingEmailTable.EmailItemId;
            outgoingEmailData.DataId            = i;
            outgoingEmailData.EmailDataType     = SysEmailDataType::Embedded;
            outgoingEmailData.Data              = embeddedBinaryData;
            [filePath, filename, fileExtension] = Global::fileNameSplit(htmlDecodedFileName);

            outgoingEmailData.FileName          = int642str(outgoingEmailTable.EmailItemId) + '_' + int2str(i);
            outgoingEmailData.FileExtension     = fileExtension;

            outgoingEmailData.insert();

            i++;
        }

        CodeAccessPermission::revertAssert();
    }

    outgoingEmailTable.Subject = SysEmailMessage::stringExpand(message.Subject, _mappings);

    outgoingEmailTable.Priority         = table.Priority;
    outgoingEmailTable.WithRetries      = false;
    outgoingEmailTable.RetryNum         = 0;
    outgoingEmailTable.UserId           = curUserId();
    outgoingEmailTable.Status           = SysEmailStatus::Executing;

    for(attachmentIDx = 1;attachmentIDx <= conLen(attachments);attachmentIDx += 2)
    {
        attachFilename          = conPeek(attachments,attachmentIDx);
        attachAttachmentName    = conPeek(attachments,attachmentIDx + 1);

        fileIOPermission = new FileIOPermission(attachFilename,'r');
        fileIOPermission.assert();

        if (attachFilename && WinAPIServer::fileExists(attachFilename))
        {
            if (WinAPIServer::fileSize(attachFilename) < (maxAttachmentSize * 1000000))
            {
                binData = new BinData();
                binData.loadFile(attachFilename);
                embeddedBinaryData = binData.getData();

                outgoingEmailData.EmailItemId       = outgoingEmailTable.EmailItemId;
                outgoingEmailData.DataId            = i;
                outgoingEmailData.EmailDataType     = SysEmailDataType::Attachment;
                outgoingEmailData.Data              = embeddedBinaryData;
                [filePath, filename, fileExtension] = Global::fileNameSplit(attachFilename);
                outgoingEmailData.FileName          = filename;
                outgoingEmailData.FileExtension     = fileExtension;

                outgoingEmailData.insert();
            }
        }
    }

    CodeAccessPermission::revertAssert();

    outgoingEmailTable.LatestStatusChangeDateTime = DateTimeUtil::getSystemDateTime();
    outgoingEmailTable.insert();

    ttscommit;
}

Job with the call

static void sendMailTest2(Args _args)
{

    str To;
    str attach = @"\\192.168.1.11\publico\TI\AX\Quotation\1.xlsx";
    CLRObject ex;
    ;

    To = "diegoaraujodv@gmail.com";

    try
    {
        SalesQuotationSendEmail::insertMailToQueue('Comercial', //email ID from sysEmailTable
                                                   To,          //email from Receiver
                                                   '',          //CustName
                                                   '',          //QuotationId
                                                   [attach]);   //Attach path
    }
    catch
    {
        //Code never executes past this point
        ex = CLRInterop::getLastException();

        while(ex != null) {
            error(ex.ToString());
            ex = ex.get_InnerException();
        }
    }
}


    Danzel
    Danzel管理员

    • 声明:本文由Danzel于2021-09-22转载(优化),转载须经原站同意并注明出处。
    • 本文地址:http://maryd.cn/?id=198
    上一篇:How to: Copy a sales order using SalesCopying class
    下一篇:Cerebro 变量名搜索插件-命名

    留言评论

    暂无留言
    取消
    扫码支持