I am trying to send email using office365 but facing this issue :
Additional information: 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 [MA1PR01CA0073.INDPRD01.PROD.OUTLOOK.COM]
My code to send email is :
var fromAddress = new MailAddress("email", "Relay");
var toAddress = new MailAddress("email");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.office365.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
TargetName = "STARTTLS/smtp.office365.com",
Credentials = new NetworkCredential(fromAddress.Address, fromPassword, "islandenergyservices.com")
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}