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
messageMailMessageThe mail message to add the HTML body to.
bodyHtmlstringThe HTML string representing the body content.
Returns
- MailMessage
The updated MailMessage instance.
Exceptions
- ArgumentNullException
Thrown if
messageis 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
messageMailMessageThe mail message to add the plain text body to.
bodyPlainstringThe plain text string representing the body content.
Returns
- MailMessage
The updated MailMessage instance.
Exceptions
- ArgumentNullException
Thrown if
messageis 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
messageMailMessageThe mail message to attach the file to.
fileNamestringThe name of the file to attach.
fileBodyStreamThe stream that represents the file content.
Returns
- MailMessage
The updated MailMessage instance.
Exceptions
- ArgumentNullException
Thrown if
messageis 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
attachmentFileStreamThe stream containing the attachment file.
displayNamestringThe display name for the attachment.
transferEncodingTransferEncodingThe 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
transferEncodingis not supported.
IsEmailValid(string)
Validates whether the specified email address string is in a correct format.
public static bool IsEmailValid(this string email)
Parameters
emailstringThe email address string to validate.
Returns
- bool
trueif 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
messageMailMessageThe mail message to send.
disposeboolIf set to
true, disposes the mail message after sending.
Exceptions
- ArgumentNullException
Thrown if
messageis null.
SendAsync(MailMessage, CancellationToken)
Sends the specified MailMessage asynchronously.
public static Task SendAsync(this MailMessage message, CancellationToken cancellationToken = default)
Parameters
messageMailMessageThe mail message to send.
cancellationTokenCancellationTokenA cancellation token to cancel the asynchronous send operation.
Returns
- Task
A task that represents the asynchronous send operation.
Exceptions
- ArgumentNullException
Thrown if
messageis 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
fileNamestringThe name of the file for the attachment.
fileBodyStreamThe stream representing the attachment content.
Returns
- Attachment
A new instance of Attachment.