10 Mar

Mognet: Mail NuGet

Mognet Sengrid is a .NET Standard 2.0 NuGet used to send mails via Sengrid.
With this package you’ll be able to send mails from any sort of project type ranging from .NET Framwork or Core to Xamarin and UWP.

Contents



Installation

The package can be found on the NuGet server when you look for “Mognet” https://www.nuget.org/packages/MognetSendgrid/.
Install it in your project and you’re good to go.

 

EmailAddress

Required

  • Email

Optional

  • Name

Code

var email = new EmailAddress("something@mail.com", "name");

 

SendgridMail

Required

  • The Sendgird API Key
  • A ‘To’-address
  • A ‘From’-address

Optional

  • Subject (default: ” “)
  • Body (default: ” “)
  • TemplateId
  • SubstitutionTags
  • IsHtml (default: false)

By default the Subject and Body are a string with a space in it.
This is because Sendgrid does not allow an empty body or subject, but due to your template you might not really need it.

SubstitutionTags are only used in combination with a template.

Code

var mail = new SendgridMail(Settings.Default.SendgridAPIKey, new EmailAddress("to@mail.com", "To"), new EmailAddress("from@mail.com", "From"))
{
    Subject = "Subject",
    TemplateId = "123456789",
    Body = "Body",
    IsHtml = false,
    SubstitutionTags = new Dictionary<string, string> { { "tag", "value" } }
};


Sending Mails

To send an e-mail you’ll have to create your SendgirdMail and call the SendgridMailer.Send() function.
This function will either return “success” when successful, or return the error given by Sendgrid in string.

Empty

var mail= new SendgridMail(Settings.Default.SendgridAPIKey, new EmailAddress("to@mail.com"), new EmailAddress("from@mail.com"));

var result = SendgridMailer.Send(mail);

I don’t know why you would do this, but it is possible to send an empty e-mail.

Plain text

var mail = new SendgridMail(Settings.Default.SendgridAPIKey, new EmailAddress("to@mail.com", "To"), new EmailAddress("from@mail.com", "From"))
{
    Subject = "Subject",
    Body = "Body"
};

var result = SendgridMailer.Send(mail);

Because the IsHtml property is set to False by default and no TemplateId is given, this e-mail will result in a simple plain-text e-mail.

HTML

var mail = new SendgridMail(Settings.Default.SendgridAPIKey, new EmailAddress("to@mail.com", "To"), new EmailAddress("from@mail.com", "From"))
{
    Subject = "Subject",
    Body = "<h1>Body</h1>",
    IsHtml = True,
};

var result = SendgridMailer.Send(mail);

By setting the property IsHtml to True everything inside the body will be handled as HTML.
This can also be combined with sending a template as shown below.

Template

var mail = new SendgridMail(Settings.Default.SendgridAPIKey, new EmailAddress("to@mail.com", "To"), new EmailAddress("from@mail.com", "From"))
{
    Subject = "Subject",
    TemplateId = "123456789",
    Body = "<i>Body</i>",
    IsHtml = true,
    SubstitutionTags = new Dictionary<string, string> { { "tag", "<b>value</b>" } }
};

var result = SendgridMailer.Send(mail);

By filling in the property TemplateId with a correct Id from your account, you’ll be able to send an e-mail with that template.
Note that it is possible to send HTML in your tags or body is you put the IsHtml on True.

By default it’s not needed to provide a Subject or Body.



Release Notes

1.0.0 – Initial Release
1.1.0 – Update to .NET Standard 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *