sending mail using smtp is too slow in asp.net

Ankit tewari
Ankit t...
40 Points
20 Posts

when i am sending email (with attached 7 mb image) then it is taking approx 50 seconds. So I am using this code. but still not working.

Thread email = new Thread(delegate ()
{
{
SendMail("a", splitMailCC);
};
});
email.IsBackground = true;
email.Start();
Views: 150
Total Answered: 1
Total Marked As Answer: 0
Posted On: 14-May-2019 03:41

Share:   fb twitter linkedin
Use SmtpClient.SendAsync instead of using your own custom code
 - Stevan  14-May-2019 10:38
Answers
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         

I think you are trying to send email in separate thread to avoid blocking the requested thread.

My suggestion is, you should use SmtpClient.SendAsync method. This method does not block the calling thread.

public void SendAsync (System.Net.Mail.MailMessage message, object userToken);

Example:

public static class EmailService
    {
        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
             String token = (string)e.UserState;

            if (e.Cancelled)
            {
                //
            }
            if (e.Error != null)
            {
                //
            }
            else
            {
                //Message sent
            }
            mailSent = true;
        }
        public static void Send()
        {
            SmtpClient client = new SmtpClient("");
            MailAddress from = new MailAddress("fromemailaddress");
            MailAddress to = new MailAddress("toemailaddress");
            MailMessage message = new MailMessage(from, to);
            message.Body = "This is a nice test email message sent. ";
            message.Subject = "test message";
            Attachment attachment = new Attachment("");
            message.Attachments.Add(attachment);
            client.SendCompleted += new
            SendCompletedEventHandler(SendCompletedCallback);
            string userState = "test message1";
            client.SendAsync(message, userState);
            message.Dispose();
        }
    }

In the above example following line is for handling call back when send email completed:

client.SendCompleted += new
            SendCompletedEventHandler(SendCompletedCallback);

If you don't want this then you can skip this part by commenting it.

Posted On: 14-May-2019 10:12
Ankit, Have u resolved your issue?
 - Rahul Maurya  29-May-2019 10:40
 Log In to Chat