3

我是使用 mailgun 的新手。但我的任务是创建一个 C# MVC 控制器,它将接收 mailgun 转发的电子邮件消息。我在 mailgun 发布的文档中看到了一个示例代码来执行此操作,但它使用了我不太熟悉的 Django。下面是用 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')

我现在的问题是,我将如何在 C# 中转换它?一个示例代码肯定会很棒。我在这里先向您的帮助表示感谢。对不起,如果你觉得这个问题很愚蠢。

4

1 回答 1

4

您需要做的是接收来自 mailgun 的 post 请求,然后将其拆分为各自的部分,这就是 django 示例在上面所做的。

这是一个如何接收 http post的示例。

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

它使用的是一个 C# 库,可以轻松阅读 POST 的内容。

查看此以获取更多详细信息:

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

于 2013-11-25T03:48:24.043 回答