0
votes

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

https://docs.microsoft.com/en-us/Exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3?redirectSourcePath=%252farticle%252fHow-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4#HowtoconfigSMTPCS

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:

  1. I should be able to send out emails from a shared mailbox if I open the connection using shared mailbox credentials.
1
Your code looks okay (i wrote something similar this week and it works fine) given the error your getting I suspect that your firewall is blocking port 587. Blocking port 25 and 587 is a common thing to do for corps to stop Trojans (or your coming from a blacklisted IP). Try running you code using a different Internet connection etcGlen Scales
Do I need to open these ports for specific IPs (or range of IPs) or is it a ON/Off kind of setting which can be either turned On or Off. I know that many other teams within my company are sending out emails, so there must be a way for this to get going for me too. 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.sdg
You need to open it for the source Ip only, its easy to test you should be able to telnet (putty etc) to the port and get a response docs.microsoft.com/en-us/exchange/mail-flow/…Glen Scales

1 Answers

0
votes

Please add UseDefaultCredentials = false, otherwise it will select system or authenticated user credential for sending email

            using (SmtpClient client = new SmtpClient
            {
                Host = "smtp.office365.com",
                Credentials = new System.Net.NetworkCredential(userName, password),
                Port = 587,
                EnableSsl = true,
                UseDefaultCredentials = false
            })
            {
                client.Send(msg);
            }

or recommend to set like this

        using (SmtpClient client = new SmtpClient
        {
            Host = "smtp.office365.com",
            Credentials = new System.Net.NetworkCredential(userName, password),
            Port = 587,
            EnableSsl = true
        })
        {
            client.UseDefaultCredentials = false;
            client.Send(msg);
        }