using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text;
using Microsoft.SqlServer.NotificationServices;
using CDO;
using Microsoft.ApplicationBlocks.ExceptionManagement;

namespace RapsNS
{
    public class myDeliveryProtocol : IDeliveryProtocol
    {
        private bool multicast = false;
        private NotificationStatusCallback notificationStatusCallback = null;

        public myDeliveryProtocol()
        {
            //Add constructor logic here if required.
        }

        //Implement the IDeliveryProtocol.Initialize method.
        public void Initialize(
        StringDictionary channelArgs,
        bool multicast,
        NotificationStatusCallback notificationStatusCallback)
        {
            this.multicast = multicast;
            this.notificationStatusCallback = notificationStatusCallback;
        }


        //Implement the IDeliveryProtocol.DeliverNotification method.
        public void DeliverNotification(
        NotificationHeaders[] notificationHeaders,
        string body)
        {
            bool successfulDelivery = false;
            Exception failureException = null;

            try
            {
                //Get a connection to SQL Server.
                // using (SqlConnection myConnection = new 
                SqlConnection(connectionString);

                CDO.Message mail = new CDO.MessageClass();
                CDO.Configuration iconfig;
                ADODB.Fields oFields;
                ADODB.Field oField;

                iconfig = mail.Configuration;
                oFields = iconfig.Fields;
                //oField =
                oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                oField.Value = 2;
                oField =
                oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                //oField.Value ;
                oField.Value = "213.36.41.244";
                oFields.Update();
                mail.Configuration = iconfig;

                mail.To = "croberts@calrom.co.uk";
                mail.From = "croberts@calrom.co.uk";
                mail.Subject = "Test";
                mail.TextBody = "testing testing";

                //Execute the command.
                mail.Send();

                successfulDelivery = true;

            }
            catch (Exception ex)
            {
                failureException = ex;
                //Handle any exceptions here; for instance,
                //write exception information to the event log.
                ExceptionManager.Publish(ex);

            }
            finally
            {
                SendStatus(notificationHeaders, successfulDelivery, body,
                failureException);
            }

        }
    }
}