Ive stato esaminando questo più recentemente e avere il seguente:
Sto facendo funzionare un'applicazione console che impostare una connessione di streaming per verificare la presenza di nuove e-mail che arrivano nella casella di posta per [email protected]
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
WebCredentials wbcred = new WebCredentials("userone", "password", "mydomain");
service.Credentials = wbcred;
Console.WriteLine("Attempting to autodiscover Url...");
service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);
EWSConnection.SetStreamingNotifications(service);
Console.ReadKey();
Environment.Exit(0);
}
mio EWSConnection
classe statica sembra vagamente simile a questo:
public static void SetStreamingNotifications(ExchangeService service)
{
_service = service;
try
{ var subscription = service.SubscribeToStreamingNotifications(
new FolderId[] { WellKnownFolderName.Inbox },
EventType.NewMail);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 5);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.Open();
_subscription = subscription;
_subscriptionConnection = connection;
Console.WriteLine($"Connection Open:{connection.IsOpen}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Con questo, è possibile vedere che ho sottoscritto lo OnNotificationEvent
ea sua volta il mio metodo OnEvent
verrà richiamato quando arriva una nuova email. Quando arriva una nuova e-mail a questa casella postale, ho l'obbligo di creare un'attività e assegnarla alla persona interessata, in base a quale sia la proprietà ToAddress
.
private static void CreateTask(NotificationEvent notification, RecievedMail recievedMail)
{
try
{
Console.WriteLine("Attempting to create task");
Microsoft.Exchange.WebServices.Data.Task task = new Microsoft.Exchange.WebServices.Data.Task(_service);
task.DueDate = DateTime.Now.AddDays(7);
task.Body = recievedMail.Body;
task.Subject = recievedMail.Subject;
string targetSharedEmailAddress = null;
if (recievedMail.ToAddress.ToString() == "humanresources <SMTP:[email protected]>")
{
targetSharedEmailAddress = "[email protected]";
}
task.Save(new FolderId(WellKnownFolderName.Tasks,targetSharedEmailAddress)); //
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
In un primo momento si può vedere che ho provato ad aggiungere la persona che volevo il compito di essere creato per nel metodo task.Save
, tuttavia, una volta sono andato a Outlook per interagire con l'operazione appena creata, il proprietario era ancora userone
, ovvero la persona con cui sono state utilizzate le credenziali per connettersi al servizio, questo è stato un problema per me poiché ho bisogno che il proprietario dell'attività sia usertwo
.
Ciò è stato ottenuto eliminando la variabile targetSharedEmailAddress
e utilizzando invece la proprietà ImpersonatedUserId
dell'oggetto ExchangeServer
.
if (recievedMail.ToAddress.ToString() == "humanresources <SMTP:[email protected]>")
{
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");
}
https://msdn.microsoft.com/en-us/library/office/dd633680(v=exchg.80).aspx
Grazie per guardare in questo per me - è molto apprezzato! Attendo con impazienza la tua risposta ... –