Exchange 2003 with C# smtpclient

315 Views Asked by At

I am currently trying to use SmtpClient to send an email with exchange 2003. Using the code below it will not send an email, but it does not throw an exception which i assume means it is making a connection to the exchange server because. Also for the server setting I tried mail.server.com as well as the IP address and it still does not throw an exception.

        public static void emailTest()
    {
        string fromEmail = @"[email protected]";
        string ToEmail = @"[email protected]";          
        string body = "C Stuck Batches"; 
        string subject = "C Stuck Batches"; 

        try
        {
            SmtpClient MyMail = new SmtpClient("x.x.x.x");
            MyMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            MyMail.UseDefaultCredentials = false;
            MyMail.Credentials = new NetworkCredential(@"domain\user", "password"); 

            MyMail.Send(fromEmail, ToEmail, subject, body);
            MessageBox.Show("Sent", "SENT", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        } 
    }

I have read through several other problems, but as far as I could tell most others were getting some sort of exception or timeout. Since this appears as though it connects I am not sure what the issue is. Would it throw an exception if it were unable to access the server? If I throw a different IP into the server it will throw an exception.

Also I have tried both the IP of the server and mail.xx.com and it will not throw an exception with either but if I put any other addresses it will fail.

Thanks in advance for anyhelp!

2

There are 2 best solutions below

0
h8a On BEST ANSWER

The issues i had was that even the domain admins did not have access to the SMTP relay. I had to make sure the user was authenticated in the SMTP relay. Regardless of the fact the authenicating user was the same as the email adress used, that user did not have access. Once corrected it was able to send without issue.

1
KenD On

Is your Exchange server set up to relay mail from your IP address? If it isn't, mail may be "silently" discarded without throwing an Exception (the client is connecting correctly, it's the back end that's discarding the email)

This link provides more information on how to configure relaying on Exchange 2003 (I'm assuming "eSupport" would equate to your application in this example i.e. the IP of the machine running your code).

Log into Exchange 2003 server (192.168.5.25 in my example above) and start Exchange System Manager.

Expand the organization_name object, and then expand the Servers node. Expand the server_name object of the server on which you want to control mail relay, and then expand the Protocols node.

Expand the SMTP node, right-click the virtual SMTP server on which you want to control mail relay, and then click Properties

Click Relay.

In the Relay Restriction dialog box, several options are available. The Only the list below option is enabled by default; the list below this option is empty.

Click Add. Enter the IP address of the server running eSupport (192.168.5.80 in my example above).

In the Relay Restrictions dialog box, click OK.

Click Apply, and then click OK in the Default SMTP Virtual Server Properties dialog box.