none
Can SmtpClient.Send() Method send through different thread instead of main/ui thread? RRS feed

  • Question

  • Hello All,

    I try using SmtpClient.Send() in my C# application , as per my understanding this method will initiate the sending on the main/ui thread and would block..but in my application it seems SmtpClient.Send() is running in the different thread..

    Is there any posibility that SmtpClient.send() runs in different thread? how it can run through the different thread? how it can be solved ?

    Thanks for the support in advance.

    Wednesday, March 4, 2020 12:58 PM

All replies

  • Did you consider SmtpClient.SendAsync too?

    Wednesday, March 4, 2020 5:07 PM
  • Hi Rajeshwari Kanchiraju,

    Thank you for posting here.

    According to your description, you can refer to the following code to run SmtpClient.send() in different thread.

                var SMTP = new SmtpClient
                {
                    Host = "Smtp.live.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(account, password)
                };
    
                Thread T1 = new Thread(delegate ()
                {
                    using (var message1 = new MailMessage(senderAdrress, toAddress)
                    {
                        Subject = strSubject,
                        Body = strBody
                    })
                    {
                        {
                            SMTP.Send(message1);
                        }
                    }
                });
    
                T1.Start();

    Hope it could be helpful.

    Besides, if I have any misunderstanding, please provide more details about your question.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 5, 2020 3:10 AM
    Moderator
  • Hi Xingyu Zhao,

    Thanks for the reply!

    I expect SmptClient.Send() has to run in the main/ui thread.....but whereas it seems to be running in different thread in my profiler...

    what can be the reason/possibility that it is running in different thread? i don't want to run SmptClient.Send() in different thread..

    Best regards,

    Rajeshwari Kanchiraju.

    Thursday, March 5, 2020 4:59 AM
  • Hi Viorel_

    i didn't use SmtpClient.SendAsync.

    Thursday, March 5, 2020 5:05 AM
  • Hi Rajeshwari Kanchiraju,

    >>it seems to be running in different thread in my profiler

    This is strange because smtpClient.send() will initiate the sending on the main/ui thread and will block.

    A simple test:

    You may need to check your code and make sure that no other thread is running smptclient. Send().

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 9:16 AM
    Moderator