Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Mail Alternative

I'm working on a tool that schedules emails with our mail server in C#. I had been using the System.Net.Mail classes to send the mail.

Recently I've come across various issues with regards to RFC violations and other issues, such as SmtpClient not ending the SMTP session according to protocol. Each of these problems is counting toward a high spam score and affecting email delivery, so I need a solution to these problems.

I'm wondering what other people have resorted to in order to resolve these issues. Have people started using a third part component, if so which one?

EDIT: As supporting evidence, please see: http://www.codeproject.com/KB/IP/MailMergeLib.aspx

like image 979
Martin Avatar asked Oct 23 '09 15:10

Martin


2 Answers

One alternative is MailKit. Has a ton of features, sending can be reliably cancelled via a CancellationToken, so it doesn't have the problem of SmtpClient that it doesn't respect the specified timeout. Has a lot of downloads on NuGet, too.

Even the recent documentation recommends it:

[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")] public class SmtpClient : IDisposable

like image 157
Gebb Avatar answered Oct 13 '22 04:10

Gebb


If you have a Microsoft Exchange 2007 email server then you have an option to use it's web service direction to send email. The web service itself is a bit strange but we were able to encapsulate the weirdness and make it work just like our SMTP class.

First you would need to make a reference to the exchange web service like this: https://mail.yourwebserver.com/EWS/Services.wsdl

Here is an example:

public bool Send(string From, MailAddress[] To, string Subject, string Body, MailPriority Priority, bool IsBodyHTML, NameValueCollection Headers)
{
    // Create a new message.
    var message = new MessageType { ToRecipients = new EmailAddressType[To.Length] };

    for (int i = 0; i < To.Length; i++)
    {
        message.ToRecipients[i] = new EmailAddressType { EmailAddress = To[i].Address };
    }

    // Set the subject and sensitivity properties.
    message.Subject = Subject;
    message.Sensitivity = SensitivityChoicesType.Normal;
    switch (Priority)
    {
        case MailPriority.High:
            message.Importance = ImportanceChoicesType.High;
            break;

        case MailPriority.Normal:
            message.Importance = ImportanceChoicesType.Normal;
            break;

        case MailPriority.Low:
            message.Importance = ImportanceChoicesType.Low;
            break;
    }

    // Set the body property.
    message.Body = new BodyType
                   {
                       BodyType1 = (IsBodyHTML ? BodyTypeType.HTML : BodyTypeType.Text),
                       Value = Body
                   };

    var items = new List<ItemType>();
    items.Add(message);

    // Create a CreateItem request.
    var createItem = new CreateItemType()
                     {
                         MessageDisposition = MessageDispositionType.SendOnly,
                         MessageDispositionSpecified = true,
                         Items = new NonEmptyArrayOfAllItemsType
                                 {
                                     Items = items.ToArray()
                                 }
                     };


    var imp = new ExchangeImpersonationType
              {
                  ConnectingSID = new ConnectingSIDType { PrimarySmtpAddress = From }
              };
    esb.ExchangeImpersonation = imp;

    // Call the CreateItem method and get its response. 
    CreateItemResponseType response = esb.CreateItem(createItem);

    // Get the items returned by CreateItem.
    ResponseMessageType[] itemsResp = response.ResponseMessages.Items;
    foreach (ResponseMessageType type in itemsResp)
    {
        if (type.ResponseClass != ResponseClassType.Success)
            return false;
    }

    return true;
}
like image 34
Nathan Palmer Avatar answered Oct 13 '22 06:10

Nathan Palmer