08Dc91wk
08Dc91wk

Reputation: 4318

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="myusername@mydomain.com">
        <network defaultCredentials="false" host="smtp.office365.com" port="587" userName="myusername@mydomain.com" 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?

Upvotes: 12

Views: 49581

Answers (4)

Luis Adrian
Luis Adrian

Reputation: 1

I had the same issue, what it worked for me was to use the System.Security.SecureString to put the password instead of string, example:

        System.Security.SecureString psw = new System.Security.SecureString();

        string PasswordGmail = "XXXXXX";

        foreach (char item in PasswordGmail.ToCharArray())
        {
            psw.AppendChar(item);
        }

        client.Credentials = new System.Net.NetworkCredential("XXXX@xxx.com", psw); 

Upvotes: 0

Pawr
Pawr

Reputation: 141

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@domain.com", "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.

Upvotes: 10

ozkary
ozkary

Reputation: 2704

When doing this integration on a third party hosting provider, you also need provide the domain name.

SmtpClient client = new SmtpClient(SMTPSettings.Url, SMTPSettings.Port);
client.Credentials = new System.Net.NetworkCredential(SMTPSettings.UserName, SMTPSettings.Password, SMTPSettings.Domain);

Upvotes: 1

08Dc91wk
08Dc91wk

Reputation: 4318

Although the workaround I mentioned in the answer update did work, I was not happy about manually fetching those values. The solution for me was to remove the line

smtpClient.UseDefaultCredentials = true;

from the original code I posted. It turns out that smtpClient is initialized with the default credentials I set up in the web.config, and the above removed line was overwriting them with empty strings from CredentialCache.DefaultCredentials. I still don't know why CredentialCache.DefaultCredentials is empty or when this is supposed to be populated from the web.config, but this was the source of my problem.

If anyone has any further insight into this please post a better answer!

Upvotes: 8

Related Questions