Rebex Secure Mail

SMTP, IMAP, EWS, POP3, S/MIME .NET library

Download 30-day free trial Buy from $299
More .NET libraries

Back to feature list...

SMTP - sending emails

Sending with a single line of code 

The easiest way to send an e-mail message is this:

// send mail
Rebex.Net.Smtp.Send("from@example.org", "to@example.org", "Subject", "Body", "smtp.example.org");
' send mail
Rebex.Net.Smtp.Send("from@example.org", "to@example.org", "Subject", "Body", "smtp.example.org")

If you need to send a more complex message than this, or if your server requires authentication, it's simple as well. Just construct an instance of Rebex.Mail.MailMessage (see MIME and MailMessage), and configure the SMTP server and its credentials in your application's configuration file.

// send mail, use SMTP configuration from Application config file
Rebex.Net.Smtp.Send(mail, SmtpConfiguration.Default);
' send mail, use SMTP configuration from Application config file
Rebex.Net.Smtp.Send(mail, SmtpConfiguration.Default)

Your application's configuration file (Web.config in case of ASP.NET web apps) might look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtp.example.org" enableSsl="true"
          userName="username" password="password" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Sending a mail message 

If you need more control about your SMTP session, or if simply you prefer your code to consist of multiple steps to ease debugging, create an instance of Smtp object first, then connect and authenticate before sending the message:

// create SMTP client, connect, log in
// ...

// create new mail message
var mail = new Rebex.Mail.MailMessage();
mail.From = "joe@example.org";
mail.To = "john@example.org";
mail.Subject = "Test";
mail.BodyText = "Hello World!";
mail.BodyHtml = "<strong>Hello World!</strong>";

// send mail
smtp.Send(mail);
' create SMTP client, connect, log in
' ...

' create new mail message
Dim mail = New Rebex.Mail.MailMessage()
mail.From = "joe@example.org"
mail.To = "john@example.org"
mail.Subject = "Test"
mail.BodyText = "Hello World!"
mail.BodyHtml = "<strong>Hello World!</strong>"

' send mail
smtp.Send(mail)

When sending multiple e-mail messages, you can save lot of overhead by sending all of them in a single SMTP session.

Sending from stream 

To send a message in MIME format from a stream, use the Smtp.Send method:

// create SMTP client instance, connect, log in
// ...

// open file in MIME format
using (FileStream input = File.OpenRead(@"C:\MyData\mail.eml"))
{
    // send mail from stream
    smtp.Send(input);
}
' create SMTP client instance, connect, log in
' ...

' open file in MIME format
Using input As FileStream = File.OpenRead("C:\MyData\mail.eml")
    ' send mail from stream
    smtp.Send(input)
End Using

There is also an overload of Smtp.Send method that accepts a file path instead of Stream.

Sending multiple messages in a single session 

You can send multiple messages in a single SMTP session. This significantly reduces the overhead involved in establishing new sessions.

// create SMTP client instance, connect, log in
// ...

// send all "*.eml" files from "MyData" directory
foreach (string file in Directory.GetFiles(@"C:\MyData", "*.eml"))
{
    smtp.Send(file);
}
' create SMTP client instance, connect, log in
' ...

' send all "*.eml" files from "MyData" directory
For Each file As String In Directory.GetFiles("C:\MyData", "*.eml")
    smtp.Send(file)
Next

Sending e-mail directly to recipient's SMTP server 

If you don't have any outgoing SMTP server, you might try using Smtp.SendDirect method to send e-mail directly to the recipient's SMTP server.

// send the mail directly to recipient SMTP server
SmtpRejectedRecipient[] rejected = Rebex.Net.Smtp.SendDirect(mail);

// print info about rejected recipients
foreach (SmtpRejectedRecipient recipient in rejected)
{
    Console.WriteLine("Rejected: {0}", recipient.Address);
}
' send the mail directly to recipient SMTP server
Dim rejected As SmtpRejectedRecipient() = Rebex.Net.Smtp.SendDirect(mail)

' print info about rejected recipients
For Each recipient As SmtpRejectedRecipient In rejected
    Console.WriteLine("Rejected: {0}", recipient.Address)
Next

However, this means you are actually simulating an SMTP server. Don't use this unless you have to. Also, many organizations block outgoing connection to port 25, which blocks the Smtp.SendDirect as well.

Sending e-mail using IIS mail pickup directory 

To send an e-mail using IIS pickup directory, use the static Smtp.Send method and set SmtpConfiguration.DeliveryMethod to SmtpDeliveryMethod.IisPickupDirectory:

// create new configuration and specify desired delivery method
var config = new SmtpConfiguration();
config.DeliveryMethod = SmtpDeliveryMethod.IisPickupDirectory;
config.PickupDirectoryPath = path;

// send the mail using IIS pickup directory
Rebex.Net.Smtp.Send(mail, config);
' create new configuration and specify desired delivery method
Dim config = New SmtpConfiguration()
config.DeliveryMethod = SmtpDeliveryMethod.IisPickupDirectory
config.PickupDirectoryPath = path

