views:

1243

answers:

3

I have a project that need to sends notification for employees by local email. I use SmtpClient class to send email but didn't work! here is the code:

    MailMessage message = new MailMessage();

    message.From = new MailAddress("[email protected]");
    message.To.Add(new MailAddress("[email protected]"));
    message.Subject = "Sending mail";
    message.Body = "Check sending email by Exchange from asp.net code <> ";

    SmtpClient client = new SmtpClient("ExchangeDNS", 25);

    try
    {
        client.Send(message);
    }
    catch (Exception exc)
    {
        Label1.Text = exc.Message.ToString();
    }

When I click buttons it give me an SmtpException with message : Failure sending mail.

NB: we use Exchange server.

How can I solve it?

+1  A: 

Assuming that you have the server and any required credentials correct (double check these) I would try increasing the timeout on the SMTP client.

Garry Shutler
+2  A: 

Use the fully qualified name for your server, for example, exchangeDNS.example.com. If that doesn't work, try the IP address. You may want to manually telnet to port 25 on the Exchange server, just to see if it is possible. Also, check whether your server requires an authenticated or encrypted connection. If so, you'll need to supply credentials with the request or change to use SSL and the secure port.

tvanfosson
A: 

Your code is fine. It's the configuration of your Exchange server that is suspect. Some options for your Exchange administrator:

  • Allow anonymous SMTP connections.
  • Whitelist the IPs, so that only your web servers can send mail in this manner.
  • Create an Active Directory account for the web server, and then have your class authenticate using a username and password.

If you post more details about your Exchange configuration, I can help, at the risk of having this question downmodded for "not programming related".

Portman