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;
}
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.