locked
Exception while sending mail via System.Net RRS feed

  • Question

  • Hi,

    Am trying to send multiple mails using system.net.mail class where i get the exception 

    "This operation cannot be performed while a message is being sent.".Please find the attached snap for more info. Also i have used separate thread for sending each mail via Thread.Factory.StartNew(). I dont know why am getting this exceptin.Please help me out to resolve this issueException

    Tuesday, December 1, 2015 10:05 AM

Answers

  • "Also i have used separate thread for sending each mail "

    You're not trying to share the SMTP client on each thread are you?  That would be a reason why you'd be getting such an exception.  SmtpClient is not thread safe and therefore cannot be used on more than one thread. You need to create a new instance inside each thread.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Christopher84 Tuesday, December 1, 2015 1:29 PM
    • Marked as answer by Kristin Xie Tuesday, December 8, 2015 5:59 AM
    Tuesday, December 1, 2015 1:11 PM
  • Hi,

    Instead of calling the method SendAsync for  sending the mail i have called sendmail which was synchronous . So till the first mail sends ,second one waits which makes the problem to resolve.

    Regards,

    Selvam.M

    • Marked as answer by selva173 Friday, December 11, 2015 3:32 AM
    Friday, December 11, 2015 3:32 AM

All replies

  • Hello,

    Have you setup a callback? If not, see the following for a simple example. You may need to look at InnerException besides the exception itself to determine the issue. I will say that seeing Credentials in the image might be a good place to start with determining the issue also.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    Tuesday, December 1, 2015 12:57 PM
  • "Also i have used separate thread for sending each mail "

    You're not trying to share the SMTP client on each thread are you?  That would be a reason why you'd be getting such an exception.  SmtpClient is not thread safe and therefore cannot be used on more than one thread. You need to create a new instance inside each thread.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Christopher84 Tuesday, December 1, 2015 1:29 PM
    • Marked as answer by Kristin Xie Tuesday, December 8, 2015 5:59 AM
    Tuesday, December 1, 2015 1:11 PM
  • Hi,

    Instead of calling the method SendAsync for  sending the mail i have called sendmail which was synchronous . So till the first mail sends ,second one waits which makes the problem to resolve.

    Regards,

    Selvam.M

    • Marked as answer by selva173 Friday, December 11, 2015 3:32 AM
    Friday, December 11, 2015 3:32 AM