MailMessage.Load

Load(string)

Loads a message from a file. MSG is recognized automatically; any other file is read as EML.

public static MailMessage Load(string fileName)
ParameterTypeDescription
fileNameStringThe path of the file.

Return Value

The loaded message.

Exceptions

exceptioncondition
ArgumentNullExceptionfileName is null or empty.
FormatNotSupportedExceptionThe format of the data is not supported.

See Also


Load(Stream)

Loads a message from a stream. MSG is recognized automatically; any other data is read as EML.

public static MailMessage Load(Stream stream)
ParameterTypeDescription
streamStreamThe stream to read.

Return Value

The loaded message.

Exceptions

exceptioncondition
ArgumentNullExceptionstream is null.
NotSupportedExceptionstream does not support reading.
FormatNotSupportedExceptionThe format of the data is not supported.

See Also


Load(string, LoadOptions)

Loads a message from a file in the format selected by the options.

public static MailMessage Load(string fileName, LoadOptions options)
ParameterTypeDescription
fileNameStringThe path of the file.
optionsLoadOptionsThe options; their class selects the format, such as EmlLoadOptions or MsgLoadOptions.

Return Value

The loaded message.

Exceptions

exceptioncondition
ArgumentNullExceptionfileName is null.
FormatNotSupportedExceptionThe format of the data is not supported.

Examples

The following example shows how to load a Message with Load Options.

[C#]

// Load Eml, html, mhtml, msg and dat file 
MailMessage.Load("Message.eml", new EmlLoadOptions());
MailMessage.Load("description.html", new HtmlLoadOptions());
MailMessage.Load("Message.mhtml", new MhtmlLoadOptions());
MailMessage.Load("Message.msg", new MsgLoadOptions());

// loading with custom options
var emlLoadOptions = new EmlLoadOptions
{
    PreferredTextEncoding = Encoding.UTF8,
    PreserveTnefAttachments = true
};

MailMessage.Load("description.html", emlLoadOptions);

var htmlLoadOptions = new HtmlLoadOptions
{
    PreferredTextEncoding = Encoding.UTF8,
    ShouldAddPlainTextView = true,
    PathToResources = htmlImagesFolder
};

MailMessage.Load("description.html", htmlLoadOptions);

[Visual Basic]

' Load Eml, html, mhtml, msg and dat file
Dim mailMessage As MailMessage = MailMessage.Load("Message.eml", New EmlLoadOptions())
MailMessage.Load("description.html", New HtmlLoadOptions())
MailMessage.Load("Message.mhtml", New MhtmlLoadOptions())
MailMessage.Load("Message.msg", New MsgLoadOptions())

' loading with custom options
Dim emlLoadOptions As EmlLoadOptions = New EmlLoadOptions With {
  .PrefferedTextEncoding = Encoding.UTF8,
	.PreserveTnefAttachments = True
}

MailMessage.Load("description.html", emlLoadOptions)

Dim htmlLoadOptions As HtmlLoadOptions = New HtmlLoadOptions With {
	.PrefferedTextEncoding = Encoding.UTF8,
		.ShouldAddPlainTextView = True,
			.PathToResources = htmlImagesFolder
}
				
MailMessage.Load("description.html", emlLoadOptions)

See Also


Load(Stream, LoadOptions)

Loads a message from a stream in the format selected by the options.

public static MailMessage Load(Stream stream, LoadOptions options)
ParameterTypeDescription
streamStreamThe stream to read.
optionsLoadOptionsThe options; their class selects the format, such as EmlLoadOptions or MsgLoadOptions.

Return Value

The loaded message.

Exceptions

exceptioncondition
ArgumentNullExceptionstream is null.
FormatNotSupportedExceptionThe format of the data is not supported.

See Also