Skip to content

NSG.Library.EMail

Phil Huhn edited this page May 20, 2019 · 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();

Fields

Logging

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

Constructors

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

Parameters
fromAddress

From email address

toAddress

To email address

subject

Email subject line

message

Email body Instantiate the SMTP MailMessage class using the four parameters.

For example: This sample shows how to call this constructor.

  IEMail _mail = new EMail("testfrom@example.com", "testto@example.com", "Sending email is Fun", "This is a test email.").Send();
Return Value

IEMail to allow fluent design.


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

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 Instantiate the SMTP MailMessage class using the four parameters.

For example: This sample shows how to call this constructor.

  IEMail _mail = new EMail(Log.Logger, "testfrom@example.com", "testto@example.com", "Sending email is Fun", "This is a test email.").Send();
Return Value

IEMail to allow fluent design.


EMail()

Instantiate the SMTP MailMessage class using no parameters.

For example: This sample shows how to call this constructor.

  MailAddress _from = new MailAddress("testfrom@example.com", "Example From User");
  MailAddress _to = new MailAddress("testto@example.com", "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(NSG.Library.Logger.ILogger)

Parameters
logging

This injects an implementation of ILogging from NSG.Library.Logger Instantiate the SMTP MailMessage class using one parameter of logging.

For example: This sample shows how to call this constructor.

  MailAddress _from = new MailAddress("testfrom@example.com", "Example From User");
  MailAddress _to = new MailAddress("testto@example.com", "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();
Return Value

IEMail to allow fluent design.


Methods

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

Parameters
fromAddress

the from email address

toAddress

the from email address

subject

the subject line

message

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

Return Value

return its self


NewMailMessage()

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

Return Value

return its self


From(System.String)

Parameters
fromAddress

string of an email address Set the single from address

Return Value

itself, IEMail to allow fluent design.


From(System.String,System.String)

Parameters
fromAddress

string of an email address

name

display name of the email address Set the single from email address

Return Value

itself, IEMail to allow fluent design.


From(System.Net.Mail.MailAddress)

Parameters
fromAddress

A MailAddress, including an email address and the name Set the single from email address

Return Value

itself, IEMail to allow fluent design.


To(System.String)

Parameters
toAddress

an email address Add a To email address.

Return Value

return its self


To(System.String,System.String)

Parameters
toAddress

an email address

name

display name of the email address Add a to email address.

Return Value

itself, IEMail to allow fluent design.


To(System.Net.Mail.MailAddress)

Parameters
toAddress

A MailAddress, including an email address and the name Add a to email address.

Return Value

itself, IEMail to allow fluent design.


CC(System.String)

Parameters
ccAddress

an email address Add a carbon copy (cc) email address.

Return Value

return its self


CC(System.String,System.String)

Parameters
ccAddress

an email address

name

display name of the email address Add a carbon copy (cc) email address.

Return Value

itself, IEMail to allow fluent design.


CC(System.Net.Mail.MailAddress)

Parameters
ccAddress

A MailAddress, including an email address and the name Add a carbon copy (cc) email address.

Return Value

itself, IEMail to allow fluent design.


BCC(System.String)

Parameters
bccAddress

an email address Add a blind carbon copy (bcc) email address.

Return Value

return its self


BCC(System.String,System.String)

Parameters
bccAddress

an email address

name

display name of the email address Add a blind carbon copy (bcc) email address.

Return Value

itself, IEMail to allow fluent design.


BCC(System.Net.Mail.MailAddress)

Parameters
bccAddress

A MailAddress, including an email address and the name Add a blind carbon copy (bcc) email address.

Return Value

itself, IEMail to allow fluent design.


Subject(System.String)

Parameters
subject

the subject line Set the title of email message.

Return Value

return its self


Body(System.String)

Parameters
body

Email body Set the body of the email message.

Return Value

return its self


Html(System.Boolean)

Parameters
isBodyHtml

true ot false, is body Html Is the mail message body Html

Return Value

itself, IEMail to allow fluent design.


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

Parameters
buffer

byte buffer containing the attachment

displayName

name placed on the attachment

mimeType

mime type like 'application/pdf' Add an attachment to the mail message. For example: This sample shows how to call this constructor.

  IEMail _mail = new EMail("testfrom@example.com", "testto@example.com", "Sending email is Fun", "This is a test email.")
      .Attachment(_textBuffer, _textFile, _textMimeType).Send();
Return Value

return its self


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


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


GetMailMessage()

Get the current MailMessage

Return Value

the current MailMessage


Logs(System.Int32)

Parameters
lastCount

Count of last log records to return return the last few log messages...

Return Value

list of strings


Emails()

Output the list of string of the last max emails sent

Return Value

list of strings


CleanUp()

Naturally and automatically dispose of mail-message resource.


EmailToString()

Format a string of the current mail message

Return Value

formated string of a mail message


ToEmailAddress(System.Net.Mail.MailAddress)

Parameters
mailAddress

SMTP MailAddress Format and SMTP MailAddress structure to a string

Return Value

string like Phil Huhn PHuhn@yahoo.com


SendList()

Write the current email to a list of string


Properties

MailgunUrl

Property to override the default MailGun URL.

ToMailGun()

Translate from MailMessage to MultipartFormDataContent.

Return Value

MultipartFormDataContent (see unit test)


SendMailGunAsync(System.String,System.String)

Parameters
apiKey

The MailGun api key.

domainName

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

Return Value

itself, IEMail to allow fluent design.


ConvertToEmailAddress(System.Net.Mail.MailAddress)

Parameters
address

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

Return Value

a new SendGrid EmailAddress address


ConvertToMailAddress(SendGrid.Helpers.Mail.EmailAddress)

Parameters
address

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

Return Value

a new MS SMTP MailAddress address


ToSendGrid()

Translate from MailMessage to SendGridMessage.

Return Value

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


NewMailMessage(SendGrid.Helpers.Mail.SendGridMessage)

Parameters
sgm

a SendGridMessage mail message from SendGrid.Helpers.Mail Convert a SendGridMessage mail message, and load it into a new message. For example: 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();
Return Value

itself, IEMail to allow fluent design.


SendSendGridAsync(System.String)

Parameters
apiKey

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

Return Value

itself, IEMail to allow fluent design.


SendSmtp()

Send the email and any attachments, via the SMTP.

Return Value

itself, IEMail to allow fluent design.


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 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.

Methods

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

Parameters
fromAddress

From email address

toAddress

To email address

subject

Email subject line

message

Email body Instantiate the SMTP MailMessage class using the four parameters.

Return Value

IEMail to allow fluent design.


NewMailMessage()

Instantiate the SMTP MailMessage class without parameters.

Return Value

IEMail to allow fluent design.


NewMailMessage(SendGrid.Helpers.Mail.SendGridMessage)

Parameters
sgm

a SendGridMessage mail message Load a SendGridMessage mail message into this message For example: 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();
Return Value

IEMail to allow fluent design.


GetMailMessage()

Get the current MailMessage

Return Value

the current MailMessage


From(System.String)

Parameters
fromAddress

an email address Set the single from email address

Return Value

itself, IEMail to allow fluent design.


From(System.String,System.String)

Parameters
fromAddress

an email address

name

display name of the email address Set the single from email address

Return Value

itself, IEMail to allow fluent design.


From(System.Net.Mail.MailAddress)

Parameters
fromAddress

A MailAddress, including an email address and the name Set the single from email address

Return Value

itself, IEMail to allow fluent design.


To(System.String)

Parameters
toAddress

an email address Add a to email address.

Return Value

itself, IEMail to allow fluent design.


To(System.String,System.String)

Parameters
toAddress

an email address

name

display name of the email address Add a to email address.

Return Value

itself, IEMail to allow fluent design.


To(System.Net.Mail.MailAddress)

Parameters
toAddress

A MailAddress, including an email address and the name Add a to email address.

Return Value

itself, IEMail to allow fluent design.


CC(System.String)

Parameters
ccAddress

an email address Add a carbon copy (cc) email address

Return Value

itself, IEMail to allow fluent design.


CC(System.String,System.String)

Parameters
ccAddress

an email address

name

display name of the email address Add a carbon copy (cc) email address

Return Value

itself, IEMail to allow fluent design.


CC(System.Net.Mail.MailAddress)

Parameters
ccAddress

A MailAddress, including an email address and the name Add a carbon copy (cc) email address

Return Value

itself, IEMail to allow fluent design.


BCC(System.String)

Parameters
bccAddress

an email address Add a blind carbon copy (bcc) email address

Return Value

itself, IEMail to allow fluent design.


BCC(System.String,System.String)

Parameters
bccAddress

an email address

name

display name of the email address Add a blind carbon copy (bcc) email address

Return Value

itself, IEMail to allow fluent design.


BCC(System.Net.Mail.MailAddress)

Parameters
bccAddress

A MailAddress, including an email address and the name Add a blind carbon copy (bcc) email address

Return Value

itself, IEMail to allow fluent design.


Subject(System.String)

Parameters
subject

Email subject line Set the email subject line

Return Value

itself, IEMail to allow fluent design.


Body(System.String)

Parameters
body

Email body Set the email body/text.

Return Value

itself, IEMail to allow fluent design.


Html(System.Boolean)

Parameters
isBodyHtml

true ot false, is body Html Is the mail message body Html

Return Value

itself, IEMail to allow fluent design.


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

Parameters
buffer

byte array of the file to attach

displayName

file name of the attachment

mimeType

mime type of the attachment Set an email attachment.

Return Value

itself, IEMail to allow fluent design.


Send()

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

  • Email:UseService.
Return Value

itself, IEMail to allow fluent design.


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


SendSmtp()

Send the email and any attachments, via the SMTP.

Return Value

itself, IEMail to allow fluent design.


SendSmtpAsync()

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

Return Value

itself, IEMail to allow fluent design.


ToSendGrid()

Translate from MailMessage to SendGridMessage.

Return Value

SendGridMessage


SendSendGridAsync(System.String)

Parameters
apiKey

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

Return Value

itself, IEMail to allow fluent design.


Properties

MailgunUrl

Property to override the default MailGun URL.

ToMailGun()

Translate from MailMessage to MultipartFormDataContent.

Return Value

MultipartFormDataContent (see unit test)


SendMailGunAsync(System.String,System.String)

Parameters
apiKey

The MailGun api key.

domainName

Your domain defined with MailGun. Send the email and any attachments, via the MailGun async task.

            Note: this was not tested (did not have an account).
Return Value

itself, IEMail to allow fluent design.


Logs(System.Int32)

Parameters
lastCount

Count of last log records to return retrurn the last few log messages...

Return Value

list of strings Remarks: Output from ILogger


Emails()

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

Return Value

list of strings

Clone this wiki locally