For a more in depth and other programming language samples, download the Sample Client Install

C# Copy imageCopy
using FaxWSConsoleApplication.FaxWSReference;
using System;
using System.IO;
using System.Net;

namespace FaxWSConsoleApplication
{
    public static class SendFaxExClass
    {
        const string oauthUrl = "https://login.concord.net/v1/connect/token";
        const string faxWSUrl = "https://ws.concordfax.com/fax/v5";

        public static void SendFaxEx()
        {
            string username = "mbxXXXXXXXX";
            string password = "XXXX";
            WSError wsError = null;

            ///////////////////////////////////////////////////////////
            ///OAuth 2.0 Authentication Start, 
            ///Comment this block out if you are using username and password authentication
            // Get an access token from Concord Identity Server
            string grantType = "password";
            string scope = "FaxWS";
            string faxWSClientId = "A9F528F9-0000-4B70-0000-078C23030424-ConcordApiFaxWS";
            string faxWSClientSecret = "";

            AccessTokenRequest accessTokenRequest = null;
            AccessTokenResponse accessTokenResponse = null;
            TokenManager tokenManager = new TokenManager(oauthUrl);

            accessTokenRequest = new AccessTokenRequest(
                clientId: faxWSClientId,
                clientSecret: faxWSClientSecret,
                username: username,
                password: password,
                grantType: grantType,
                scope: scope,
                redirectUri: null,
                code: null,
                refreshToken: null,
                state: null);

            ///OAuth 2.0 Authentication End
            ///////////////////////////////////////////////////////////

            using(FaxWSAuthWrapper ws = new FaxWSAuthWrapper())
            {
                ws.Url = faxWSUrl;

                FaxJob jobDetails = new FaxJob();
                jobDetails.SenderName = "SenderName";
                jobDetails.SenderCSID = "SenderCSID";

                FaxJobRecipient[] recipients = new FaxJobRecipient[1];
                recipients[0] = new FaxJobRecipient();
                recipients[0].RecipName = "RecipientName";
                recipients[0].RecipFaxNumber = "12065550000"; // FaxNumber
                recipients[0].RecipCompany = string.Empty; // Value not required but needs to be populated
                recipients[0].RecipTitle = string.Empty; // Value not required but needs to be populated
                recipients[0].RecipSecureCSID = string.Empty; // Value not required but needs to be populated

                FaxJobFile[] faxFiles = new FaxJobFile[1];
                faxFiles[0] = new FaxJobFile();

                string faxFileName = "c:\\FaxImage.tif";

                FileStream inputFile = new FileStream(faxFileName, FileMode.Open, FileAccess.Read);
                faxFiles[0].FileData = new Byte[inputFile.Length];
                faxFiles[0].FileIndex = 1; // first file in the list
                faxFiles[0].FileTypeId = GetFileType(faxFileName);

                inputFile.Read(faxFiles[0].FileData, 0, (int)inputFile.Length); // Read file
                inputFile.Close();

                FaxJobId[] faxJobIdList = null;
                long ttfp = 0;

                try
                {
                    // Get an access token from Cache OR request one from the Identity Server
                    accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest);

                    if(accessTokenResponse == null)
                    {
                        Console.WriteLine("Failed to get an AccessToken, returned null");

                        return;
                    }

                    // Check to see if we failed to get an Access token via a Refresh token that has expired or been revoked, if so we need to re-authenticate
                    if(accessTokenResponse.HttpStatusCode == HttpStatusCode.BadRequest && accessTokenResponse.Error == "invalid_grant")
                    {
                        Console.WriteLine("Refresh token expired, attempting to get a new Access Token");
                        accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest, ignoreCache: true);
                    }

                    if(accessTokenResponse.HttpStatusCode != HttpStatusCode.OK)
                    {
                        Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription);

                        return;
                    }

                    // User Name and Password authentication
                    //if (ws.SendFaxEx(username, password, recipients, faxFiles, jobDetails, out faxJobIdList, out ttfp, out wsError))

                    // OAuth 2.0 Authentication requires that you pass in an access token as part of the Authorization: header 
                    // (No Password required)

                    ws.AccessToken = accessTokenResponse.AccessToken;

