Sending Fax using c# and .NET

Developing WinForms application with VS 2005 to programmatically
fax a document.

You could use fax service API to send fax in your application. Here is the
starting point for the Fax APIs:

 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxportal_9nol.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxabout_5s8n.asp

First, there are no real samples in MSDN showing how to using the Fax
interfaces from a Managed language.  The Fax documentation existed before
C# or VB.Net existed. However, using Fax from C# or VB.Net is very similar
to doing it from VB.  You need to add a reference to the FaxComEx object
within the Visual Studio IDE and then reference the FAXCOMEXLib namespace.

Next, there are several programmatic interfaces to the Fax Service.  There
is a “C” style API and two different COM interfaces. I strongly recommend
that you use FaxComEx, which is the most recent and by far the most robust
version.  However, this is only supported on Windows XP and later.  If you
must have support for Windows 2000, then you should investigate the FaxCom
interfaces.  Here are some links that discuss the different options.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxabout_18s3.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxabout_699l.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxlegacy_9lf4.asp

The following link provide a VB code example of sending a fax.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxusing_27co.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fax/fax/faxusing_8dyw.asp

I think it might help more to introduce the following link to you, which
discusses “COM Interop from VB.NET”.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconCOMInteroperability.asp

I also found some VB.NET and C# code samples as follows.

http://www.pcreview.co.uk/forums/thread-1477361.php
http://www.thescripts.com/forum/thread273119.html

Sample Code :

using FAXCOMLib;
using FAXCOMEXLib;

FaxServerClass fs = new FaxServerClass();
fs.Connect(“<your_computer_name>”); //specifies the machinename
object obj = fs.CreateDocument(“<your_filename>”);
FaxDoc fd = (FaxDoc)obj;
fd.FaxNumber = “<your_fax_number_to_send_to”;
fd.RecipientName = “<your_recipients_name”;
int i = fd.Send();
MessageBox.Show(i.ToString());
fs.Disconnect();

 

Windows fax service will install without a fax modem installed in the computer.

But the above code will Fail with a “Unknown Interop Error” if you have no modem installed. The only way I could get it to work is by installing a modem.

About these ads

One thought on “Sending Fax using c# and .NET

  1. Hi,
    I have tried the above code. But in compile time I am geeting same errors in several places. In my system I am having a simple USB modem.
    Please help.

    The error are:
    Error 2 Interop type ‘FAXCOMEXLib.FaxRecipientsClass’ cannot be embedded. Use the applicable interface instead.

    The Code is:
    private int FaxDocument(String TheFile, string faxnumber)
    {
    int JobID=0;
    FAXCOMEXLib.FaxRecipients frs = new FAXCOMEXLib.FaxRecipientsClass();
    FAXCOMEXLib.FaxDocument faxdoc = new FAXCOMEXLib.FaxDocumentClass();
    FAXCOMEXLib.FaxServer faxsrv = new FAXCOMEXLib.FaxServerClass();
    try
    {
    faxsrv.Connect(Environment.MachineName);
    faxdoc.Body = TheFile;
    faxdoc.DocumentName = “Fax Transmission”;
    faxdoc.ReceiptAddress = “bob@doe.com”;

    frs.Add(faxnumber, “John Doe”);
    faxdoc.Recipients.Add(faxnumber, “PH”);
    faxdoc.ConnectedSubmit(faxsrv);
    }
    catch
    {

    }
    finally
    {
    faxsrv.Disconnect();
    Marshal.ReleaseComObject(frs);
    Marshal.ReleaseComObject(faxdoc);
    Marshal.ReleaseComObject(faxsrv);
    }
    return JobID;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s