Skip to content

slick-nic/mailgun_csharp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mailgun_csharp

A Mailgun API library for C#

Made with inspiration from the Mailgun-php implementation, this library wraps the Mailgun HTTP API for easy use in C# applications.

##Download You can download the source and build it using VS2013, or use Nuget Install-Package mailgun_csharp and the ASP.net package (if needed) Install-Package mailgun_csharp.AspNet.Identity

##Basic usage The current implementation supports creating a MessageService and sending Messages. A Message can be created manually or you can use the recommended MessageBuilder.

     var mg = new MessageService(ApiKey);
     //var mg = new MessageService(ApiKey,false); //you can specify to use SSL or not, which determines the url API scheme to use
     //var mg = new MessageService(ApiKey,false,"api.mailgun.net/v3"); //you can also override the base URL, which defaults to v2

     //build a message
     var message = new MessageBuilder()
          .AddToRecipient(new Recipient
                {
                    Email = "[email protected]",
                    DisplayName = "Charlie King"
                })
          .SetTestMode(true)
          .SetSubject("Plain text test")
          .SetFromAddress(new Recipient {Email = "[email protected]", DisplayName = "Mailgun C#"})
          .SetTextBody("This is a test")
          .GetMessage();

     var content = await mg.SendMessageAsync(Domain, message);
     content.ShouldNotBeNull();

The current Message object supports all the options listed in the Mailgun documentation here

##ASP.net Identity Usage The new ASP.net Identity system supports the addition of an IIdentityMessageService for sending authorization emails. The Mailgun.AspNet.Identity package has an implementation for use with your Mailgun account. Usage would be something like this-

     //wherever you initialize your user manager
    _userManager = new UserManager<IdentityUser, string>(store);
    
    //simple usage
    _userManager.EmailService = new MailgunMessageService("domain","apiKey");

The above configuration will send plain text emails using the specified domain and apiKey over SSL. For more options, you can pass in an IMailgunMessageServiceOptions object, to specify any custom rackspace configuration options you might have.

     //wherever you initialize your user manager
    _userManager = new UserManager<IdentityUser, string>(store);
    
    //advanced usage
    _userManager.EmailService = new MailgunMessageService(new MailgunMessageServiceOptions
            {
                ApiKey = "",
                Domain = "",
                TestMode = true,
                Tracking = true,
                TrackingClicks = true,
                TrackingOpen = true,
                UseDkim = true,
                DefaultHeaders = new Dictionary<string, string>{{"X-Some-Custom-Header","Custom"}},
                DefaultTags = new Collection<string>{"AuthorizationEmails"},
                BaseUrlOverride = "api.mailgun.net/v3" //use a different base URL
            });

##TODO There is much more to do, but on the plate next are-

  • Stored Messages
  • Events

About

A Mailgun api library for C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%