Class MailHelper
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
MailMessageThe mail message to add the HTML body to.
bodyHtml
stringThe 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
MailMessageThe mail message to add the plain text body to.
bodyPlain
stringThe 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
MailMessageThe mail message to attach the file to.
fileName
stringThe name of the file to attach.
fileBody
StreamThe 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
StreamThe stream containing the attachment file.
displayName
stringThe display name for the attachment.
transferEncoding
TransferEncodingThe 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
stringThe 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
MailMessageThe mail message to send.
dispose
boolIf 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
MailMessageThe mail message to send.
cancellationToken
CancellationTokenA 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
stringThe name of the file for the attachment.
fileBody
StreamThe stream representing the attachment content.
Returns
- Attachment
A new instance of Attachment.