I am creating a application which will work as a window service in the back ground. This application will parse the new emails in the inbox and save the attachments. I tried streaming notification but as the connection disconnects after 30 mints I thought to use pull notifications. Below is the code which I debug but I don't see any output on console. As soon as I run the application it closes the console window so don't know if it is working. I want watch the new email as soon as it enters in the inbox so need some guidance how to achieve that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
using System.Configuration;
using System.Timers;
namespace PullNotification
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
WebCredentials wbcred = new WebCredentials(ConfigurationSettings.AppSettings["user"], ConfigurationSettings.AppSettings["PWD"]);
PullSubscription SubscriptionInbox;
service.Credentials = wbcred;
service.AutodiscoverUrl(ConfigurationSettings.AppSettings["user-id"], RedirectionUrlValidationCallback);
SubscriptionInbox = service.SubscribeToPullNotifications(new FolderId[] { WellKnownFolderName.Inbox }, 5/* subcription will end if the server is not polled within 5 mints*/, null/*to start a new subcription*/, EventType.NewMail, EventType.Modified);
//Timer myTimer = new Timer();
//myTimer.Elapsed += new ElapsedEventHandler(GetPullNotifications);
//myTimer.Interval = 10000;
//myTimer.Start();
GetEventsResults events = SubscriptionInbox.GetEvents();
EmailMessage message;
foreach (ItemEvent itemEvent in events.ItemEvents)
{
switch (itemEvent.EventType)
{
case EventType.NewMail:
try
{
Item item = Item.Bind(service, itemEvent.ItemId);
if (item.Subject == "A123")
{
Console.WriteLine("check the code");
}
}
catch (Exception e)
{
Console.WriteLine("error=" + e.Message);
}
break;
case EventType.Deleted:
Item item1 = Item.Bind(service, itemEvent.ItemId);
Console.WriteLine("Mail with subject" + item1.Subject + "--is deleted");
break;
}
}
//Loop
}
internal static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
//The default for the validation callback is to reject the URL
bool result=false;
Uri redirectionUri=new Uri(redirectionUrl);
if(redirectionUri.Scheme=="https")
{
result=true;
}
return result;
}
}
}
______________________________________________________________________________________________________________________
MyCCPay Login
EWS managed API Pull notification to watch the new email is
-
- Posts: 1
- Joined: March 25th, 2020, 3:00 am