SmtpAddJob Method (String, String, Boolean, String, EmailAddressCollection)
Puts the e-mail message stored in an .EML file onto waiting list for subsequent processing in bulk mode.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public void AddJob(
	string tag,
	string msgFilename,
	bool preferXSenderXReceiver,
	string senderEmail,
	EmailAddressCollection recipients
)

Parameters

tag
Type: SystemString
Any string the developer wants to assign to Tag property of SendMailJob object created by this method. The developer can leave it a null reference (Nothing in Visual Basic).
msgFilename
Type: SystemString
The filename of the .EML file containing the e-mail message to be sent.
preferXSenderXReceiver
Type: SystemBoolean
Denotes if "X-Sender" and "X-Receiver" message headers have priority over any other method of specifying sender and recipients for this message. Has no effect if "X-Sender" and "X-Receiver" headers are not set in the message file.
senderEmail
Type: SystemString
The e-mail address of the sender. If it's a null reference (Nothing in Visual Basic), the e-mail address is taken from From property. Ignored if preferXSenderXReceiver is true and the message includes "X-Sender" header.
recipients
Type: MailBee.MimeEmailAddressCollection
The list of the message recipients. If it's a null reference (Nothing in Visual Basic), the recipients list is combined from To, Cc, and Bcc lists. Ignored if preferXSenderXReceiver is true and the message includes at least one "X-Receiver" header.
Remarks

This method is useful if you want to implement some kind of background mail queue. E-mails themselves can be created using other tools or with SubmitToPickupFolder(String, String, String, EmailAddressCollection, Boolean) method. SubmitToPickupFolder(String, String, String, EmailAddressCollection, Boolean) method can create .EML files with "X-Sender" and "X-Receiver" headers so that it's possible to implement scenarios when an e-mail needs to be sent from/to addresses different from its From/To/CC/BCC.

Note Note
This overload is different from AddJob(String, MailMessage, String, EmailAddressCollection) (it accepts MailMessage as a parameter) in the way it uses memory. The current overload (which takes just a filename of the e-mail message) does NOT internally call LoadMessage(String) to get the message in memory from the file and put it onto the waiting queue as a job. Instead, it loads the message into memory only when the job actually gets processed. Thus, if you added 100 e-mails as MailMessage objects, all 100 will consume memory before they start to get sent. However, if you added 100 e-mails as files, the job list will contain just 100 filenames, not actual messages. The messages will be loaded and processed one-by-one and memory usage will be substantially lower. Of course, if you're using multi-threaded processing jobs, multiple messages will be processed simultaneously (and thus occupy more memory) but you'll still have significant memory usage benefit if number of threads is less than the number of messages in the queue.

The note above does not apply to mail merge over database jobs. In that case, each e-mail is generated on the fly when it gets sent (not when the mail merge job is created). E-mails generated with mail merge over database do not occupy more memory than required in order to send out a single e-mail (or several e-mails in case of multi-threading).

Examples

This sample implements a very simple e-mail queue dispatcher which drops a couple of e-mail files in a folder first, and then sends out all e-mail files in a folder. E-mails are save to and then taken from "C:\Queue\Pickup" folder. For successful e-mails, .EML file is then removed, for failed e-mails - moved to "C:\Queue\Bad" folder.

Note Note
For a more advanced sample which can be used as a production level queue dispatcher, download MailBee.NET Queue from AfterLogic web site.
using System;
using System.IO;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;

class Sample
{
    // Reports readiness to start sending an e-mail.
    static void mailer_SendingMessage(object sender, SmtpSendingMessageEventArgs e)
    {
        // Display e-mail address of the e-mail which is about to be sent.
        Console.WriteLine(e.MailMessage.Filename + " WILL BE SENT");
    }

    // Reports successful attempt of sending e-mail.
    static void mailer_MessageSent(object sender, SmtpMessageSentEventArgs e)
    {
        // Display e-mail address of the successful e-mail.
        Console.WriteLine(e.MailMessage.Filename + " SUCCEEDED");
        File.Delete(e.MailMessage.Filename);
    }

    // Reports failed attempt of sending e-mail.
    static void mailer_MessageNotSent(object sender, SmtpMessageNotSentEventArgs e)
    {
        // Display e-mail address of the failed e-mail.
        Console.WriteLine(e.MailMessage.Filename + " FAILED");
        File.Move(e.MailMessage.Filename, Path.Combine(BadFolder, Path.GetFileName(e.MailMessage.Filename)));
    }

    const string PickupFolder = @"C:\Queue\Pickup";
    const string BadFolder = @"C:\Queue\Bad";

    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Logging into a file is useful for troubleshooting.
        mailer.Log.Filename = @"C:\Temp\log.txt";
        mailer.Log.Enabled = true;
        mailer.Log.Format = LogFormatOptions.AddContextInfo;
        mailer.Log.Clear();

        // For demo purposes, submit a couple of e-mails so that we would have something to send out.
        // Show diffenent cases. First message is a bit advanced, it has different From and "real From"
        // (which is usually used to send bounces to a special address). Second one is simpler.
        mailer.Message.From.AsString = "John Doe <jdoe@domain.com>";
        mailer.Message.To.AsString = "Alice <alice@server.com>";
        mailer.Message.Subject = "Subject #1";
        mailer.Message.BodyPlainText = "Text #1";
        mailer.SubmitToPickupFolder(PickupFolder, null, "bounce@domain.com", (string)null, false);

        mailer.Message.From.AsString = "John Doe <jdoe@domain.com>";
        mailer.Message.To.AsString = "Bob <bob@domain.com>, Bill Smith <bill@company.com>";
        mailer.Message.Subject = "Subject #2";
        mailer.Message.BodyPlainText = "Text #2";
        mailer.SubmitToPickupFolder(PickupFolder, false);

        // Uncomment the line below to use unlimited number of worker threads (up to 60)
        // and increase performance. Note that not all SMTP servers support many incoming connections
        // from a single IP address (each worker thread may create its own connection).

        // mailer.MaxThreadCount = -1;

        // Subscribe to events to track send bulk mail progress.
        mailer.SendingMessage += new SmtpSendingMessageEventHandler(mailer_SendingMessage);
        mailer.MessageSent += new SmtpMessageSentEventHandler(mailer_MessageSent);
        mailer.MessageNotSent += new SmtpMessageNotSentEventHandler(mailer_MessageNotSent);

        // Setup SMTP server parameters.
        mailer.SmtpServers.Add("mail.domain.com", "jdoe@domain.com", "secret");

        // Note that by default if a message is intended to multiple recipients
        // (message #2 in this sample) and at least one of them succeeds, the entire
        // message succeeds. To change this behavior, uncomment the line below.

        // mailer.SmtpServers[0].AllowRefusedRecipients = false;

        // Uncomment next line if ESMTP CHUNKING causes problems with your server (usually, with MS Exchange).
        // mailer.SmtpServers[0].SmtpOptions = ExtendedSmtpOptions.NoChunking;

        DirectoryInfo dirInfo = new DirectoryInfo(PickupFolder);
        FileInfo[] fileInfos = dirInfo.GetFiles("*.eml");
        foreach (FileInfo info in fileInfos)
        {
            // Remember the filename but don't actually load the message file at the moment.
            // If the message file overrides sender and recipients in From/To/CC/BCC by having X-Sender or
            // X-Receiver in the headers, honor this by setting preferXSenderXReceiver to true.
            mailer.AddJob(null, info.FullName, true, null, null);
        }

        // Dispatch the queue.
        mailer.SendJobs();
    }
}
See Also