I have a console application, through which I need to send out email notifications. I am able to login into mailbox using the same credentials in the browser.
From the exception thrown, it seems like I am not able to open connect with Office OWA through C# code and getting an error message: "Failure Sending mail" - Inner exception - "Unable to connect to remote server".
I have noticed that while login into mailbox from the browser, it 1st make me land on company-specific page based on email address domain and then ask to enter password to login into the mailbox.
Also, network credentials passed to open smtpClient connect need to have some admin privileges or elevated permissions in Office 365, or it can be a regular office 365 user with a mailbox.
Reference blog:
https://weblogs.asp.net/sreejukg/send-email-using-office-365-account-and-c?__r=8d721ba733714a1
try
{
String userName = "[email protected]";
String password = "passwordgoeshere";
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("[email protected]"));
msg.From = new MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
using (SmtpClient client = new SmtpClient
{
Host = "smtp.office365.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 587,
EnableSsl = true,
})
{
client.Send(msg);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadKey();
}
Goal:
- I should be able to send out emails from a shared mailbox if I open the connection using shared mailbox credentials.