Unable to authenticate email from c# to cMailServer

mike_allain
mike_allain used Ask the Experts™
on
Hi,

I have been fighting with this for some time now and I feel that I have to be missing something simple....

Basically, I set up a LAN based email server (CMailServer) on another box for development / testing purposes.  Under the options in cMailServer...I have enabled ESMTP (Authentication)

I then set up the email account in Microsoft Outlook and under advance have it set to authenticate outgoing email.

I am then able to run a test (Send and receive) succesfully in MS outlook...

I then run my code (C#) and attempt to send an email ....If I have authentication on the cMailServer turned off...the email from c# will send successfully.... how ever if I have authentication on the cMailServer turned on I am unable to send a successfully email even though I am using the appropriate credentials....

Here is the code and settings I am using to send an email:
- On the C# box...I have set the host file to point to (cMailServer IP Address)  to r1qe.com...
- this.HTMLSupported = true
- this.OutGoingSerReqAuth = true
- this.SMTPMailServer = r1qe.com.
- this.SMTPMailPort = 25
- this.SSLRequired = false;

Any help with this will certainly be appreciated...

Thanks
NetworkCredential basicCredentials = new NetworkCredential(this.MailUserName, this.MailPassword);
            MailMessage mail = new MailMessage();
            mail.IsBodyHtml = this.HTMLSupported; 
            mail.From = new MailAddress("results@r1qe.com", "Critical " + (char)0xD8 + " Results", System.Text.Encoding.UTF8);           // put the from address here
            mail.To.Add(new MailAddress("results@r1qe.com"));           // put to address here
            mail.ReplyTo = (new MailAddress("results@r1qe.com"));         // put reply to address here
            mail.Subject = "email test";                                         // put subject here	
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = "test of email sender";                                 // put body of email here
            mail.BodyEncoding = System.Text.Encoding.UTF8; 
 
            //Set Up Outgoing smtp requires authentication support
            SmtpClient wClient = new SmtpClient(this.SMTPMailServer, this.SMTPMailPort);
            wClient.Timeout = this.MailServerTimeouts;
            wClient.EnableSsl = this.SSLRequired;
 
            if (this.OutGoingSerReqAuth == true)
            {
                wClient.UseDefaultCredentials = false;
                wClient.Credentials = basicCredentials;
            }
            else
            {
                wClient.UseDefaultCredentials = true;
            }
 
            wClient.Send(mail);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I cannot see anything wrong with your approach, so some things to try instead.

Do you have access to the SMTP server -- is it configured to accept/require authentication before sending; do the SMTP server logs indicate any failure?

Are you able to authenticate with the SMTP server and send a mail using a dedicated email client, such as Outlook?

Have you tried adding the credential to a CredentialCache and setting that on the SmtpClient?

J.

One other possible: are you specifying the credentials elsewhere, i.e. in the system.net section of your app/web.config?  Can you use this instead of trying to set it in code?

Author

Commented:
Hi,

Turn out as you siad that the code was fine and that it was an issue with something else.... the cMailServer for some reason was unable to authenticate .....but I decided to try another smtp server (outside our LAN) and authenticate to it and was able to without any issues...

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial