An event that occurs when a message is received.

Namespace:  Linxter.SDK
Assembly:  Linxter.SDK (in Linxter.SDK.dll) Version: 1.5.0.0 (1.5.0.0)

Syntax

C#
event Action<MessageReceivedEventArgs> MessageReceived
Visual Basic (Declaration)
Event MessageReceived As Action(Of MessageReceivedEventArgs)

Examples

CopyC#
private ILinxterMessaging m_LinxterMessagingInstance;
private ILinxterRegistration m_LinxterRegistration;
private ILinxterCommunicationChannel m_LinxterCommChannels;

public void CompleteSetupExample()
{
    // Instantiate Linxter using default path to database file
    m_LinxterMessagingInstance = new LinxterSDK();

    // Fetch registration info and comm channels
    m_LinxterRegistration = m_LinxterMessagingInstance as ILinxterRegistration;
    m_LinxterCommChannels = m_LinxterMessagingInstance as ILinxterCommunicationChannel;

    // Register this program instance with the ISB, if it's not registered yet.  This is only done 
    // the first time you run the application.
    if (!m_LinxterRegistration.IsRegistered)
    {
        m_LinxterRegistration.RegisterProgramInstance();
    }

    // Make sure registration is complete
    if (!m_LinxterRegistration.IsActivated)
    {
        while (!m_LinxterRegistration.IsActivated)
        {
            Thread.Sleep(250);
        }
    }

    // Setup handler for the Linxter MessageReceived event.  Every time a message is received, 
    // it will fire the OnMessageReceived event.
    m_LinxterMessagingInstance.MessageReceived += OnMessageReceived;
    // MessageStatusNotifcation fires when the ISB has delivered or quarantined the message,
    // or the message failed to be delivered to the recipient.
    m_LinxterMessagingInstance.MessageStatusNotification += new Action<Linxter.SDK.EventArgs.MessageTrackingEventArgs>(OnMessageStatusNotification);
    // SendCompleted fires when an outbound message has been transferred to the ISB for deliver,
    // or the message failed to be sent.
    m_LinxterMessagingInstance.SendCompleted += new Action<Linxter.SDK.EventArgs.SendCompletedEventArgs>(OnSendCompleted);

    // Begin send/receive
    m_LinxterMessagingInstance.StartScheduledSend();
    m_LinxterMessagingInstance.StartScheduledReceive();
}

public void OnMessageReceived(Linxter.SDK.EventArgs.MessageReceivedEventArgs e)
{
    // Save the received message to a file
    ASCIIEncoding encoding = new ASCIIEncoding();
    File.WriteAllBytes(string.Format(".\\message{0}.txt", e.MessageId), encoding.GetBytes(e.Body));

    // If there are attachments, save them as well
    foreach (AttachmentInfo aInfo in e.Attachments)
    {
        Attachment thisAttachment = m_LinxterMessagingInstance.DownloadAttachment(aInfo);
        File.WriteAllBytes(string.Format(".\\{0}", thisAttachment.FileName), thisAttachment.Data);
    }
}

public void OnProgramClosed()
{
    // Wait until there are no messages in queue and then shut down messaging
    while (m_LinxterMessagingInstance.MessagesInQueue > 0 || m_LinxterMessagingInstance.MessagesInSecondaryQueue > 0)
    {
        Thread.Sleep(250);
    }

    m_LinxterMessagingInstance.StopScheduledSend();
    m_LinxterMessagingInstance.StopScheduledReceive();
}

