SmtpMessage.DirectSend() is slow.

May 2, 2011 at 7:14 PM
Edited May 2, 2011 at 7:21 PM

SmtpMessage.DirectSend() takes around 30 seconds on my machine.

Is there anything I can do to speed this up?

Code looks like this:

 

        public void SendEmail(Email email, bool htmlBody = false, Dictionary<string, string> headers = null)
        {
            var message = new SmtpMessage();
            message.From.Email = email.From;
            message.To.Add(email.To);
            if (!string.IsNullOrEmpty(email.Cc))
            {
                message.Cc.Add(email.Cc);
            }
            if (!string.IsNullOrEmpty(email.Bcc))
            {
                message.Bcc.Add(email.Bcc);
            }
            if (!string.IsNullOrEmpty(email.Subject))
            {
                message.Subject = email.Subject;
            }
            if (!string.IsNullOrEmpty(email.Body))
            {
                if (htmlBody)
                {
                    message.BodyHtml.Text = email.Body;
                }
                else
                {
                    message.BodyText.Text = email.Body;
                }
            }
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    message.AddHeaderField(header.Key, header.Value);
                }
            }
            if (email.Attachments != null)
            {
                foreach (var attachment in email.Attachments)
                {
                    message.Attachments.Add(attachment.Data, attachment.Filename);
                }
            }
            
            message.BuildMimePartTree();
            message.DirectSend();
        }

Email is just a simple container class with From, To, Cc, Subject, etc.

My subject and body are pretty small, one thing I notice is that I'm sending a simple attachment also.  Attachment is a UTF8 text file with a few words in it.