Click or drag to resize
MimeKit

SmtpDataFilter Class

An SMTP filter designed to format a message stream for the DATA command.
Inheritance Hierarchy
SystemObject
  MimeKit.IO.FiltersMimeFilterBase
    MailKit.Net.SmtpSmtpDataFilter

Namespace: MailKit.Net.Smtp
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
public class SmtpDataFilter : MimeFilterBase

The SmtpDataFilter type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleSmtpDataFilter Initializes a new instance of the SmtpDataFilter class.
Top
Properties
 NameDescription
Protected propertyOutputBuffer Get the output buffer.
(Inherited from MimeFilterBase)
Top
Methods
 NameDescription
Protected methodEnsureOutputSize Ensure that the output buffer is greater than or equal to the specified size.
(Inherited from MimeFilterBase)
Public methodEquals
(Inherited from Object)
Public methodFilter(Byte, Int32, Int32, Int32, Int32) Filter the specified input.
(Inherited from MimeFilterBase)
Protected methodFilter(Byte, Int32, Int32, Int32, Int32, Boolean) Filter the specified input.
(Overrides MimeFilterBaseFilter(Byte, Int32, Int32, Int32, Int32, Boolean))
Protected methodFinalize
(Inherited from Object)
Public methodFlush Filter the specified input, flushing all internally buffered data to the output.
(Inherited from MimeFilterBase)
Public methodGetHashCode
(Inherited from Object)
Public methodGetType
(Inherited from Object)
Protected methodMemberwiseClone
(Inherited from Object)
Public methodReset Reset the filter.
(Overrides MimeFilterBaseReset)
Protected methodSaveRemainingInput Save the remaining input for the next round of processing.
(Inherited from MimeFilterBase)
Public methodToString
(Inherited from Object)
Top
Remarks
A special stream filter that can encode or decode lines beginning with a '.' as needed when sending/receiving a message via the SMTP protocol or when saving a message to an IIS message pickup directory.
Example
C#
public static void SaveToPickupDirectory (MimeMessage message, string pickupDirectory)
{
    do {
        // Generate a random file name to save the message to.
        var path = Path.Combine (pickupDirectory, Guid.NewGuid ().ToString () + ".eml");
        Stream stream;

        try {
            // Attempt to create the new file.
            stream = File.Open (path, FileMode.CreateNew);
        } catch (IOException) {
            // If the file already exists, try again with a new Guid.
            if (File.Exists (path))
                continue;

            // Otherwise, fail immediately since it probably means that there is
            // no graceful way to recover from this error.
            throw;
        }

        try {
            using (stream) {
                // IIS pickup directories expect the message to be "byte-stuffed"
                // which means that lines beginning with "." need to be escaped
                // by adding an extra "." to the beginning of the line.
                // 
                // Use an SmtpDataFilter to "byte-stuff" the message as it is written
                // to the file stream. This is the same process that an SmtpClient
                // would use when sending the message in a `DATA` command.
                using (var filtered = new FilteredStream (stream)) {
                    filtered.Add (new SmtpDataFilter ());

                    // Make sure to write the message in DOS (<CR><LF>) format.
                    var options = FormatOptions.Default.Clone ();
                    options.NewLineFormat = NewLineFormat.Dos;

                    message.WriteTo (options, filtered);
                    filtered.Flush ();
                    return;
                }
            }
        } catch {
            // An exception here probably means that the disk is full.
            // 
            // Delete the file that was created above so that incomplete files are not
            // left behind for IIS to send accidentally.
            File.Delete (path);
            throw;
        }
    } while (true);
}
C#
public static MimeMessage LoadFromPickupDirectory (string fileName)
{
    using (var stream = File.OpenRead (fileName)) {
        // IIS pickup directories store messages that have been "byte-stuffed"
        // which means that lines beginning with "." have been escaped by
        // adding an extra "." to the beginning of the line.
        // 
        // Use an SmtpDataFilter to decode the message as it is loaded from
        // the file stream. This is the reverse process that an SmtpClient
        // would use when sending the message in a `DATA` command.
        using (var filtered = new FilteredStream (stream)) {
            filtered.Add (new SmtpDataFilter (decode: true));

            return MimeMessage.Load (filtered);
        }
    }
}
See Also