                    if(!ws.SendFaxEx(username, "", recipients, faxFiles, jobDetails, out faxJobIdList, out ttfp, out wsError))
                    {
                        Console.WriteLine("SendFaxEx Failed");

                        Console.WriteLine("Error Code: {0} Error Description: {1}", wsError.ErrorCode, wsError.ErrorString);

                        // Check to see if response indicates access token has expired, if so attempt to Refresh or Get New OAuth access token and try method call again
                        if(wsError.ErrorCode == -5059)
                        {
                            Console.WriteLine("AccessToken is not valid");

                            // Attempt to get a new access token or use a refresh token, if we fail we should stop
                            accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest);

                            if(accessTokenResponse == null)
                            {
                                Console.WriteLine("Failed to get an AccessToken, returned null");

                                return;
                            }

                            if(accessTokenResponse.HttpStatusCode != HttpStatusCode.OK)
                            {
                                Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription);

                                return;
                            }
                        }

                        return;
                    }

                    Console.WriteLine("Estimated Time To Free Port: " + ttfp);

                    Console.WriteLine("Looping over Job ID's");
                    foreach(FaxJobId faxJobId in faxJobIdList)
                    {
                        Console.WriteLine("JobId: " + faxJobId.JobId);
                    }

                }
                catch(Exception ex)
                {
                    Console.WriteLine("Error while calling SendFaxEx : " + ex.Message);
                }

            }
        }

        /// <summary>
        /// returns the file type based on file extension
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static int GetFileType(string filename)
        {
            if(string.IsNullOrEmpty(filename)) throw new ArgumentNullException("filename");

            string extension = Path.GetExtension(filename);

            if(string.IsNullOrEmpty(extension)) throw new ArgumentException("File does not contain an extension", "filename");

            switch(extension.ToLower())
            {
                case ".tif":
                case ".tiff":
                    return 1;
                case ".doc":
                case ".docx":
                    return 100;
                case ".pdf":
                    return 101;
                case ".rtf":
                    return 102;
                case ".xls":
                case ".xlsx":
                    return 103;
                case ".ppt":
                case ".pptx":
                case ".pps":
                case ".ppsx":
                    return 104;
                case ".txt":
                    return 105;
                case ".vsd":
                case ".vsdx":
                    return 106;
                case ".gif":
                    return 107;
                case ".jpg":
                case ".jpeg":
                    return 108;
                case ".htm":
                case ".html":
                case ".mht":
                case ".mhtml":
                    return 111;
                case ".png":
                    return 120;
                default:
                    throw new ArgumentException("Unrecognized file attachment type: " + extension, "filename");
            }

        }
    }
}
Visual Basic Copy imageCopy
Imports System.IO
Imports System.Net
Imports FaxWSVBConsoleApplication.FaxWSReference

