3

I want to check whether an SMTP mailbox is available for a given username and password. I am using the SmtpClient.Send method to send the email, but before sending it I want to check the whether the credentials provided are correct and also to check whether the SMTP server is valid.

How can I do this from C#?

Kit
  • 20,354
  • 4
  • 60
  • 103
Pravin
  • 523
  • 2
  • 5
  • 22
  • Just to make sure I understood, you want to check not if the recipient's mailbox is available, but if the sender's username and password are valid on that SMTP server? – Blachshma Jun 04 '12 at 08:47
  • see a old link.I think it helps you.http://stackoverflow.com/questions/372742/can-i-test-smtpclient-before-calling-client-send – 4b0 Jun 04 '12 at 08:49
  • yes I want check sender's username and password are valid on that SMTP server. – Pravin Jun 04 '12 at 08:58
  • Please can anybody tell me how to check the configured sender's SMTP server name, username and password are correct? – Pravin Jun 04 '12 at 13:40

1 Answers1

0

I don't think the API provides the equivalent of an "is valid" type of query. However, when you look at the documentation, it states that an SmtpException will be thrown if the send fails; two of the reasons for failure include what you were looking for

  • invalid server (i.e. you couldn't connect with the SMTP protocol)
  • authentication failed (i.e. invalid username and password)

So handling that exception and inspecting it for whether the failure was indeed due to an authentication failure or a server connection failure should provide you with what you need.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • thanks kit really helpful..simply wasting the time to check SMTP mailbox is available or not. – Pravin Jun 08 '12 at 09:33