This is a library for working with Mailgun API. It is compatible with both .Net Core and .Net Framework.
Currently supports:
The current version of the library satisfies all my needs. Additional features will be implemented when someone ask for it. So if you need some feature, you may create an "Issue" and describe what you want.
PM> Install-Package PureApi.Mailgun
First of all, you need to create a Mailgun account, create a domain that will be used to send emails, and get an API key for your account. Then you can use this library to send emails from your domain.
var mailgun = new Mailgun("sending-domain.com", "api-key");
You can send email messages with any parameters, that supported by Mailgun API.
var result = await mailgun.SendMessageAsync(
new EmailAddress("[email protected]", "Support Team"), // From
new EmailAddress("[email protected]"), // To
"Welcome", // Subject
"Welcome, dear user!"); // Message
if (!result.Successful)
Console.WriteLine(result.ErrorMessage);
var message = new Message()
{
From = new EmailAddress("[email protected]", "Support Team"),
To = new List<EmailAddress>()
{
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]")
},
Cc = new List<EmailAddress>()
{
new EmailAddress("[email protected]")
},
Attachments = new List<Attachment>()
{
new Attachment("/images/photo-1.jpg")
},
Subject = "Hello",
Html = "<h1>Hello, dear user!</h1>",
Dkim = true,
RequireTls = true,
Tracking = true
};
var result = await mailgun.SendMessageAsync(message);
Mailing Lists provide a convenient way to send to multiple recipients by using an alias email address. Mailgun sends a copy of the message sent to the alias address to each subscribed member of the Mailing List.
Mailing Lists API methods are available in the MailingListManager
class.
var result = await mailgun.Lists.CreateMailingListAsync("news");
var result = await mailgun.Lists.AddMemberToListAsync("news", "[email protected]");
var result = await mailgun.SendMessageToListAsync(
"news", // Mailing list
new EmailAddress("[email protected]", "Support Team"), // From
"Welcome", // Subject
"Welcome, dear users!"); // Message
var result = await mailgun.Lists.UpdateMemberStatusAsync("news", "[email protected]", false);
Mailgun Routes are a powerful way to handle the incoming traffic.
Routes API methods are available in the RouteManager
class. It's recommended to use the RouteFilters
helper class to create filter expressions and the RouteActions
helper class to create a set of actions.
var result = await mailgun.Routes.CreateRouteAsync(
RouteFilters.MatchRecipient(".*@bar.com"),
RouteActions.AddForward("http://callback.com").AddStore());
if (result.Successful)
Console.WriteLine(result.Response.Route.Id);