void OnSendCompleted(Linxter.SDK.EventArgs.SendCompletedEventArgs obj)
{
    // Notify user if a message could not be sent
    if (obj.Cancelled)
    {
        MessageBox.Show(string.Format("Error sending message ID {0}.  The exception reported was: {1}",
            obj.MessageId.ToString(),
            obj.Error.Message),
            "Error sending message",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
    // ... otherwise we can assume the message was sent successfully
}

void OnMessageStatusNotification(Linxter.SDK.EventArgs.MessageTrackingEventArgs obj)
{
    // Log messages status to windows application event log
    using (System.Diagnostics.EventLog evt = new System.Diagnostics.EventLog("Application"))
    {
        evt.Source = "Complete Setup Example";
        evt.WriteEntry(string.Format("Message status event.\nMessage ID: {0}\nStatus: {1}\nReceived by: {2}\nTimestamp:{3}\nError info:{4}",
            obj.MessageId,
            obj.MessageStatus,
            obj.ReceiverInfo.ProgramInstanceAlias,
            obj.MessageStatusDateTime,
            obj.ErrorMessage),
            System.Diagnostics.EventLogEntryType.Information,
            1000);
    }

    // warn user if message was quarantined
    if (obj.MessageStatus.Equals(MessageTrackingEvent.Quarantined))
    {
        MessageBox.Show(string.Format("Your message ID {0} to {1} was quarantined!",
            obj.ReceiverInfo.ProgramInstanceAlias,
            obj.MessageId),
            "Warning",
            MessageBoxButtons.OK,
            MessageBoxIcon.Warning);
    }
}
CopyVB.NET
Private m_LinxterMessagingInstance As ILinxterMessaging
Private m_LinxterRegistration As ILinxterRegistration
Private m_LinxterCommChannels As ILinxterCommunicationChannel

Public Sub CompleteSetupExample()
    ' Instantiate Linxter using default path to database file
    m_LinxterMessagingInstance = New LinxterSDK()

    ' Fetch registration info and comm channels
    m_LinxterRegistration = TryCast(m_LinxterMessagingInstance, ILinxterRegistration)
    m_LinxterCommChannels = TryCast(m_LinxterMessagingInstance, ILinxterCommunicationChannel)

    ' Register this program instance with the ISB, if it's not registered yet.  This is only done 
    ' the first time you run the application.
    If Not m_LinxterRegistration.IsRegistered Then
        m_LinxterRegistration.RegisterProgramInstance()
    End If

    ' Make sure registration is complete
    If Not m_LinxterRegistration.IsActivated Then
        Do While Not m_LinxterRegistration.IsActivated
            Thread.Sleep(250)
        Loop
    End If

    ' Setup handler for the Linxter MessageReceived event.  Every time a message is received, 
    ' it will fire the OnMessageReceived event.
    AddHandler m_LinxterMessagingInstance.MessageReceived, AddressOf OnMessageReceived
    ' MessageStatusNotifcation fires when the ISB has delivered or quarantined the message,
    ' or the message failed to be delivered to the recipient.
    AddHandler m_LinxterMessagingInstance.MessageStatusNotification, AddressOf OnMessageStatusNotification
    ' SendCompleted fires when an outbound message has been transferred to the ISB for deliver,
    'or the message failed to be sent.
    AddHandler m_LinxterMessagingInstance.SendCompleted, AddressOf OnSendCompleted

    ' Begin send/receive
    m_LinxterMessagingInstance.StartScheduledSend()
    m_LinxterMessagingInstance.StartScheduledReceive()
End Sub

Public Sub OnMessageReceived(ByVal e As Linxter.SDK.EventArgs.MessageReceivedEventArgs)
    ' Save the received message to a file
    Dim encoding As New ASCIIEncoding()
    File.WriteAllBytes(String.Format(".\message{0}.txt", e.MessageId), encoding.GetBytes(e.Body))

    ' If there are attachments, save them as well
    For Each aInfo As AttachmentInfo In e.Attachments
        Dim thisAttachment As Attachment = m_LinxterMessagingInstance.DownloadAttachment(aInfo)
        File.WriteAllBytes(String.Format(".\{0}", thisAttachment.FileName), thisAttachment.Data)
    Next aInfo
End Sub

Public Sub OnProgramClosed()
    ' Wait until there are no messages in queue and then shut down messaging
    Do While m_LinxterMessagingInstance.MessagesInQueue > 0 OrElse m_LinxterMessagingInstance.MessagesInSecondaryQueue > 0
        Thread.Sleep(250)
    Loop

    m_LinxterMessagingInstance.StopScheduledSend()
    m_LinxterMessagingInstance.StopScheduledReceive()
End Sub

Private Sub OnSendCompleted(ByVal obj As Linxter.SDK.EventArgs.SendCompletedEventArgs)
    ' Notify user if a message could not be sent
    If obj.Cancelled Then
        MessageBox.Show(String.Format("Error sending message ID {0}.  The exception reported was: {1}", obj.MessageId.ToString(), obj.Error.Message), "Error sending message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    ' ... otherwise we can assume the message was sent successfully
End Sub

Private Sub OnMessageStatusNotification(ByVal obj As Linxter.SDK.EventArgs.MessageTrackingEventArgs)
    ' Log messages status to windows application event log
    Using evt As New System.Diagnostics.EventLog("Application")
        evt.Source = "Complete Setup Example"
        evt.WriteEntry(String.Format("Message status event." & vbLf & "Message ID: {0}" & vbLf & "Status: {1}" & vbLf & "Received by: {2}" & vbLf & "Timestamp:{3}" & vbLf & "Error info:{4}", obj.MessageId, obj.MessageStatus, obj.ReceiverInfo.ProgramInstanceAlias, obj.MessageStatusDateTime, obj.ErrorMessage), System.Diagnostics.EventLogEntryType.Information, 1000)
    End Using

    ' warn user if message was quarantined
    If obj.MessageStatus.Equals(MessageTrackingEvent.Quarantined) Then
        MessageBox.Show(String.Format("Your message ID {0} to {1} was quarantined!", obj.ReceiverInfo.ProgramInstanceAlias, obj.MessageId), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End If
End Sub

See Also