Skip to content

Settings

axunonb edited this page Aug 15, 2017 · 7 revisions

Settings of MailMergeLib consist of

  • The crypto key to encrypt credentials when settings are serialized
  • The SenderConfig - configuration of the MailMergeSender
    • One or more SmtpClientConfigs to allow for fail-over from one to another SmtpClientConfig, e.g. in case an SMTP server is out of operation
  • The MessageConfig - the settings for MailMergeMessages

All settings can be serialized and deserialized to a string, a stream or to the file system.

Usually settings are rarely subject to changes across mail messages. Therefore it's a good strategy to keep all of them in one config file:

var s = new Settings();
// set properties as needed
s.Serialize("path-to-file.config");

Later you can restore the settings:

var s = Settings.Deserialize("path-to-file.config");
var mms = new MailMergeSender {Config = s.SenderConfig};
var mmm = new MailMergeMessage {Config = s.MessageConfig};

When serializing a MailMergeMessage, the Config will be saved (and restored) as well. So you have to overwrite it with your general settings:

var mmm = new MailMergeMessage {Config = s.MessageConfig};
mmm.Serialize("path-to-message-file.xml");
// more stuff
mmm = MailMergeMessage.Deserialize("path-to-message-file.xml");
mmm.Config = Settings.Deserialize("path-to-file.config").MessageConfig;

Here is a complete overview of all configuration settings:

Settings.CryptoKey = "SomeSecretCryptoKey"; // Change this property!
var settings = new Settings
{
    MessageConfig =
    {
        CharacterEncoding = Encoding.UTF8,
        StandardFromAddress = new MailboxAddress("sender name", "sender@example.com"),
        CultureInfo = new CultureInfo("en-US"),
        IgnoreIllegalRecipientAddresses = true,
        IgnoreMissingInlineAttachments = false,
        IgnoreMissingFileAttachments = false,
        Organization = "axuno gGmbH",
        Priority = MessagePriority.Normal,
        SmartFormatterConfig =
        {
            FormatErrorAction = ErrorAction.ThrowError,
            ParseErrorAction = ErrorAction.ThrowError,
            CaseSensitivity = CaseSensitivityType.CaseSensitive,
            ConvertCharacterStringLiterals = true
        },
        Xmailer = "MailMergeLib 5",
        // The directory where attachments or images in HTML content are stored
        FileBaseDirectory = "Path-to-Base-Dir",
        TextTransferEncoding = ContentEncoding.QuotedPrintable,
        BinaryTransferEncoding = ContentEncoding.Base64
    },
    SenderConfig =
    {
        SmtpClientConfig = new[]
        {
            new SmtpClientConfig()
            {
                Name = "Best",
                MessageOutput = MessageOutput.SmtpServer,
                // depends on SMTP server configuration
                SmtpHost = "some.host.com",
                SmtpPort = 587,
                // depends on SMTP server configuration
                NetworkCredential = new Credential("user", "password"),
                // after this number of trials per message sending will be aborted
                MaxFailures = 3,
                // time to wait for the next trial after a failure
                RetryDelayTime = 500,
                // some SMTP servers will block otherwise:
                DelayBetweenMessages = 100,
                // depends on SMTP server configuration
                SecureSocketOptions = SecureSocketOptions.StartTlsWhenAvailable,
                // set when you have several TCP/IP adapters, null for the default
                LocalEndPoint = new IPEndPoint(192.168.100.123, 8000),
                // depends on SMTP server configuration
                SslProtocols = SslProtocols.Ssl3,
                // client domain name used in the initial SMTP protocol request to connect to an SMTP server:
                ClientDomain = "mail.sender.domain",
                // timeout for a single message
                Timeout = 1800
            },
            new SmtpClientConfig()
            {
                Name = "Next best",
                MessageOutput = MessageOutput.SmtpServer,
                SmtpHost = "some.otherhost.com",
                SmtpPort = 25,
                NetworkCredential = new Credential("user2", "password2"),
                DelayBetweenMessages = 100
            },
            new SmtpClientConfig()
            {
                Name = "Save to files",
                // if all SMTP configurations fail, store messages to file system
                MessageOutput = MessageOutput.Directory,
                MailOutputDirectory = "Path-to-target-directory",
            }
        },
        // number of SMTP clients used when invoking async send methods
        MaxNumOfSmtpClients = 5
    }
};