I have purchased "Office 365 Email Essentials" plan from GoDaddy for sending the emails. I am able to send and receive these emails in my outlook mailbox, but not programatically via C# code. I keep receiving the error "
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BMXPR01CA0092.INDPRD01.PROD.OUTLOOK.COM]"
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("xxx@gmail.com", "xxx"));
msg.From = new MailAddress("xxx@mydomain.com", "Admin");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("xxx@mydomain.com", "mypassword","mydomain.com");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
//Send the msg
try
{
client.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
And here is the mailsettings entry in the config file -
<mailSettings>
<smtp from="xxx@mydomain.com">
<network host="smtp.office365.com" port="587" />
</smtp>
</mailSettings>
The settings I see in the Outlook are -
Server name: smtp.office365.com Port: 587 Encryption method: STARTTLS
Alternatively, I also tried using the userid in place of actual email address in the network credentials,but that did not work either.
I also tried to use the MX Txt record in GoDaddy i.e. server "relay-hosting.secureserver.net" and port #25 ,but no luck.
I also changed the settings in "Mailbox delegation" as per the link http://edudotnet.blogspot.com/2014/02/smtp-microsoft-office-365-net-smtp.html , but that did not help either.
Please help me what am I missing in these steps.