Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send a fax for a pdf from a Windows Service using FAXCOMEXLib?

I've seen this question asked before, but I have not seen any definite answers, and definitely not any answers that solve my problem. I created a windows service to send faxes (semi-automatically) using the FAXCOMEXLib library. So far, my service has been successful at sending text files (.txt). But when I try to send pdf, jpg, or tif files, I get the "Operation failed" error. In SO, I've seen a lot of discussion about the permissions of the user that the service is running under. I've tried a lot of different options (Local Service, Local User, custom user with admin privileges, allow service to interact with desktop). But nothing seems to make a difference. It seems that the service does not have permissions to open the appropriate app to "print" the pdf, jpg, or tif file. But I am only speculating. Has anyone been successful in sending a fax through FAXCOMEXLib in a Windows service? Here is my code that sends the fax:

fileName = @"D:\temp\FaxTest.txt"; //THIS WORKS
//fileName = @"D:\temp\FaxTest.pdf"; //Operation failed
//fileName = @"D:\temp\FaxTest.tif"; //Operation failed
faxDoc.Sender.Name = faxRec.From;
faxDoc.Sender.Company = faxRec.From;
faxDoc.Body = fileName;
faxDoc.Subject = faxRec.ReferenceId;
faxDoc.DocumentName = faxRec.ReferenceId;
var to = "xxxxxxxxxx";
faxDoc.Recipients.Add(to, "Some Name");
var serverName = Environment.MachineName;
string[] returnVal = faxDoc.Submit(serverName);

In case you're wondering, yes, those files do exist on the server with those names, and they are valid files (I can open them up in Adobe Reader and Picture Viewer). And this also works just fine if I run this locally on my dev machine. And of course, the appropriate viewer pops up before sending (on my local machine). My guess is that for some reason the service cannot open the viewer. Has anyone been successful sending a PDF this way in a Windows service?

like image 727
Matt Spinks Avatar asked Sep 30 '17 20:09

Matt Spinks


1 Answers

It is documented pretty well in the MSDN article. The non-trivial thing that needs to happen is that some software needs to convert the file content to printable text that can be faxed. Quoting:

Examples of documents that you can send as a fax body are a text file (.txt), a Microsoft Word document (.doc), or a Microsoft Excel spreadsheet (.xls). When you send a fax from a client computer, the body has to be associated with an application that is installed on that computer, and the application has to support the PrintTo verb; otherwise, the fax will fail.

So one simple test you can do is right-click the file in Explorer and look for the "Print" command. Next drag the file to a printer to exercise the PrintTo verb. If these test fail then it isn't going to work and you need to install an app that knows how to print the file.

Doing this from a service puts extra requirements on the app that does the printing. There are a lot of them that don't behave particularly well in a service. Especially so when you try to print, Microsoft strongly recommends to never do that in a service. Office apps don't for example as of late, making the MSDN advice already weak sauce.

On my machine, the .tif extension is associated with a UWP app, that isn't going to work in a service either. Giving good advice is difficult given the large number of apps that handle these popular extensions, best to go to superuser.com and name the specific extension, Windows version and the app you prefer to use. Doing this from a user session is certainly the least troublesome.

like image 162
Hans Passant Avatar answered Oct 03 '22 22:10

Hans Passant