public Boolean Fax(Faxing.FaxServer faxServer)
        {
            if (faxServer == null)
            {
                return(false);
            }

            Boolean sentSuccess = true;


            // SET AUTOMATION INFORMATION

            automationId = Guid.NewGuid();

            automationStatus = Automation.Enumerations.AutomationStatus.Open;

            automationDate = DateTime.Now;

            automationException = String.Empty;



            try {
                // SET UP FAX DOCUMENT

                Public.Faxing.IFaxServer faxServerObject;

                String faxAttention = ((!String.IsNullOrWhiteSpace(attention)) ? attention : application.EntityGet(entityId).Name);

                Public.Faxing.FaxSender faxSender = new Public.Faxing.FaxSender(application.Session.UserDisplayName, String.Empty, faxServer.SenderEmailAddress);

                faxSender.Email = faxSender.Email.Replace("%useraccountname%", application.Session.UserAccountName);

                Public.Faxing.FaxRecipient faxRecipient = new Public.Faxing.FaxRecipient(faxAttention, this.ContactFaxNumber);

                Public.Faxing.FaxDocument faxDocument;


                Public.ImageStream attachment = application.EntityCorrespondenceImageGet(id);

                if ((attachment.Image != null) ? (attachment.Image.Length == 0) : true)
                {
                    attachment = Render();
                }

                faxDocument = new Public.Faxing.FaxDocument(automationId.ToString(), correspondenceName, attachment);


                System.Reflection.Assembly faxServerAssembly = System.Reflection.Assembly.LoadFrom(faxServer.AssemblyReference);

                Type faxServerType = faxServerAssembly.GetType(faxServer.AssemblyClassName);

                if (faxServerType == null)
                {
                    throw new ApplicationException("Unable to find Class [" + faxServer.AssemblyClassName + "] in referenced Assembly [" + faxServer.AssemblyReference + "].");
                }

                faxServerObject = (Public.Faxing.IFaxServer)Activator.CreateInstance(faxServerType);

                faxServerObject.OnFaxCompleted += new EventHandler <Public.Faxing.FaxCompletedEventArgs> (Fax_OnFaxCompleted);

                faxServerObject.Fax(faxServer.FaxServerConfiguration, faxSender, faxRecipient, faxDocument, faxServer.ExtendedProperties);
            }

            catch (Exception applicationException) {
                application.SetLastException(applicationException);

                AutomationStatus = Automation.Enumerations.AutomationStatus.Critical;

                AutomationException = applicationException.Message;

                sentSuccess = false;
            }

            if (sentSuccess)
            {
                sentSuccess = Save();
            }

            return(sentSuccess);
        }
        public void Fax(Public.Faxing.FaxServerConfiguration forFaxServerConfiguration, FaxSender forSender, FaxRecipient forRecipient, FaxDocument forDocument, Dictionary <String, String> extendedProperties)
        {
            faxServerConfiguration = forFaxServerConfiguration;

            sender = forSender;

            recipient = forRecipient;

            document = forDocument;


            String controlFileName = document.UniqueId + ".xml";

            String attachmentFileName = document.UniqueId + "." + document.Attachment.Extension;

            String statusFileName = controlFileName + ".status";


            System.Diagnostics.Debug.WriteLine("Source ID:" + document.UniqueId);


            // CREATE FAX CONTROL FILE


            System.Xml.XmlDocument controlDocument = new System.Xml.XmlDocument();

            System.Xml.XmlDeclaration xmlDeclaration = controlDocument.CreateXmlDeclaration("1.0", "utf-8", String.Empty);

            System.Xml.XmlElement controlElementNode = controlDocument.CreateElement("faxmakerdata");

            System.Xml.XmlElement fieldsElementNode = controlDocument.CreateElement("fields");

            System.Xml.XmlElement senderElementNode = controlDocument.CreateElement("sender");

            System.Xml.XmlElement recipientsElementNode = controlDocument.CreateElement("recipients");

            System.Xml.XmlElement recipientsFaxElementNode = controlDocument.CreateElement("fax");

            System.Xml.XmlElement recipientElementNode = controlDocument.CreateElement("recipient");


            #region Initialize Document Structure

            controlDocument.InsertBefore(xmlDeclaration, controlDocument.DocumentElement);

            controlDocument.AppendChild(controlElementNode);

            controlElementNode.AppendChild(fieldsElementNode);

            controlElementNode.AppendChild(senderElementNode);

            controlElementNode.AppendChild(recipientsElementNode);

            recipientsElementNode.AppendChild(recipientsFaxElementNode);

            recipientsFaxElementNode.AppendChild(recipientElementNode);


            // FAXMAKERDATA

            // +-- FIELDS

            // +-- SENDER

            // +-- RECIPIENTS

            // +-- +-- FAX

            // +-- +-- +-- RECIPIENT

            #endregion


            #region Sender Node

            if (!String.IsNullOrWhiteSpace(sender.Name))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "lastname", sender.Name);
            }

            if (!String.IsNullOrWhiteSpace(sender.Company))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "company", sender.Company);
            }

            if (!String.IsNullOrWhiteSpace(sender.Department))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "department", sender.Department);
            }

            if (!String.IsNullOrWhiteSpace(sender.FaxNumber))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "faxnumber", sender.FaxNumber);
            }

            if (!String.IsNullOrWhiteSpace(sender.VoiceNumber))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "voicenumber", sender.VoiceNumber);
            }

            // if (!String.IsNullOrWhiteSpace (sender.Email)) { Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode (controlDocument, senderElementNode, "emailaddress", sender.Email); }

            Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, senderElementNode, "emailaddress", sender.Email);

            #endregion


            #region Recipient Node

            String faxNumber = recipient.FaxNumber;

            faxNumber = (faxNumber.Length == 7) ? faxNumber.Substring(0, 3) + "-" + faxNumber.Substring(3, 4) : faxNumber;

            faxNumber = (faxNumber.Length == 10) ? "1-" + faxNumber.Substring(0, 3) + "-" + faxNumber.Substring(3, 3) + "-" + faxNumber.Substring(6, 4) : faxNumber;



            if (!String.IsNullOrWhiteSpace(recipient.RecipientName))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, recipientElementNode, "lastname", recipient.RecipientName);
            }

            if (!String.IsNullOrWhiteSpace(recipient.CompanyName))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, recipientElementNode, "company", recipient.CompanyName);
            }

            if (!String.IsNullOrWhiteSpace(recipient.DepartmentName))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, recipientElementNode, "department", recipient.DepartmentName);
            }

            Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, recipientElementNode, "faxnumber", faxNumber);

            if (!String.IsNullOrWhiteSpace(recipient.RecipientEmail))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, recipientElementNode, "emaladdress", recipient.RecipientEmail);
            }

            #endregion


            #region Fields Node

            if (!String.IsNullOrWhiteSpace(document.Subject))
            {
                Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, fieldsElementNode, "subject", document.Subject);
            }

            Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, fieldsElementNode, "resolution", "high");

            Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, fieldsElementNode, "attachment", attachmentFileName);

            Mercury.Server.Public.CommonFunctions.XmlDocumentAppendNode(controlDocument, fieldsElementNode, "uid", document.UniqueId);

            #endregion


            attachmentFileName = faxServerConfiguration.FaxUrl + @"\" + attachmentFileName;

            attachmentFileName = attachmentFileName.Replace(@"\\" + attachmentFileName, @"\" + attachmentFileName);


            System.IO.FileStream attachmentFile = new System.IO.FileStream(attachmentFileName, System.IO.FileMode.Create);

            document.Attachment.Image.Seek(0, System.IO.SeekOrigin.Begin);

            document.Attachment.Image.WriteTo(attachmentFile);

            attachmentFile.Flush();

            attachmentFile.Close();


            controlFileName = faxServerConfiguration.FaxUrl + @"\" + controlFileName;

            controlFileName = controlFileName.Replace(@"\\" + controlFileName, @"\" + controlFileName);

            controlDocument.Save(controlFileName);


            statusWatchStartTime = DateTime.Now;

            statusWatcher = new System.IO.FileSystemWatcher();

            statusWatcher.Filter = "*.status";

            statusWatcher.Created += new System.IO.FileSystemEventHandler(StatusWatcher_OnFileCreated);

            statusWatcher.Path = faxServerConfiguration.FaxUrl;

            statusWatcher.EnableRaisingEvents = true;


            return;
        }