' send the mail using IIS pickup directory
Rebex.Net.Smtp.Send(mail, config)

Sending e-mail on behalf of another user 

To send an e-mail on behalf of another address, specify the MailMessage.Sender property in addition to MailMessage.From:

// prepare mail message
mail.From = "joe@example.org";
mail.Sender = "bob@example.org";

// create SMTP client instance, connect, log in
// ...

// Bob sends mail on behalf of Joe
smtp.Send(mail);
' prepare mail message
mail.From = "joe@example.org"
mail.Sender = "bob@example.org"

' create SMTP client instance, connect, log in
' ...

' Bob sends mail on behalf of Joe
smtp.Send(mail)

Instructing the receiver to send replies to another address 

When you would like the replies to be delivered to an address different from the sender, specify the MailMessage.ReplyTo property in addition to MailMessage.From:

// prepare mail message
mail.From = "joe@example.org";
mail.ReplyTo = "bob@example.org";

// create SMTP client instance, connect, log in
// ...

// send mail from Joe, but request that replies go to Bob
smtp.Send(mail);
' prepare mail message
mail.From = "joe@example.org"
mail.ReplyTo = "bob@example.org"

' create SMTP client instance, connect, log in
' ...

' send mail from Joe, but request that replies go to Bob
smtp.Send(mail)

Setting and getting a client domain 

Many SMTP servers require the client machine domain announced by the client to be valid. The Smtp object tries detecting the domain automatically, but it's not always possible. In such cases, set the client domain explicitly using the Smtp.ClientDomain property.

// create SMTP client instance
using (var smtp = new Rebex.Net.Smtp())
{
    // set the client domain
    smtp.ClientDomain = "domain.example.org";

    // connect
    smtp.Connect(hostname);

    // write info about client and server domain
    Console.WriteLine(smtp.ClientDomain);
    Console.WriteLine(smtp.ServerDomain);
}
' create SMTP client instance
Using smtp = New Rebex.Net.Smtp()
    ' set the client domain
    smtp.ClientDomain = "domain.example.org"

    ' connect
    smtp.Connect(hostname)

    ' write info about client and server domain
    Console.WriteLine(smtp.ClientDomain)
    Console.WriteLine(smtp.ServerDomain)
End Using

Delivery status notifications (DSN) 

You can ask your SMTP server to generate delivary status notifications (DSN) for you. Use the following properties to configure various aspects of this:
  Smtp.DeliveryStatusNotificationConditions
  Smtp.DeliveryStatusNotificationOriginalMessageMethod

To be able to match the DSN replies with your messages, use MailMessage object's EnvelopeID property.

// generate DSN on Failure or Unusual Delay (default is Failure only)
smtp.DeliveryStatusNotificationConditions =
    DeliveryStatusNotificationConditions.Failure |
    DeliveryStatusNotificationConditions.Delay;

// include full original message to DSN
smtp.DeliveryStatusNotificationOriginalMessageMethod =
    DeliveryStatusNotificationOriginalMessageMethod.FullMessage;

// generate and/or assign a unique envelope ID
// (although it is convenient to assign the same unique ID to both MessageId
// and EnvelopeId, these headers are not required to be the same)
mail.MessageId = new MessageId();
mail.EnvelopeId = mail.MessageId.Id;

// send the email
// ...
' generate DSN on Failure or Unusual Delay (default is Failure only)
smtp.DeliveryStatusNotificationConditions =
    DeliveryStatusNotificationConditions.Failure Or
    DeliveryStatusNotificationConditions.Delay

' include full original message to DSN
smtp.DeliveryStatusNotificationOriginalMessageMethod =
    DeliveryStatusNotificationOriginalMessageMethod.FullMessage

' generate and/or assign a unique envelope ID
' (although it is convenient to assign the same unique ID to both MessageId
' and EnvelopeId, these headers are not required to be the same)
mail.MessageId = New MessageId
mail.EnvelopeId = mail.MessageId.Id

' send the email
' ...

Sending replies 

To create a reply to a message, use MailMessage.CreateReply method. Then, send the message using Smtp.Send.

Forwarding messages 

To forward a message to another address, copy the required fields and add the original message as an attachment:

// download a message to forward
MailMessage original = imap.GetMailMessage(uniqueId);

// prepare forward message
MailMessage forward = new MailMessage();
forward.From = "joe@example.org";
forward.To = "john@example.org";
forward.Subject = "FW: " + original.Subject;
forward.BodyText = "Hello, I am forwarding this to you:\n\n" + original.BodyText;

// include original message as attachment
forward.Attachments.Add(new Attachment(original));

// send it
smtp.Send(forward);
' download a message to forward
Dim original As MailMessage = imap.GetMailMessage(uniqueId)

' prepare forward message
Dim forward As New MailMessage()
forward.From = "joe@example.org"
forward.To = "john@example.org"
forward.Subject = "FW: " + original.Subject
forward.BodyText = "Hello, I am forwarding this to you:" & vbLf & vbLf & original.BodyText

' include original message as attachment
forward.Attachments.Add(New Attachment(original))

' send it
smtp.Send(forward)

Back to feature list...