Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception using default SMTP credentials on Office365 - Client was not authenticated to send anonymous mail during MAIL FROM

I'm using NLog to send logs as email with a custom mail target. I am sending from my office365 account set up as the default in my web.config (of my main project) as follows:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network defaultCredentials="false" host="smtp.office365.com" port="587" userName="[email protected]" password="mypassword" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>

I override the Write method with my log target (in my NLog implementation package) as follows:

    protected override void Write(LogEventInfo logEvent) 
    { 
        try
        {
            using (var mail = new MailMessage())
            {
                this.SetupMailMessage(mail, logEvent, this.Layout.Render(logEvent));

                using (SmtpClient smtpClient = new SmtpClient())
                {
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Send(mail);
                }
            }
        }
        catch (Exception exception)
        {
            throw new NLogRuntimeException("An error occurred when sending a log mail message.", exception);
        }
    }

When the system tries to send a mail from this account, the following System.Net.Mail.SmtpException is thrown:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

I have quadruple checked the credentials and they are correct. Does anyone know what else could be causing this exception?

UPDATE: It turns out the CredentialCache.DefaultCredentials property is full of empty strings. Yet, when I extract the settings manually using the below code I can get the settings from the web.config.

SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
smtpClient.Credentials = new NetworkCredential(settings.Network.UserName, settings.Network.Password);
smtpClient.Host = settings.Network.Host;
smtpClient.Port = settings.Network.Port;
smtpClient.EnableSsl = settings.Network.EnableSsl;

var creds = CredentialCache.DefaultCredentials;  // Is empty

I can use this as a workaround. But what gives? Why would the default credentials be empty?

like image 459
08Dc91wk Avatar asked Apr 15 '15 10:04

08Dc91wk


People also ask

Do I need to enable SMTP Authentication in Office 365?

SMTP Authentication is used to send email from apps, reporting servers, and multifunction devices (like printers and scanners). We recommend only enabling SMTP Authentication for user accounts that require it since your default Exchange settings are more secure.


1 Answers

Thanks to this post, I was able to resolve our issues. We migrated mailboxes to O365 from a hybrid setup at Rackspace. The mailbox being used to send was not previously an Exchange account but became one after the migration.

mySmtpClient = New SmtpClient("smtp.office365.com")
mySmtpClient.Port = 587
mySmtpClient.EnableSsl = True
mySmtpClient.Credentials = New System.Net.NetworkCredential("[email protected]", "password", "domain.com")
mySmtpClient.Send(Msg)

Previous setup did not require us to provide port or enable ssl or even put the domain in the credential parameters. Hope this helps with folks who have to work with VB scripts automating emails via SMTP with Office 365.

like image 57
Pawr Avatar answered Sep 27 '22 20:09

Pawr