3

I am new to using mailgun. But I was tasked to create a C# MVC controller that will receive email messages forwarded by mailgun. I saw in the documentation released by mailgun a sample code to do this, but it uses Django, which I am not very much familiar with. Here is the sample code written in Django:

def on_incoming_message(request):
 if request.method == 'POST':
     sender    = request.POST.get('sender')
     recipient = request.POST.get('recipient')
     subject   = request.POST.get('subject', '')

     body_plain = request.POST.get('body-plain', '')
     body_without_quotes = request.POST.get('stripped-text', '')
     # note: other MIME headers are also posted here...

     # attachments:
     for key in request.FILES:
         file = request.FILES[key]
         # do something with the file

 # Returned text is ignored but HTTP status code matters:
 # Mailgun wants to see 2xx, otherwise it will make another attempt in 5 minutes
 return HttpResponse('OK')

my question now is, how am I going to convert this in C#? a sample code would definitely be amazing. Thanks for the help in advance. Sorry if you find this question stupid.

Lemuel Nitor
  • 333
  • 4
  • 5
  • 15

1 Answers1

4

What you need to do is receive the post request from mailgun, then split it up into it's respective parts, that is what the django example is doing above.

This is an example of how to recieve an http post.

public void ProcessRequest(HttpContext context)
{
    var value1 = context.Request["param1"];
    var value2 = context.Request["param2"];
    ...
}

What this is using is a C# library which makes it easy to read the contents of a POST.

look at this for more detailed information:

http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.90).aspx

Community
  • 1
  • 1
hamhut1066
  • 406
  • 3
  • 13