Handling Mailgun delivery confirmations in MVC4

I’m struggling to process Mailgun’s delivery confirmation webhooks in my MVC4 app. The main issue is accessing the Message-Id parameter. I’ve tried a few things:

  1. Using Request.Params[“Message-Id”] directly, but it needs requestValidationMode set to 2.0.
  2. Creating a custom model for auto-binding:
public class MailgunWebhookData
{
    public string EventType { get; set; }
    public string RecipientEmail { get; set; }
    public string DomainName { get; set; }

    [AllowHtml]
    [JsonProperty(PropertyName="DeliveryId")]
    public object UniqueMessageId { get; set; }

    public int DeliveryTime { get; set; }
    public string SecurityToken { get; set; }
    public string VerificationSignature { get; set; }        
}

I’ve also tried using [ValidateInput(false)] on the controller and [Bind(Exclude=“unwanted-field”)] on the model.

Nothing seems to work. The UniqueMessageId always comes back null. Any ideas on how to properly handle this in MVC4?

I’ve faced similar issues with Mailgun webhooks in MVC4. Here’s what worked for me:

Instead of relying on model binding, try accessing the raw form data directly in your controller action. You can do this using:

Request.Form[“Message-Id”]

This bypasses the model binding process and allows you to access the unaltered form data. Remember to validate and sanitize this input manually.

Also, ensure your action is decorated with [HttpPost] and [AllowHtml] attributes. If security is a concern, consider implementing a custom model binder that handles the Message-Id separately.

Lastly, double-check your Mailgun webhook configuration to ensure it’s sending the correct parameter name. Sometimes, the issue lies on the sender’s side rather than in your receiving code.

I have worked on Mailgun webhooks in MVC4 and ran into similar issues with accessing the Message-Id parameter. In my case, I solved the problem by creating a custom model binder that specifically handled this parameter. I registered the binder in Global.asax and used the ModelBinder attribute in my action method. This approach allowed the request data to be processed differently from the default model binding behavior, giving me better control. Also, ensure that the content type matches what Mailgun sends, usually application/x-www-form-urlencoded, to avoid parsing issues.