Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I am a self-taught C# student.

I wanted to send faxes in bulk through the FAXCOMEXLIB library and print the status of the faxes being sent, but there was a problem that could not be solved. Any help in fixing the code would be appreciated.

1. Get the fax number currently being sent
2. Handling of fax cancellation and retry in case of error

thank you


What I have tried:

//Winform code==================================================
public partial class FaxManager : Form
    {
        public FaxManager()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (dgvFaxNumber.Rows.Count > 0)
            {
                int cnt = GetAttachmentsCount();
                if (cnt == 0)
                {
                    MessageBox.Show("Retry");
                    return;
                }

                Libs.Tools.FaxSender fs = new Libs.Tools.FaxSender();
                for (int i = 0; i < dgvFaxNumber.Rows.Count - 1; i++)
                {
                    string fax_number = dgvFaxNumber.Rows[i].Cells["fax_number"].Value.ToString();

                    string[] file = new string[cnt];
                    int idx = 0;
                    foreach (AttachmentUnit con in pnAttachments.Controls)
                    {
                        file[idx] = con.file;
                        idx++;
                    }
                    

                    fs.SendFax(fax_number, file);
                }
            }
        }
}
//=================================================================



//Common class code================================================
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FAXCOMEXLib;

namespace Libs.Tools
{
    public class FaxSender
    {
        private static FaxServer faxServer;
        private static FaxDocument faxDoc;
        public FaxSender()
        {
            try
            {
                faxServer = new FaxServer();
                faxServer.Connect(Environment.MachineName);
                RegisterFaxServerEvents();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private void RegisterFaxServerEvents()
        {
            faxServer.OnOutgoingJobAdded +=
                    new IFaxServerNotify2_OnOutgoingJobAddedEventHandler(faxServer_OnOutgoingJobAdded);
            faxServer.OnOutgoingJobChanged +=
                    new IFaxServerNotify2_OnOutgoingJobChangedEventHandler(faxServer_OnOutgoingJobChanged);
            faxServer.OnOutgoingJobRemoved +=
                    new IFaxServerNotify2_OnOutgoingJobRemovedEventHandler(faxServer_OnOutgoingJobRemoved);

            var eventsToListen =
                      FAX_SERVER_EVENTS_TYPE_ENUM.fsetFXSSVC_ENDED | FAX_SERVER_EVENTS_TYPE_ENUM.fsetOUT_QUEUE
                    | FAX_SERVER_EVENTS_TYPE_ENUM.fsetOUT_ARCHIVE | FAX_SERVER_EVENTS_TYPE_ENUM.fsetQUEUE_STATE
                    | FAX_SERVER_EVENTS_TYPE_ENUM.fsetACTIVITY | FAX_SERVER_EVENTS_TYPE_ENUM.fsetDEVICE_STATUS;

            faxServer.ListenToServerEvents(eventsToListen);
        }

        #region Event Handlers/Listeners
        private static void faxServer_OnOutgoingJobAdded(FaxServer faxServer, string bstrJobId)
        {
            Console.WriteLine("OnOutgoingJobAdded event fired. A fax is added to the outgoing queue.");
        }

        private static void faxServer_OnOutgoingJobChanged(FaxServer faxServer, string bstrJobId, FaxJobStatus JobStatus)
        {
            Console.WriteLine("OnOutgoingJobChanged event fired. A fax is changed to the outgoing queue.");
            faxServer.Folders.OutgoingQueue.Refresh();
            PrintFaxStatus(JobStatus);
        }

        private static void faxServer_OnOutgoingJobRemoved(FaxServer faxServer, string bstrJobId)
        {
            Console.WriteLine("OnOutgoingJobRemoved event fired. Fax job is removed to outbound queue.");
        }
        #endregion

        private static void PrintFaxStatus(FaxJobStatus faxJobStatus)
        {
            if (faxJobStatus.ExtendedStatusCode == FAX_JOB_EXTENDED_STATUS_ENUM.fjesDIALING)
            {
                Console.WriteLine("Dialing...");
            }

            if (faxJobStatus.ExtendedStatusCode == FAX_JOB_EXTENDED_STATUS_ENUM.fjesTRANSMITTING)
            {
                Console.WriteLine("Sending Fax...");
            }

            if (faxJobStatus.Status == FAX_JOB_STATUS_ENUM.fjsCOMPLETED
                && faxJobStatus.ExtendedStatusCode == FAX_JOB_EXTENDED_STATUS_ENUM.fjesCALL_COMPLETED)
            {
                Console.WriteLine("Fax is sent successfully.");
            }
        }

        public void SendFax(string faxNumber, string[] files_path)
        {
            try
            {
                FaxDocumentSetup(faxNumber, files_path);

                //One file
                if (files_path.Length == 1)
                {
                    object submitReturnValue = faxDoc.Submit(faxServer.ServerName);
                }
                //Multi files
                else
                {
                    object submitReturnValue = faxDoc.Submit2(faxServer.ServerName, out submitReturnValue);
                }
                faxDoc = null;
            }
            catch (System.Runtime.InteropServices.COMException comException)
            {
                Console.WriteLine("Error connecting to fax server. Error Message: " + comException.Message);
                Console.WriteLine("StackTrace: " + comException.StackTrace);
            }
        }
        string[] strJobIds;
        private void FaxDocumentSetup(string faxNumber, string[] files_path)
        {
            //One file
            if (files_path.Length == 1)
            {
                faxDoc = new FaxDocument();
                faxDoc.Priority = FAX_PRIORITY_TYPE_ENUM.fptHIGH;
                faxDoc.ReceiptType = FAX_RECEIPT_TYPE_ENUM.frtNONE;
                faxDoc.AttachFaxToReceipt = true;

                faxDoc.Sender.Name = "Manager";
                faxDoc.Sender.Company = "Ato Trading";
                faxDoc.Body = files_path[0];
                faxDoc.Subject = "Ato Handling Product List";
                faxDoc.DocumentName = "";
                faxDoc.Recipients.Add(faxNumber, "Customer");
            }
            else
            {
                //Multi files
                faxDoc = new FaxDocument();
                faxDoc.Priority = FAX_PRIORITY_TYPE_ENUM.fptHIGH;
                faxDoc.ReceiptType = FAX_RECEIPT_TYPE_ENUM.frtNONE;
                faxDoc.AttachFaxToReceipt = true;

                string[] files = files_path;

                strJobIds = new string[files_path.Length];
                for (int i = 0; i < files_path.Length - 1; i++)
                {
                    strJobIds[i] = "fax" + i;
                }

                faxDoc.Bodies = files;

                faxDoc.Subject = "Ato Handling Product List";
                faxDoc.DocumentName = "";
                faxDoc.Recipients.Add(faxNumber, "Customer");
                
                /*faxDoc.CoverPageType = FAX_COVERPAGE_TYPE_ENUM.fcptSERVER;
                faxDoc.CoverPage = "generic";
                faxDoc.Note = "Here is the info you requested";    // '===Comments files*/
            }
        }
    }
}
//=================================================================
Posted
Comments
OriginalGriff 12-Aug-22 0:30am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

And that's ignoring the obvious "Who the heck still has a fax machine?" question ...

Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900