Namespace FaxWSVBConsoleApplication
    Friend Class SendFaxExClass
        Public Shared Sub SendFaxEx()
            Dim username As String = "mbxXXXXXXXX"
            Dim password As String = "XXXX"
            Dim wsError As WSError = Nothing

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '' OAuth 2.0 Authentication Start, 
            '' Comment this block out if you are using username and password authentication
            ' Get an access token from Concord Identity Server

            Dim oauthUrl As String = "https://login.concord.net/v1/connect/token"
            Dim grantType As String = "password"
            Dim scope As String = "FaxWS"
            Dim faxWSClientId As String = "A9F528F9-0000-4B70-0000-078C23030424-ConcordApiFaxWS"
            Dim faxWSClientSecret As String = ""
            Dim accessTokenRequest As AccessTokenRequest = Nothing
            Dim accessTokenResponse As AccessTokenResponse = Nothing
            Dim tokenManager As TokenManager = New TokenManager(oauthUrl)
            accessTokenRequest = New AccessTokenRequest(clientId:=faxWSClientId, clientSecret:=faxWSClientSecret, username:=username, password:=password, grantType:=grantType, scope:=scope, redirectUri:=Nothing, code:=Nothing, refreshToken:=Nothing, state:=Nothing)


            '' OAuth 2.0 Authentication End
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Using ws As FaxWSAuthWrapper = New FaxWSAuthWrapper()
                Dim jobDetails As FaxJob = New FaxJob()
                jobDetails.SenderName = "SenderName"
                jobDetails.SenderCSID = "SenderCSID"
                Dim recipients As FaxJobRecipient() = New FaxJobRecipient(0) {}
                recipients(0) = New FaxJobRecipient()
                recipients(0).RecipName = "RecipientName"
                recipients(0).RecipFaxNumber = "12065550000" ' FaxNumber
                recipients(0).RecipCompany = String.Empty ' Value not required but needs to be populated
                recipients(0).RecipTitle = String.Empty ' Value not required but needs to be populated
                recipients(0).RecipSecureCSID = String.Empty ' Value not required but needs to be populated
                Dim faxFiles As FaxJobFile() = New FaxJobFile(0) {}
                faxFiles(0) = New FaxJobFile()
                Dim faxFileName As String = "c:\FaxImage.tif"
                Dim inputFile As FileStream = New FileStream(faxFileName, FileMode.Open, FileAccess.Read)
                faxFiles(0).FileData = New Byte(inputFile.Length - 1) {}
                faxFiles(0).FileIndex = 1 ' first file in the list
                faxFiles(0).FileTypeId = GetFileType(faxFileName)
                inputFile.Read(faxFiles(0).FileData, 0, inputFile.Length) ' Read file
                inputFile.Close()
                Dim faxJobIdList As FaxJobId() = Nothing
                Dim ttfp As Long = 0

                Try
                    ' Get an access token from Cache OR request one from the Identity Server
                    accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest)

                    If accessTokenResponse Is Nothing Then
                        Console.WriteLine("Failed to get an AccessToken, returned null")
                        Return
                    End If


                    ' Check to see if we failed to get an Access token via a Refresh token that has expired or been revoked, if so we need to re-authenticate
                    If accessTokenResponse.HttpStatusCode = HttpStatusCode.BadRequest AndAlso Equals(accessTokenResponse.Error, "invalid_grant") Then
                        Console.WriteLine("Refresh token expired, attempting to get a new Access Token")
                        accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest, ignoreCache:=True)
                    End If

                    If accessTokenResponse.HttpStatusCode <> HttpStatusCode.OK Then
                        Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription)
                        Return
                    End If


                    ' User Name and Password authentication
                    'if (ws.SendFaxEx(username, password, recipients, faxFiles, jobDetails, out faxJobIdList, out ttfp, out wsError))

                    ' OAuth 2.0 Authentication requires that you pass in an access token as part of the Authorization: header 
                    ' (No Password required)

                    ws.AccessToken = accessTokenResponse.AccessToken

                    If Not ws.SendFaxEx(username, "", recipients, faxFiles, jobDetails, faxJobIdList, ttfp, wsError) Then
                        Console.WriteLine("SendFaxEx Failed")
                        Console.WriteLine("Error Code: {0} Error Description: {1}", wsError.ErrorCode, wsError.ErrorString)


                        ' Check to see if response indicates access token has expired, if so attempt to Refresh or Get New OAuth access token and try method call again
                        If wsError.ErrorCode = -5059 Then
                            Console.WriteLine("AccessToken is not valid")

                            ' Attempt to get a new access token or use a refresh token, if we fail we should stop
                            accessTokenResponse = tokenManager.GetAccessToken(accessTokenRequest)

                            If accessTokenResponse Is Nothing Then
                                Console.WriteLine("Failed to get an AccessToken, returned null")
                                Return
                            End If

                            If accessTokenResponse.HttpStatusCode <> HttpStatusCode.OK Then
                                Console.WriteLine("AccessToken not valid, Error Code: {0}, Error Description: {1}", accessTokenResponse.Error, accessTokenResponse.ErrorDescription)
                                Return
                            End If
                        End If

                        Return
                    End If

                    Console.WriteLine("Estimated Time To Free Port: " & ttfp)
                    Console.WriteLine("Looping over Job ID's")

                    For Each faxJobId As FaxJobId In faxJobIdList
                        Console.WriteLine("JobId: " & faxJobId.JobId)
                    Next

                Catch ex As Exception
                    Console.WriteLine("Error while calling SendFaxEx : " & ex.Message)
                    Return
                End Try
            End Using
        End Sub


        ''' <summary>
        ''' returns the file type based on file extension
        ''' </summary>
        ''' <param name="filename"></param>
        ''' <returns></returns>
        Public Shared Function GetFileType(ByVal filename As String) As Integer
            If String.IsNullOrEmpty(filename) Then Throw New ArgumentNullException("filename")
            Dim extension As String = Path.GetExtension(filename)
            If String.IsNullOrEmpty(extension) Then Throw New ArgumentException("File does not contain an extension", "filename")

            Select Case extension.ToLower()
                Case ".tif", ".tiff"
                    Return 1
                Case ".doc", ".docx"
                    Return 100
                Case ".pdf"
                    Return 101
                Case ".rtf"
                    Return 102
                Case ".xls", ".xlsx"
                    Return 103
                Case ".ppt", ".pptx", ".pps", ".ppsx"
                    Return 104
                Case ".txt"
                    Return 105
                Case ".vsd", ".vsdx"
                    Return 106
                Case ".gif"
                    Return 107
                Case ".jpg", ".jpeg"
                    Return 108
                Case ".htm", ".html", ".mht", ".mhtml"
                    Return 111
                Case ".png"
                    Return 120
                Case Else
                    Throw New ArgumentException("Unrecognized file attachment type: " & extension, "filename")
            End Select
        End Function
    End Class
End Namespace

See Also