Click here to Skip to main content
14,699,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I cant get this working no matter which workarounds i put in my code . What i am trying to do is send an email using my domain Exchange Server and my usual domain email address.

This is my code :

<pre> static void Main(string[] args)
        {
		
            try
            {
                using (var message = new MailMessage("username.surname@company.co.zw", "recipient.surname@company.co.zw"))
                {
                    message.Subject = "Message Subject test";
                    message.Body = "My test email " + DateTime.Now;                   
                    using (SmtpClient client = new SmtpClient
                    {
                        EnableSsl = true,
                        Host = "+ mailserverIP +",
                        Port = 25,
                        DeliveryMethod = SmtpDeliveryMethod.Network,                       
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential("company.corp\username", "password")
                    })
                    {

                        ServicePointManager
         .ServerCertificateValidationCallback +=
         (sender, cert, chain, sslPolicyErrors) => true;
                        client.Send(message);
                    }
                }
            }

            catch (Exception ex)
            {
                ExceptionLogger.SendErrorToText(ex);
            }
		
          }


I am getting error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
with Status Code MustIssueStartTlsFirst .

When i try validating certificate using the below snippet i also get the same error :
ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
             X509Certificate certificate,
             X509Chain chain,
             SslPolicyErrors sslPolicyErrors)
                      {
                          return true;
                      };


What I have tried:

I have tried to grab the certificate hash from the Outlook Web App in my browser as below but doing this gives another error
The remote certificate is invalid according to the validation procedure.
. Unfortunately in the stack trace there is no Inner Exception.

static void Main(string[] args)
        {
		
            try
            {
                using (var message = new MailMessage("username.surname@company.co.zw", "recipient.surname@company.co.zw"))
                {
                    message.Subject = "Message Subject test";
                    message.Body = "My test email " + DateTime.Now;                   
                    using (SmtpClient client = new SmtpClient
                    {
                        EnableSsl = true,
                        Host = "+ mailserverIP +",
                        Port = 25,
                        DeliveryMethod = SmtpDeliveryMethod.Network,                       
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential("company.corp\username", "password")
                    })
                    {

      System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate (
                            object sender,
                            X509Certificate cert,
                            X509Chain chain,
                            SslPolicyErrors sslPolicyErrors)
                            {
                            if (sslPolicyErrors == SslPolicyErrors.None)
                            {
                                return true;   //Is valid
                            }

                            if (cert.GetCertHashString() == "+ certHash+ ")
                            {
                                return true;
                            }

                            return false;
                        };
						
                        client.Send(message);
                    }
                }
            }

            catch (Exception ex)
            {
                ExceptionLogger.SendErrorToText(ex);
            }
		
          }


What am i missing? I have been struggling to get this work for quite some time but i still do feel im missing something obvious.

I have even tried changing the sender to be an administrator domain email but i still get the same errors.
Posted
Comments
F-ES Sitecore 12-Jun-20 8:34am
   
Try expanding the SmtpClient to set the properties independently, ensuring you set UseDefaultCredentials before you set the Credentials themselves.

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = ...
Tshumore 12-Jun-20 9:31am
   
Tried using that way . I still get the same exception "The remote certificate is invalid according to the validation procedure."
MadMyche 12-Jun-20 11:35am
   
Normally port 25 is not encrypted, typically secured SMTP would use ports 365 or 587
Tshumore 18-Jun-20 2:45am
   
The issue was with permissions. My IP address needed to be added to Exchange Server as an email originator

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900