Skip to content

NSG.Library.EMail

Phil Huhn edited this page Apr 27, 2018 · 3 revisions

Table of Contents

Assembly: NSG.Library.EMail

Namespace: NSG.Library.EMail

The namespace contains a class for emailing via SMTP or SendGrid or Mailgun (Mailgun is untested). This requires an ILogger. NSG.Library.Logger has the default. Also needs NSG.Library.Helpers for reading the appSettings. Finally, it needs the following appSettings (see the tests):

  • Email:Enabled
  • Email:TestEmailName
  • Email:UseService
  • Email:ApiKey
  • Email:MailgunDomain

Class: EMail

A fluent interface for smtp mail message. Example:

    new EMail( from, to, "Subject", "Body").Send()

This is an implementation of ILogging. Errors are loggged to the ILogger during the sending of email.


EMail.#ctor(System.String,System.String,System.String,System.String)

Instantiate the SMTP MailMessage class using the four parameters. This sample shows how to call this constructor.

    IEMail _mail = new EMail("[email protected]", "[email protected]", "Sending email is Fun", "This is a test email.").Send();

Parameters

fromAddress

From email address

toAddress

To email address

subject

Email subject line

message

Email body

Return Value

IEMail to allow fluent design.


EMail.#ctor(NSG.Library.Logger.ILogger,System.String,System.String,System.String,System.String)

Instantiate the SMTP MailMessage class using the four parameters. This sample shows how to call this constructor:

    IEMail _mail = new EMail(Log.Logger, "[email protected]", "[email protected]", "Sending email is Fun", "This is a test email.").Send();

Parameters

logging

This injects an implementation of ILogging from NSG.Library.Logger

fromAddress

From email address

toAddress

To email address

subject

Email subject line

message

Email body

Return Value

IEMail to allow fluent design.


EMail.#ctor

Instantiate the SMTP MailMessage class using no parameters. This sample shows how to call this constructor.

    MailAddress _from = new MailAddress("[email protected]", "Example From User");
    MailAddress _to = new MailAddress("[email protected]", "Example To User");
    string _subject = "Sending email is fun";
    string _text = "This is a test email.";
    IEMail _mail = new EMail().From(_from).To(_to).Subject(_subject).Body(_text).Send();

Return Value

IEMail to allow fluent design.


EMail.#ctor(NSG.Library.Logger.ILogger)

Instantiate the SMTP MailMessage class using one parameter of logging. This sample shows how to call this constructor.

    MailAddress _from = new MailAddress("[email protected]", "Example From User");
    MailAddress _to = new MailAddress("[email protected]", "Example To User");
    string _subject = "Sending email is fun";
    string _text = "This is a test email.";
    IEMail _mail = new EMail(Log.Logger).From(_from).To(_to).Subject(_subject).Body(_text).Send();

Parameters

logging

This injects an implementation of ILogging from NSG.Library.Logger

Return Value

IEMail to allow fluent design.


EMail.NewMailMessage(System.String,System.String,System.String,System.String)

Given and EMail instance, start a new email message by instantiating a new SMTP MailMessage class without parameters.

Parameters

fromAddress

the from email address

toAddress

the from email address

subject

the subject line

message

the message body

Return Value

return its self


EMail.NewMailMessage

Given and EMail instance, start a new email message by instantiating a new SMTP MailMessage class without parameters.

Return Value

return its self


EMail.From(System.String)

Set the single from address

Parameters

fromAddress

string of an email address

Return Value

itself, IEMail to allow fluent design.


EMail.From(System.String,System.String)

Set the single from email address

Parameters

fromAddress

string of an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


EMail.From(System.Net.Mail.MailAddress)

Set the single from email address

Parameters

fromAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


EMail.To(System.String)

Add a To email address.

Parameters

toAddress

an email address

Return Value

return its self


EMail.To(System.String,System.String)

Add a to email address.

Parameters

toAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


EMail.To(System.Net.Mail.MailAddress)

Add a to email address.

Parameters

toAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


EMail.CC(System.String)

Add a carbon copy (cc) email address.

Parameters

ccAddress

an email address

Return Value

return its self


EMail.CC(System.String,System.String)

Add a carbon copy (cc) email address.

Parameters

ccAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


EMail.CC(System.Net.Mail.MailAddress)

Add a carbon copy (cc) email address.

Parameters

ccAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


EMail.BCC(System.String)

Add a blind carbon copy (bcc) email address.

Parameters

bccAddress

an email address

Return Value

return its self


EMail.BCC(System.String,System.String)

Add a blind carbon copy (bcc) email address.

Parameters

bccAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


EMail.BCC(System.Net.Mail.MailAddress)

Add a blind carbon copy (bcc) email address.

Parameters

bccAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


EMail.Subject(System.String)

Set the title of email message.

Parameters

subject

the subject line

Return Value

return its self


EMail.Body(System.String)

Set the body of the email message.

Parameters

body

Email body

Return Value

return its self


EMail.Html(System.Boolean)

Is the mail message body Html

Parameters

isBodyHtml

true ot false, is body Html

Return Value

itself, IEMail to allow fluent design.


EMail.Attachment(System.Byte[],System.String,System.String)

Add an attachment to the mail message. This sample shows how to call this constructor.

    IEMail _mail = new EMail("[email protected]", "[email protected]", "Sending email is Fun", "This is a test email.")
         .Attachment(_textBuffer, _textFile, _textMimeType).Send();

Parameters

buffer

byte buffer containing the attachment

displayName

name placed on the attachment

mimeType

mime type like 'application/pdf'

Return Value

return its self


EMail.Send

Use appropriate EMail to send out the message/attachments. After sending clean-up the attachments and the streams and finally the mail-message.

Return Value

return its self


EMail.SendAsync

Use MMPA.Library.EMailing to send out the message/attachments. After sending clean-up the attachments and the streams and finally the mail-message.

Return Value

return its self


EMail.GetMailMessage

Get the current MailMessage

Return Value

the current MailMessage


EMail.Logs(System.Int32)

retrurn the last few log messages...

Parameters

lastCount

Count of last log records to return

Return Value

list of strings


EMail.Emails

Output the list of string of the last max emails sent

Return Value

list of strings


EMail.CleanUp

Naturally and automatically dispose of mail-message resource.


EMail.EmailToString

Format a string of the current mail message

Return Value

formated string of a mail message


EMail.ToEmailAddress(System.Net.Mail.MailAddress)

Format and SMTP MailAddress structure to a string

Parameters

mailAddress

SMTP MailAddress

Return Value

string like Phil Huhn [email protected]


EMail.SendList

Write the current email to a list of string

Properties

MailgunUrl

Property to override the default MailGun URL.


EMail.ToMailGun

Translate from MailMessage to MultipartFormDataContent.

Return Value

MultipartFormDataContent (see unit test)


EMail.SendMailGunAsync(System.String,System.String)

Send the email and any attachments, via the MailGun async task. Note: this was not tested (did not have an account).

Parameters

apiKey

The MailGun api key.

domainName

Your domain defined with MailGun.

Return Value

itself, IEMail to allow fluent design.


EMail.ConvertToEmailAddress(System.Net.Mail.MailAddress)

llamda method to move MailAddress email address to a new SendGrid EmailAddress address.

Parameters

address

a MailAddress email address

Return Value

a new SendGrid EmailAddress address


EMail.ConvertToMailAddress(SendGrid.Helpers.Mail.EmailAddress)

llamda method to move SendGrid EmailAddress email address to a new MS SMTP MailAddress address.

Parameters

address

a SendGrid EmailAddress email address

Return Value

a new MS SMTP MailAddress address


EMail.ToSendGrid

Translate from MailMessage to SendGridMessage.

Return Value

a new SendGrid SendGridMessage message found in SendGrid.Helpers.Mail


EMail.NewMailMessage(SendGrid.Helpers.Mail.SendGridMessage)

Convert a SendGridMessage mail message, and load it into a new message. This sample shows how to call the NewMailMessage method.

    // translate the message from json string of SendGrid message type
    JavaScriptSerializer j = new JavaScriptSerializer();
    SendGridMessage _sgm = (SendGridMessage)j.Deserialize(_jsonString, typeof(SendGridMessage));
    IEMail _email = new EMail(Log.Logger).NewMailMessage(_sgm).Send();

Parameters

sgm

a SendGridMessage mail message from SendGrid.Helpers.Mail

Return Value

itself, IEMail to allow fluent design.


EMail.SendSendGridAsync(System.String)

Send the email and any attachments, via the SendGrid async task.

Parameters

apiKey

The SendGrid api key

Return Value

itself, IEMail to allow fluent design.


EMail.SendSmtp

Send the email and any attachments, via the SMTP.

Return Value

itself, IEMail to allow fluent design.


EMail.SendSmtpAsync

Send the email and any attachments, via the SMTP async task. After sending clean-up the attachments and the streams and finally the mail-message.

Return Value

itself, IEMail to allow fluent design.

Class: IEMail

The <see cref="N:NSG.Library.EMail"></see> namespace contains a class for emailing via SMTP or SendGrid or Mailgun (Mailgun is untested). This requires an ILogger. NSG.Library.Logger has the default. Also needs NSG.Library.Helpers for reading the appSettings. Finally, it needs the following appSettings (see the tests):

  • production,
  • Email:Enabled
  • Email:TestEmailName,
  • Email:UseService,
  • Email:ApiKey,
  • Email:MailgunDomain.

IEMail.NewMailMessage(System.String,System.String,System.String,System.String)

Instantiate the SMTP MailMessage class using the four parameters.

Parameters

fromAddress

From email address

toAddress

To email address

subject

Email subject line

message

Email body

