Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:45:10+00:00 2026-06-03T05:45:10+00:00

I have set the Timeout property of SmtpClient class, but it doesn’t seem to

  • 0

I have set the Timeout property of SmtpClient class, but it doesn’t seem to work, when i give it a 1 millisecond value, the timeout actually is 15secs when the code is executed. The code i took from msdn.

string to = "jane@contoso.com";
string from = "ben@contoso.com";
string subject = "Using the new SMTP client.";
string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("1.2.3.4");
Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
client.Timeout = 1;
// Credentials are necessary if the server requires the client 
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);

I tried the implementation on mono, it also doesn’t work.

Do anyone encountered the same problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:45:11+00:00Added an answer on June 3, 2026 at 5:45 am

    Reproducing your test – it works for me

    You asked if anyone has encountered the same problem – I just tried your code on Windows 7, VS 2008 with .NET 2.0 – it worked just fine. With the timeout set to 1, as you have it, I get this error almost immediately:

    Unhandled Exception: System.Net.Mail.SmtpException: The operation has timed out
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at mailtimeout.Program.Main(String[] args) in c:\test\mailtimeout\Program.cs:line 29
    

    I think the problem may be that you are expecting something different from timeout. Timeout means that the connection was made successfully, but the response did not come back from the server. This means you need to actually have a server listening on port 25 at your destination, but it doesn’t respond. For this test, I use Tcl to create a socket on 25 that did nothing:

    c:\> tclsh
    % socket -server foo 25
    

    When I changed the timout to 15000, I didn’t get the timeout error unti l5s later.

    Why Smtp.Timeout has no effect if the connection can’t be made

    If nothing is listening at port 25, or the host is not reachable, the timeout is not going to happen until at least 20s, when the system.net.tcpclient layer times out. This is below the system.net.mail layer. From an excellent article describing the problem and solution:

    You will notice that neither of the two classes, System.Net.Sockets.TcpClient nor System.Net.Sockets.Socket has a timeout to connect a socket. I mean a timeout you can set. .NET Sockets do not provide a Connect Timeout when calling the Connect/BeginConnect method while establishing a Synchronous/Asynchronous socket connection. Instead, connect is forced to wait a very long time before an Exception is thrown if the server it tried to connect to is not listening or if there is any network error. The default timeout is 20 – 30 seconds.

    There is no ability to change that timeout from mail (which makes sense, mail servers are usually up), and in fact there’s no ability to change the connect from system.net.socket, which is really surprising. But you can do an asynchronous connect, and can then tell if your host is up and the port open. From this MSDN thread, and in particular this post, this code works:

    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IAsyncResult result = socket.BeginConnect("192.168.1.180", 25, null, null);
    // Two second timeout
    bool success = result.AsyncWaitHandle.WaitOne(2000, true);
    if (!success) {
        socket.Close();
        throw new ApplicationException("Failed to connect server.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used to set Transaction timeouts by using TransactionOptions.Timeout , but have decided for
In my web application I have set the session timeout for 30 minutes. But
I have an animation that is set to play after a timeout. This animation
I have set up the Nginx +unicorn for my rails application, it is working
I have set up an admin panel for charter, yacht and other vehicles reservation
We have set a custom 404 error page in order to display a 'nice'
I have set an environment variable in windows by name sample. Can I get
Is it possible to use JavaScript to set the cursor attribute to the property
In my DAO implementation I'm doing Stripersist.getEntityManager().persist(client); , this doesn't seem to return any
I work with SQL Server 2008 R2 I have a simple trigger CREATE TRIGGER

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.