Created
November 15, 2024 02:11
-
-
Save warrenkc/562da58e06beea3c6735b181c5d37702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Routes/#tag/Routes/operation/post-v3-routes | |
// This sample works with .net framework | |
private static async Task<Route> CreateRoute(string recipientAddress, string forwardAddress) | |
{ | |
RestClientOptions options = new RestClientOptions(new Uri("https://api.mailgun.net/v3")); | |
options.Authenticator = new HttpBasicAuthenticator("api", ApiKey); | |
using (RestClient client = new RestClient(options)) | |
{ | |
//Url encoded format, example: https://example.com/api?priority=0&description=it%27s+a+new+route&expression=match_recipient%28%22user%40example.com%22%29&action=%5B%27forward%28%22forward%40example.com%22%29%27%5D | |
RestRequest request = new RestRequest(); | |
request.Resource = "/routes"; | |
request.AddParameter("priority", "0"); | |
request.AddParameter("description", "it's a new route"); | |
request.AddParameter("expression", $"match_recipient(\"{recipientAddress}\")"); | |
request.AddParameter("action", $"forward(\"{forwardAddress}\")"); | |
var response = await client.ExecutePostAsync(request); | |
// Parse the JSON and select only the "route" part | |
JObject jsonObject = JObject.Parse(response.Content); | |
Route route = jsonObject["route"].ToObject<Route>(); | |
return route; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment