I am using SmtpClient with office365 mailserver to send email. But everytime i try smtpclient.Send(msg),it will throw 'Operation has timed out' smtpexception. I have tried all the earlier options like to change the port to 587 and increase the timeout value but nothing works. Can anyone help me out. Below is my source code.
using (MailMessage msg = new MailMessage
{
From = new MailAddress("Sender@ourdomain.com"),
Subject = this.Subject,
Body = this.Body,
IsBodyHtml = true
})
{
msg.To.Add(new MailAddress("Receiver@ourdomain.com"));
using (SmtpClient client = new SmtpClient
{
Host = "smtp.office365.com",
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("username", "password"),
Port = 587
})
{
try
{
client.TargetName = "STARTTLS/smtp.office365.com";
client.Send(msg);
}
catch (Exception)
{
throw;
}
}
}