1

I'm trying to create a program in C# which should make it able to create appointments in someone else's Outlook calendar. I have code which makes it able to create appointments in my own calendar. I searched on Google and I found I should use impersonation. So I added the line:

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

This is my code:

private void button2_Click(object sender, EventArgs e) {
try{
    ExchangeService service = new ExchangeService();
    service.UseDefaultCredentials = true;   
    service.Credentials = new WebCredentials("Test@domain.com", "password");
    service.AutodiscoverUrl("Test@domain.com", adAutoDiscoCallBack);
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "Test2@domain.com");

    Appointment appointment = new Appointment(service);
    // Set the properties on the appointment object to create the appointment.
    appointment.Subject = "Tennis lesson";
    appointment.Body = "Focus on backhand this week.";
    appointment.Start = DateTime.Now.AddDays(2);
    appointment.End = appointment.Start.AddHours(1);
    appointment.Location = "Tennis club";
    appointment.ReminderDueBy = DateTime.Now;

    // Save the appointment to your calendar.
    appointment.Save(SendInvitationsMode.SendToNone);

    // Verify that the appointment was created by using the appointment's item ID.
    Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
}
}catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

internal static bool adAutoDiscoCallBack(string redirectionUrl) {
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https") {
        result = true;
    }

    return result;
}

The problem is that I keep getting this error ""The SMTP-address has no mailbox associated with it."

Is it because impersonation isn't allowed on the server? If so how do I allow it? I hope someone can help.

Ps: Sorry for the bad english

Quinten Henry
  • 125
  • 1
  • 2
  • 10
  • We're missing the exception message; Place a try/catch around your code which results in that exception and see what the error message is. It looks like someone else has a similar issue here: http://stackoverflow.com/questions/32062394/exception-thrown-microsoft-exchange-webservices-data-serviceresponseexception – Squiggle Feb 25 '16 at 09:02
  • Now, i keep getting the message: "The SMTP-address has no mailbox associated with it. – Quinten Henry Feb 25 '16 at 09:13
  • 1
    There's your problem! It looks to me like you're doing everything correct in code - but there's prerequisites on the account that aren't met. Update your question with this error message. – Squiggle Feb 25 '16 at 10:12

1 Answers1

5

A few suggestions if this is Office365 or Exchange 2013 you first need the PrimarySMTP address of the Mailbox you wish to access eg

String MailboxToAccess = "PrimarySMTP@domain.demo";

Note for some tenants the SMTP and UPN/Logon are the same but that is not always the case.

This is what user you are going to be impersonating eg

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, MailboxToAccess);

You should also add in the X-AnchorMailbox header https://learn.microsoft.com/en-us/archive/blogs/webdav_101/best-practices-ews-authentication-and-access-issues eg

service.HttpHeaders.Add("X-AnchorMailbox", MailboxToAccess);

Also when you go to save the Appointment use the FolderId class with the Mailbox overload to ensure your hitting the correct Mailbox eg

FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, MailboxToAccess);
appointment.Save(CalendarFolderId,SendInvitationsMode.SendToNone);

Cheers Glen

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
Glen Scales
  • 20,495
  • 1
  • 20
  • 23