Click here to Skip to main content
12,943,299 members (61,252 online)
Click here to Skip to main content


17 bookmarked
Posted 19 Apr 2010

EWS Mail Notifier

, 19 Apr 2010 CPOL
Simple mail notifier for Exchange
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace EWSConsoleNotify
    public class Notify
        private ExchangeService _service;
        private TcpListener _listener;
        private Thread _th;

        public string SubscriptionId { get; set; }

        public string Name
                return "EWSConsoleNotify";

        public void WriteLog(string Message)

        public void ShowMessage(string UniqueId)
            EmailMessage mail = ((EmailMessage)Item.Bind(_service, new ItemId(UniqueId)));

            Console.WriteLine("New mail from: {0}, Subject: {1}", mail.From.Name, mail.Subject);

        public void Start()
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(
                Object obj,
                X509Certificate certificate,
                X509Chain chain,
                SslPolicyErrors errors)
                return true;
                // TODO: check for a valid certificate

            // Create the subscription
            _service = new ExchangeService();
            // Set credentials: user, password and domain
            _service.Credentials = new WebCredentials("user", "password", "domain");
            // URL of the EWS
            _service.Url = new Uri("https://exchange_server/ews/exchange.asmx");

            PushSubscription ps = _service.SubscribeToPushNotificationsOnAllFolders(new Uri("http://your_IP:5050"),
                5 // Every 5 minutes Exchange will send us a status message
                , null, EventType.NewMail);

            SubscriptionId = ps.Id;

            // Listen on port 5050
            _listener = new TcpListener(5050);

            _th = new Thread(new ThreadStart(ProcessIncomingMessages));

            WriteLog("Listening for incomming messages...");

        public void Stop()

        private void ProcessIncomingMessages()
            for (; ; )
                CSHTTPRequest csHttpRequest = new CSHTTPRequest(_listener.AcceptTcpClient(), this);


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Spain Spain
MCTS: .NET Framework 3.5 ASP.NET Applications

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 19 Apr 2010
Article Copyright 2010 by avg360
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid