SmtpSocketCreating Event
Occurs when MailBee needs a new socket object for establishing a connection with the SMTP server.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
public event SocketCreatingEventHandler SocketCreating


Type: MailBeeSocketCreatingEventHandler

This event is raised just before MailBee will start making the connection (i.e. before SocketConnected event). This lets you specify your own socket object in case if you need some special socket settings. If you need this, set NewSocket property to the socket object needed in your event handler.

This event fires for mail server connections only. Connections with DNS servers (such as DNS MX lookup during direct send) don't trigger this event.

Note Note
It's possible that this event will fire multiple times in case if the host has multple endpoints assigned. For instance, if the first endpoint was IPv6, the connection could not be made but second endpoint (IPv4) would succeed. On each call of this event handler, your code must supply a new, fresh socket object (otherwise MailBee will create its own). By default, however, MailBee tries IPv4 addresses first (see PreferIPv4Hosts) so this situation is unlikely to happen. Another case would be if you have multiple SMTP servers in SmtpServers collection, the first server failed and fail-over server is tried. Mass-mailing, direct-send and POP-before-SMTP auth scenarios can also cause multiple connections to SMTP servers occur (in case of POP-before-SMTP, to POP3 server as well). This results in firing this event multiple times as well.

Imap.SocketCreating topic contains a code snippet which shows the idea how to use this event in UWP apps to implement support of self-signed certificates.

The code snippet below shows how to assign a local end point for an SMTP connection in classic .NET apps (for instance, for direct send). Although you can specify local IP address by setting mailer.DirectSendDefaults.LocalEndPoint (mailer is Smtp instance), you may want to set different local IPs for individual sends during the same mass mailing operation.

using System;
using System.Net;
using System.Net.Sockets;
using MailBee;
using MailBee.SmtpMail;

class Sample
    static void mailer_SocketCreating(object sender, SocketCreatingEventArgs e)
        e.NewSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        // In real app, you can select a particular IP address from a pool of your local IP addresses on random or round-robin basis.
        e.NewSocket.Bind(new IPEndPoint(IPAddress.Parse(""), 0));

    // The actual code.
    static void Main(string[] args)
        Smtp mailer = new Smtp();

        // Subscribe to the SocketCreating event.
        mailer.SocketCreating += new SocketCreatingEventHandler(mailer_SocketCreating);

        // Here you'll add multiple email jobs with .AddJobs method.

See Also