Skip to content

Groxan/Api.Mailgun

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

.NET Mailgun API wrapper

This is a library for working with Mailgun API. It is compatible with both .Net Core and .Net Framework.

Currently supports:

Feature request

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.

Install

PM> Install-Package PureApi.Mailgun

How to use

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.

Create a Mailgun instanse

var mailgun = new Mailgun("sending-domain.com", "api-key");

Sending email

You can send email messages with any parameters, that supported by Mailgun API.

Send simple message

var result = await mailgun.SendMessageAsync(
                new EmailAddress("support@your-domain.com", "Support Team"), // From
                new EmailAddress("user@gmail.com"),                          // To
                "Welcome",                                                   // Subject
                "Welcome, dear user!");                                      // Message

if (!result.Successful)
    Console.WriteLine(result.ErrorMessage);

Build message with required parameters

var message = new Message()
{
    From = new EmailAddress("support@your-domain.com", "Support Team"),
    To = new List<EmailAddress>()
    {
        new EmailAddress("user1@gmail.com"),
        new EmailAddress("user2@gmail.com")
    },
    Cc = new List<EmailAddress>()
    {
        new EmailAddress("user3@gmail.com")
    },
    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);

Working with mailing lists

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.

Create new mailing list

var result = await mailgun.Lists.CreateMailingListAsync("news");

Subscribe member to list

var result = await mailgun.Lists.AddMemberToListAsync("news", "user@gmail.com");

Send simple message to all members in list

var result = await mailgun.SendMessageToListAsync(
                "news",                                                      // Mailing list
                new EmailAddress("support@your-domain.com", "Support Team"), // From
                "Welcome",                                                   // Subject
                "Welcome, dear users!");                                     // Message

Unsubscribe member from list

var result = await mailgun.Lists.UpdateMemberStatusAsync("news", "user@gmail.com", false);

Working with routes

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);

About

.NET Standard 2.0 library for sending email and working with mailing lists and routes via Mailgun API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages