public class NightlyInvoiceCreditFaxWorkerProgram
{
private FAXCOMEXLib.FaxServer _faxServer = new FaxServerClass();
public FAXCOMEXLib.FaxServer faxSrv
{
get
{ return _faxServer; }
set
{ _faxServer = value; }
}
NightlyInvoiceCreditFaxWorkerProgram nws = new NightlyInvoiceCreditFaxWorkerProgram();
try
{
nws.faxSrv = new FaxServerClass();
nws.faxSrv.Connect("");
nws.faxSrv.ListenToServerEvents(FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM.fsetOUT_QUEUE);
nws.faxSrv.OnOutgoingJobAdded += new FAXCOMEXLib.IFaxServerNotify_OnOutgoingJobAddedEventHandler(faxSrv_OnOutgoingJobAdded);
nws.faxSrv.OnOutgoingJobChanged += new FAXCOMEXLib.IFaxServerNotify_OnOutgoingJobChangedEventHandler(faxSrv_OnOutgoingJobChanged);
nws.faxSrv.OnOutgoingJobRemoved += new FAXCOMEXLib.IFaxServerNotify_OnOutgoingJobRemovedEventHandler(faxSrv_OnOutgoingJobRemoved);
try {
FaxDocument faxDoc = new FaxDocumentClass();
faxDoc.Priority = FAX_PRIORITY_TYPE_ENUM.fptHIGH;
faxDoc.ReceiptType = FAX_RECEIPT_TYPE_ENUM.frtNONE;
//THIS CODE IS TO MAKE SURE THE FILE IS NOT IN USE WHEN WE TRY TO ATTACH AND THEN SEND
ReadFile:
FileStream fs = null;
try
{
fs = File.OpenRead(attachmentPath);
}
catch (IOException)
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "Waiting for file to clear", EventLogEntryType.Information, 3);
goto ReadFile;
}
finally
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "File finally clear", EventLogEntryType.Information, 3);
if (fs != null)
{
fs.Close();
fs.Dispose();
fs = null;
}
}
faxDoc.Body = attachmentPath;
faxDoc.Subject = thisItem.InvoiceNumber;
faxDoc.DocumentName = thisItem.RecipientName + " - Invoice: " + thisItem.InvoiceNumber;
faxDoc.Recipients.Add(thisItem.FaxNumber, thisItem.RecipientName);
object garb = faxDoc.Submit(faxSrv.ServerName);
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "Successful Fax Send!!", EventLogEntryType.Information, 10);
returnString = "Send Success for Item ID: " + thisItem.ID.ToString() + " for: " + thisItem.RecipientName + " " + thisItem.InvoiceNumber + " ";
faxDoc = null;
}catch (System.Runtime.InteropServices.COMException ce)
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "Error connecting to fax server. Error Message: " + ce.Message + " " + ce.StackTrace, EventLogEntryType.Information, 5);
}
private static void faxSrv_OnOutgoingJobRemoved(FAXCOMEXLib.FaxServer pFaxServer, string bstrJobId)
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "Job Removed to outbound queue.", EventLogEntryType.Information, 4);
}
private static void faxSrv_OnOutgoingJobChanged(FAXCOMEXLib.FaxServer pFaxServer, string bstrJobId, FAXCOMEXLib.FaxJobStatus pJobStatus)
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "There was a fax changed to the outgoing queue. WAITING UNTIL FINISHED SENDING", EventLogEntryType.Information, 4);
while (pJobStatus.Status != FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCOMPLETED)
{
//loop until this job is completed, then go back to the program
}
}
private static void faxSrv_OnOutgoingJobAdded(FAXCOMEXLib.FaxServer pFaxServer, string bstrJobId)
{
InvoiceCreditFaxServiceExVersion.WriteEventToWindowsLog("InvoiceCreditFaxAndEmailServiceExVersion", "There was a fax added to the outgoing queue.", EventLogEntryType.Information, 4);
}
using FAXCOMEXLib;
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Author
Commented:As far as I know, this code would work in a Windows Application as well. In a Windows Application, errors and stuff would be much more visible as well, with no need to write events out to the Windows Log. If anyone needs any more specific information on this, feel free to contact me via EE.