Solved

c# Office365 SMTP Exception received 5.7.1 Client was not authenticated

Posted on 2014-07-21
4
356 Views
Last Modified: 2016-04-01
I am trying to send an email from within a c# app via Office365.

I am receiving the following error:

[test out message sending] System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated.

My code is simple at the moment
        public static void SendMail()
        {
            SmtpClient client = new SmtpClient("smtp.office365.com", 587);
            client.EnableSsl = true;

            client.Credentials = new System.Net.NetworkCredential("me@bubbles.com", "Pa55w0rd");
            client.Timeout = 5000;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("me@bubbles.com");
            mail.To.Add("friend@bubbles.com");
            mail.Subject = "test out message sending";
            mail.Body = "this is my message body";
            mail.IsBodyHtml = true;

            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            client.SendAsync(mail,mail);
            Console.WriteLine("Sending Message...");
        }

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            MailMessage msg = (MailMessage)e.UserState;

            if (e.Cancelled)
            {
                Console.WriteLine("[{0}] Send canceled.", msg.Subject);
            }
            if (e.Error != null)
            {
                Console.WriteLine("[{0}] {1}", msg.Subject, e.Error.ToString());
            }
            else
            {
                Console.WriteLine("Message sent.");
            }

        }

Open in new window


I've looked through lots of posts for this, but solutions recommended do not seem to change my fault.
0
Comment
Question by:west-com
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Mitchell Milligan earned 250 total points
Comment Utility
In office 365, port 587 requires authentication, and to have the sending ip address added as a connector in the panel to trust the incoming message.  Alternatively you can use port 25 for SMTP to office 365 and it should go through without a trust issue.  You can see more about that here: How to Allow a Multi-function Device or Application to Send E-mail through Office 365 Using SMTP
0
 
LVL 9

Assisted Solution

by:xav056
xav056 earned 250 total points
Comment Utility
Sign in through OWA and verify your mailbox address from the help icon
see if replacing smtp.office365.com with your actual server name resolves it.
0
 

Author Comment

by:west-com
Comment Utility
Many thanks for these two. I've just been dragged off this project for a couple of days. Will try these two options shortly. I wanted to avoid port 25 as many of our clients block port 25 on the router to all but the mail server to prevent viruses emailing from PCs and causing the external IP being Blacklisted.
0
 

Author Comment

by:west-com
Comment Utility
Both solutions worked. Thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now