zoukankan      html  css  js  c++  java
  • How to fix to using SysEmailBatch class can't send attachment issue

    AOT\SysEmailBatch\Run
    // VAR Changed on 29 Mar 2012 at 14:29:31 by JXie7519
    public void run()
    {

        #SysMailer

        SysMailer                       mailer;
        SysEmailParameters              parameters      = SysEmailParameters::find();
        BinData                         binData;
        SysMailerAddressField tos;
        SysOutgoingEmailTable outgoingEmailTable;
        SysOutgoingEmailData outgoingEmailData;
        SysEmailItemId nextEmailItemId;

        FileIOPermission fileIOPermission;
        InteropPermission interopPermission;

        Filename htmlDecodedFileName;

        container embeddedBinaryData;



        str tmpPath;
        str filePath;
        str fileName;
        str fileExtension;

        str html;
        container data;
        List list;
        ListEnumerator enumerator;
        ListEnumerator attachmentFileNameListEnumerator;
        ListEnumerator attachmentDataListEnumerator;

        int i = 1;
        int  maxAttachmentSize;

        SysEmailParameters emailParams = SysEmailParameters::find();

        ;

        interopPermission = new InteropPermission(InteropKind::ComInterop);
        interopPermission.assert();
        mailer          = new SysMailer();
        CodeAccessPermission::revertAssert();

        maxAttachmentSize = emailParams.MaxEmailAttachmentSize;

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

        //BP Deviation Documented
        fileIOPermission = new FileIOPermission('','r');
        fileIOPermission.assert();

        //BP Deviation Documented
        tmpPath = WinAPIServer::getTempPath();

        CodeAccessPermission::revertAssert();

        mailer.bodyCharSet('UTF-8');

        if (isTraceAble)
            {
                ttsbegin;

                nextEmailItemId = EventInbox::nextEventId();

                outgoingEmailTable.EmailItemId = nextEmailItemId;

                outgoingEmailTable.IsSystemEmail = NoYes::No;

                outgoingEmailTable.Sender = senderAddr;
                outgoingEmailTable.SenderName = senderName;
                outgoingEmailTable.Recipient = emailAddr;

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

                messageBody = WebLet::weblets2Html4Help(messageBody, '');
                outgoingEmailTable.Message = 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)))
                        /*&& removed: VAR Changed on 29 Mar 2012 at 14:29:31 by JXie7519 Jimmy Xie
                         SysEmailTable::isFromAttachmentsFolder(htmlDecodedFileName))
    */
                    {

                        binData = new BinData();
                        //BP Deviation Documented
                        binData.loadFile(htmlDecodedFileName);
                        embeddedBinaryData = binData.getData();

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

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

                        outgoingEmailData.insert();

                        i++;

                    }
                    CodeAccessPermission::revertAssert();
                }

                outgoingEmailTable.Subject = subject;

                outgoingEmailTable.Priority = priority;
                outgoingEmailTable.WithRetries = isWithRetries;
                outgoingEmailTable.RetryNum = 0;
                outgoingEmailTable.UserId = '';
                outgoingEmailTable.Status = SysEmailStatus::Unsent;
                if (attachmentFileNameList)
                {
                    attachmentFileNameListEnumerator = attachmentFileNameList.getEnumerator();
                    if (attachmentDataList)
                    {
                        attachmentDataListEnumerator = attachmentDataList.getEnumerator();
                    }
                    else
                    {
                        attachmentDataListEnumerator = null;
                    }
                    while (attachmentFileNameListEnumerator.moveNext())
                    {
                        attachmentfileName = attachmentFileNameListEnumerator.current();
                        attachmentData = connull();
                        if (attachmentfileName)
                        {
                            //not in batch mode
                            if (!attachmentDataList)
                            {
                                fileIOPermission = new FileIOPermission(attachmentfileName,'r');
                                fileIOPermission.assert();

                                //BP Deviation Documented
                                if(WinAPIServer::fileExists(attachmentfileName) &&
                                //BP Deviation Documented
                                (WinAPIServer::fileSize(attachmentfileName) < (maxAttachmentSize * 1000000)))
                                /*&& removed: VAR Changed on 29 Mar 2012 at 14:29:31 by JXie7519 Jimmy Xie
                                 SysEmailTable::isFromAttachmentsFolder(attachmentfileName))
    */
                                {
                                    binData = new BinData();
                                    //BP Deviation Documented
                                    binData.loadFile(attachmentfileName);
                                    attachmentData = binData.getData();
                                }

                                CodeAccessPermission::revertAssert();
                            }
                            else if (attachmentDataListEnumerator && attachmentDataListEnumerator.moveNext())
                            {
                                attachmentData = attachmentDataListEnumerator.current();
                            }

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

                             outgoingEmailData.insert();


                        }

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

                ttscommit;

        }
        else //if traceable
        {
            mailer.fromAddress(this.parmSenderAddr(),this.parmSendername());
            mailer.subject(this.parmSubject());

            if (parameters.SMTPRelayServerName)
            {
                mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
                                   parameters.SMTPPortNumber,
                                   parameters.SMTPUserName,
                                   SysEmailParameters::password(),
                                   parameters.NTLM);
            }
            else
            {
                mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
                                   parameters.SMTPPortNumber,
                                   parameters.SMTPUserName,
                                   SysEmailParameters::password(),
                                   parameters.NTLM);
        }


        switch (priority)
        {
            case eMailPriority::High:
                mailer.priority(2);
                break;
            case eMailPriority::Low:
                mailer.priority(0);
                break;
            default:
                mailer.priority(1);
                break;
        }

        [html, data] = SysEmailBatch::embedImages(this.parmMessageBody());

        mailer.htmlBody(html);

        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)))
                /*&& removed: VAR Changed on 29 Mar 2012 at 14:29:31 by JXie7519 Jimmy Xie
                 SysEmailTable::isFromAttachmentsFolder(htmlDecodedFileName))
    */
            {

                mailer.addRelatedBodyPart(htmlDecodedFileName,int2str(i));
                i++;

            }

            CodeAccessPermission::revertAssert();
        }

        tos = mailer.tos();
        tos.appendAddress(this.parmEmailAddr());


        if (attachmentFileNameList)
        {
            if (attachmentDataList)
            {
                attachmentDataListEnumerator = attachmentDataList.getEnumerator();
            }
            else
            {
                attachmentDataListEnumerator = null;
            }
            attachmentFileNameListEnumerator = attachmentFileNameList.getEnumerator();
            while (attachmentFileNameListEnumerator.moveNext())
            {
                 attachmentData = connull();
                 attachmentfileName = attachmentFileNameListEnumerator.current();
                if (attachmentfileName)
                {
                    [filePath, filename, fileExtension] = Global::fileNameSplit(attachmentfileName);
                    if (attachmentDataListEnumerator && attachmentDataListEnumerator.moveNext())
                    {
                        attachmentData = attachmentDataListEnumerator.current();
                        if (attachmentData)
                        {
                            fileIOPermission = new FileIOPermission(tmpPath + fileName,'w');
                            fileIOPermission.assert();

                            binData = new BinData();
                            binData.setData(attachmentData);
                            //BP Deviation Documented
                            binData.saveFile(tmpPath + fileName);
                            CodeAccessPermission::revertAssert();

                            mailer.attachments().add(tmpPath + fileName);

                        }
                    }
                    else
                    {
                        //not running in batch mode
                        fileIOPermission = new FileIOPermission(attachmentfileName,'r');
                        fileIOPermission.assert();

                        //BP Deviation Documented
                        if( WinAPIServer::fileExists(attachmentfileName,true) &&
                          //BP Deviation Documented
                          (WinAPIServer::fileSize(attachmentfileName) < (maxAttachmentSize * 1000000)))
                          /*&& removed: VAR Changed on 29 Mar 2012 at 14:29:31 by JXie7519 Jimmy Xie
                          SysEmailTable::isFromAttachmentsFolder(attachmentfileName))
    */
                        {
                            mailer.attachments().add(attachmentfileName);
                        }

                        CodeAccessPermission::revertAssert();
                    }
                }
            }
        }
            mailer.sendMail();

        } // end if traceable
    }
  • 相关阅读:
    web service
    常用的正则表达式
    xml
    sql helper
    sql server 表连接
    asp.net页面生命周期
    创建简单的ajax对象
    checkbox选中问题
    ES6之扩展运算符 三个点(...)
    Object.assign()的用法 -- 用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,返回目标对象
  • 原文地址:https://www.cnblogs.com/Fandyx/p/2429811.html
Copyright © 2011-2022 走看看