Class MailMessageExtensions
- Namespace
- Wolfgang.Extensions.Mail
- Assembly
- Wolfgang.Extensions.Mail.dll
A collection of extension methods for the MailMessage class.
public static class MailMessageExtensions
- Inheritance
-
MailMessageExtensions
- Inherited Members
Methods
Clone(MailMessage)
Creates a deep copy of the MailMessage. All properties, address collections, attachments, alternate views, and linked resources are independently copied. The returned message can be modified and disposed independently of the original.
public static MailMessage Clone(this MailMessage source)
Parameters
sourceMailMessageThe MailMessage to clone.
Returns
- MailMessage
A new MailMessage that is an independent deep copy of
source.
Examples
using var original = new MailMessage("from@example.com", "to@example.com");
original.Subject = "Hello";
using var clone = original.Clone();
clone.To.Add("extra@example.com");
// original.To is unchanged
Exceptions
- ArgumentNullException
sourceis null.
ToMimeString(MailMessage)
Serializes the MailMessage to a raw MIME/EML format string.
The output conforms to RFC 2822 and can be saved as a .eml file.
[RequiresUnreferencedCode("ToMimeString uses reflection to reach the internal System.Net.Mail.MailWriter type and MailMessage.Send; the trimmer may remove those members.")]
[RequiresDynamicCode("ToMimeString relies on reflection-based method invocation that Native AOT cannot compile ahead of time.")]
public static string ToMimeString(this MailMessage source)
Parameters
sourceMailMessageThe MailMessage to serialize.
Returns
- string
A string containing the complete MIME representation of the message.
Examples
using var message = new MailMessage("from@example.com", "to@example.com");
message.Subject = "Test";
message.Body = "Hello, World!";
string eml = message.ToMimeString();
Exceptions
- ArgumentNullException
sourceis null.- InvalidOperationException
The From property is not set, or the runtime does not support the internal MIME serialization API.
Validate(MailMessage)
Validates the MailMessage and returns a ValidationResult containing any errors or warnings found. This method is pure and does not modify the message.
public static ValidationResult Validate(this MailMessage source)
Parameters
sourceMailMessageThe MailMessage to validate.
Returns
- ValidationResult
A ValidationResult indicating whether the message is valid.
Examples
using var message = new MailMessage();
var result = message.Validate();
if (!result.IsValid)
{
foreach (var error in result.Errors)
Console.WriteLine(error);
}
Exceptions
- ArgumentNullException
sourceis null.
Validate(MailMessage, ValidationOptions?)
Validates the MailMessage using the specified options and returns a ValidationResult containing any errors or warnings found. This method is pure and does not modify the message.
public static ValidationResult Validate(this MailMessage source, ValidationOptions? options)
Parameters
sourceMailMessageThe MailMessage to validate.
optionsValidationOptionsOptional validation configuration. Pass
nullfor default behavior.
Returns
- ValidationResult
A ValidationResult indicating whether the message is valid.
Exceptions
- ArgumentNullException
sourceis null.