20

We are thinking about moving to O365; however, we developed software that uses our current Exchange server to send email both to external users as well as to a support box when errors occur.

I've been testing this to ensure that the code we have in place will continue to work with O365 but so far, I have not been very successful.

I have tried using .Net's SmtpClient as well as MailKit's SmtpClient and neither one seems to work. I keep getting error (this is the error from MailKit -- the .Net error is similar)

"AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful [*.prod.exchangelabs.com]"

I can use the credentials that I have in my code to log into OWA -- so I know the credentials are valid. Is it not possible to send email via O356? Is there any special configuration that has to happen in Exchange to make this possible?

Here is what I've tried so far:

MailKit

var msg = new MimeMessage();
msg.From.Add(new MailboxAddress("Support","support@mydomain.com"));
msg.To.Add(new MailboxAddress("Me","me@mydomain.com"));
msg.To.Add(new MailboxAddress("External User","euser@externaldomain.com"));
msg.Subject = "Test";
msg.Body = new TextPart("plain"){
   Text = "Here is a message for you"
};
using(var client = new SmtpClient()){
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;
    client.AuthenticationMechanisms.Remove("XOAUTH2"); //Not sure what this does.  Have tried with and without
    client.Connect("smtp.office365.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
    client.Authenticate(new NetworkCredential("support@mydomain.com", "supportPwd"));
    client.Send(msg);
    client.Disconnect(true);
}

The .Net SmtpClient code looked very similar to the MailKit code.

  1. Is there a way to send through O365 with a licensed user? (code above)
  2. Are there any special settings required in Exchange or on the licensed user to make this work? (If the answer to 1 is yes)
  3. Is it possible to send email through a shared mailbox for which the credentialed user has Send As rights?

Update

I'm still getting the same error message. We do have MFA enabled for our domain users. However, we have a policy that does not require MFA for users when they are signing in from a trusted location (our org's IP). I also listed our IP as a Trusted IP. In my mind, MFA shouldn't be the issue here.

I know the credentials are correct. I copied them from the code and pasted them in to the login screen when signing into M365 -- and I got in just fine.

What am I doing wrong?

RHarris
  • 10,641
  • 13
  • 59
  • 103

3 Answers3

13
  1. Yes, you can.

  2. Usersettings: Screenshot of Admin Center Screenshot of Manage email apps

Server-settings : https://support.office.com/en-us/article/POP-IMAP-and-SMTP-settings-for-Outlook-com-d088b986-291d-42b8-9564-9c414e2aa040

SMTP server name smtp.office365.com

SMTP port 587

SMTP encryption method STARTTLS
  1. No, you cannot. You need a licenced user to send mail via SMTP.

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_o365admin/set-up-smtp-relay-with-shared-mailbox/d7b98214-9564-432c-b098-525a98c529fb

A customer of ours has a newsletter system set up with TYPO3 and we had to create a new mailbox for this. However, a light one will suffice: instead of a Office 365 Business Premium we only assigned a Office 365 F1 licence.

Edit: also found this: Can Office365 shared mailbox use SMTP?

Wolfgang Jacques
  • 769
  • 6
  • 15
  • So that seems to answer my question regarding a Shared Mailbox. But in my initial code, I'm working with a licensed user and that isn't working either -- should it? – RHarris Jan 14 '20 at 16:23
  • @RHarris yes it should. See edited answer. Sorry for incomplete first reply. – Wolfgang Jacques Jan 14 '20 at 21:29
  • @WolfgangJacques Hi Thank you for the answer. Understanding the question and the answers, even using .NET frameworks' own native [SmtpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.6.2) will also be not allowed? Since it's using SMTP. But then what is the alternative way of sending emails we have other than sending Emails from `SmtpClient` if the same is treated as **Legacy Authentication**? – hiFI Oct 04 '21 at 06:36
  • @hiFI, sorry, I cannot tell. I guess no, since SMTP is SMTP. In my experience, it is the licencing principle of Microsoft. If you authenticate as a licenced user you might be able to send _as_ the shared mailbox if you have sufficient rights. About Legacy Authentication I know nothing. – Wolfgang Jacques Oct 05 '21 at 08:10
2

For anyone who is having similar issues, I found that my problem was a Conditional Access Policy. Microsoft provides a Baseline Policy: Block Legacy Authentication -- which had been turned on in our AAD.

In looking at the Policy, it is designed to BLOCK any authentication mechanisms that don't require MFA. This includes things like POP and SMTP. Once I disabled this policy, the code listed above worked just fine.

RHarris
  • 10,641
  • 13
  • 59
  • 103
1

For me only disabling "Security defaults" helped.

kuzavas
  • 2,156
  • 1
  • 16
  • 8