Return Value

IEMail to allow fluent design.


IEMail.NewMailMessage

Instantiate the SMTP MailMessage class without parameters.

Return Value

IEMail to allow fluent design.


IEMail.NewMailMessage(SendGrid.Helpers.Mail.SendGridMessage)

Load a SendGridMessage mail message into this message This sample shows how to call the NewMailMessage method.

    // translate the message from json string of SendGrid message type
    JavaScriptSerializer j = new JavaScriptSerializer();
    SendGridMessage _sgm = (SendGridMessage)j.Deserialize(_jsonString, typeof(SendGridMessage));
    IEMail _email = new EMail(Log.Logger).NewMailMessage(_sgm).Send();

Parameters

sgm

a SendGridMessage mail message

Return Value

IEMail to allow fluent design.


IEMail.GetMailMessage

Get the current MailMessage

Return Value

the current MailMessage


IEMail.From(System.String)

Set the single from email address

Parameters

fromAddress

an email address

Return Value

itself, IEMail to allow fluent design.


IEMail.From(System.String,System.String)

Set the single from email address

Parameters

fromAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


IEMail.From(System.Net.Mail.MailAddress)

Set the single from email address

Parameters

fromAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


IEMail.To(System.String)

Add a to email address.

Parameters

toAddress

an email address

Return Value

itself, IEMail to allow fluent design.


IEMail.To(System.String,System.String)

Add a to email address.

Parameters

toAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


IEMail.To(System.Net.Mail.MailAddress)

Add a to email address.

Parameters

toAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


IEMail.CC(System.String)

Add a carbon copy (cc) email address

Parameters

ccAddress

an email address

Return Value

itself, IEMail to allow fluent design.


IEMail.CC(System.String,System.String)

Add a carbon copy (cc) email address

Parameters

ccAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


IEMail.CC(System.Net.Mail.MailAddress)

Add a carbon copy (cc) email address

Parameters

ccAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


IEMail.BCC(System.String)

Add a blind carbon copy (bcc) email address

Parameters

bccAddress

an email address

Return Value

itself, IEMail to allow fluent design.


IEMail.BCC(System.String,System.String)

Add a blind carbon copy (bcc) email address

Parameters

bccAddress

an email address

name

display name of the email address

Return Value

itself, IEMail to allow fluent design.


IEMail.BCC(System.Net.Mail.MailAddress)

Add a blind carbon copy (bcc) email address

Parameters

bccAddress

A MailAddress, including an email address and the name

Return Value

itself, IEMail to allow fluent design.


IEMail.Subject(System.String)

Set the email subject line

Parameters

subject

Email subject line

Return Value

itself, IEMail to allow fluent design.


IEMail.Body(System.String)

Set the email body/text.

Parameters

body

Email body

Return Value

itself, IEMail to allow fluent design.


IEMail.Html(System.Boolean)

Is the mail message body Html

Parameters

isBodyHtml

true ot false, is body Html

Return Value

itself, IEMail to allow fluent design.


IEMail.Attachment(System.Byte[],System.String,System.String)

Set an email attachment.

Parameters

buffer

byte array of the file to attach

displayName

file name of the attachment

mimeType

mime type of the attachment

Return Value

itself, IEMail to allow fluent design.


IEMail.Send

Send the email and any attachments, via the means defined in the appSetting:

  • Email:UseService.

Return Value

itself, IEMail to allow fluent design.


IEMail.SendAsync

Use MMPA.Library.EMailing to send out the message/attachments. After sending clean-up the attachments and the streams and finally the mail-message.

Return Value

return its self


IEMail.SendSmtp

Send the email and any attachments, via the SMTP.

Return Value

itself, IEMail to allow fluent design.


IEMail.SendSmtpAsync

Send the email and any attachments, via the SMTP async task.

Return Value

itself, IEMail to allow fluent design.


IEMail.ToSendGrid

Translate from MailMessage to SendGridMessage.

Return Value

SendGridMessage


IEMail.SendSendGridAsync(System.String)

Send the email and any attachments, via the SendGrid async task.

Parameters

apiKey

The SendGrid api key

Return Value

itself, IEMail to allow fluent design.

Properties

MailgunUrl

Property to override the default MailGun URL.


IEMail.ToMailGun

Translate from MailMessage to MultipartFormDataContent.

Return Value

MultipartFormDataContent (see unit test)


IEMail.SendMailGunAsync(System.String,System.String)

Send the email and any attachments, via the MailGun async task. Note: this was not tested (did not have an account).

Parameters

apiKey

The MailGun api key.

domainName

Your domain defined with MailGun.

Return Value

itself, IEMail to allow fluent design.


IEMail.Logs(System.Int32)

retrurn the last few log messages...

Parameters

lastCount

Count of last log records to return

Return Value

list of strings


IEMail.Emails

Output a list of string of the last # of emails sent

Return Value

list of strings

Clone this wiki locally