Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Last active October 5, 2017 22:17
Show Gist options
  • Save martinnormark/9514422 to your computer and use it in GitHub Desktop.
Save martinnormark/9514422 to your computer and use it in GitHub Desktop.
Receive e-mails from Mailgun routes in ASP.NET MVC
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace WebApp
{
public class MailgunUploadController : Controller
{
//
// POST: /MailgunUpload/Mail
[HttpPost]
[ValidateInput(false)]
public ActionResult Mail(FormCollection form)
{
try
{
EmailUpload email = new EmailUpload();
email.FromEmail = Request.Unvalidated.Form["sender"];
email.RecipientEmail = Request.Unvalidated.Form["recipient"];
email.Subject = Request.Unvalidated.Form["subject"];
email.BodyPlain = Request.Unvalidated.Form["body-plain"];
email.StrippedText = Request.Unvalidated.Form["stripped-text"];
email.StrippedSignature = Request.Unvalidated.Form["stripped-signature"];
email.BodyHtml = Request.Unvalidated.Form["body-html"];
email.StrippedHtml = Request.Unvalidated.Form["stripped-html"];
email.AttachmentCount = Request.Unvalidated.Form["attachment-count"].TryParseInt();
email.TimeStampSeconds = Request.Unvalidated.Form["timestamp"].TryParseLong().GetValueOrDefault();
email.TimeStamp = FromUnixTime(email.TimeStampSeconds);
// Work your magic
}
catch (Exception ex)
{
throw ex;
}
return Content("ok");
}
private static DateTime FromUnixTime(long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment