Table of Contents

Class MailHelper

Namespace
Ecng.Net
Assembly
Ecng.Net.dll

Provides helper methods to send emails and to manage mail attachments.

public static class MailHelper
Inheritance
MailHelper
Inherited Members

Methods

AddHtml(MailMessage, string)

Adds an HTML body alternate view to the specified MailMessage.

public static MailMessage AddHtml(this MailMessage message, string bodyHtml)

Parameters

message MailMessage

The mail message to add the HTML body to.

bodyHtml string

The HTML string representing the body content.

Returns

MailMessage

The updated MailMessage instance.

Exceptions

ArgumentNullException

Thrown if message is null.

AddPlain(MailMessage, string)

Adds a plain text body alternate view to the specified MailMessage.

public static MailMessage AddPlain(this MailMessage message, string bodyPlain)

Parameters

message MailMessage

The mail message to add the plain text body to.

bodyPlain string

The plain text string representing the body content.

Returns

MailMessage

The updated MailMessage instance.

Exceptions

ArgumentNullException

Thrown if message is null.

Attach(MailMessage, string, Stream)

Attaches a file to the specified MailMessage using the provided stream.

public static MailMessage Attach(this MailMessage message, string fileName, Stream fileBody)

Parameters

message MailMessage

The mail message to attach the file to.

fileName string

The name of the file to attach.

fileBody Stream

The stream that represents the file content.

Returns

MailMessage

The updated MailMessage instance.

Exceptions

ArgumentNullException

Thrown if message is null.

CreateAttachment(Stream, string, TransferEncoding)

Creates an Attachment from the provided stream and display name, with specified transfer encoding.

public static Attachment CreateAttachment(Stream attachmentFile, string displayName, TransferEncoding transferEncoding = TransferEncoding.Base64)

Parameters

attachmentFile Stream

The stream containing the attachment file.

displayName string

The display name for the attachment.

transferEncoding TransferEncoding

The transfer encoding to use. Defaults to Base64.

Returns

Attachment

A new instance of Attachment configured with the provided parameters.

Exceptions

ArgumentOutOfRangeException

Thrown if the specified transferEncoding is not supported.

IsEmailValid(string)

Validates whether the specified email address string is in a correct format.

public static bool IsEmailValid(this string email)

Parameters

email string

The email address string to validate.

Returns

bool

true if the email format is valid; otherwise, false.

Send(MailMessage, bool)

Sends the specified MailMessage synchronously.

public static void Send(this MailMessage message, bool dispose = true)

Parameters

message MailMessage

The mail message to send.

dispose bool

If set to true, disposes the mail message after sending.

Exceptions

ArgumentNullException

Thrown if message is null.

SendAsync(MailMessage, CancellationToken)

Sends the specified MailMessage asynchronously.

public static Task SendAsync(this MailMessage message, CancellationToken cancellationToken = default)

Parameters

message MailMessage

The mail message to send.

cancellationToken CancellationToken

A cancellation token to cancel the asynchronous send operation.

Returns

Task

A task that represents the asynchronous send operation.

Exceptions

ArgumentNullException

Thrown if message is null.

ToAttachment(string, Stream)

Creates a new Attachment from the specified file name and file stream.

public static Attachment ToAttachment(string fileName, Stream fileBody)

Parameters

fileName string

The name of the file for the attachment.

fileBody Stream

The stream representing the attachment content.

Returns

Attachment

A new instance of Attachment.