Microsoft’s .net mail libraries by default will use the NTAuth credentials of the user performing the action. Many Exchange and other mail servers that are configured to be more restrictive may reject sending emails via the .net library using the defaults. This post will show how to configure your application or code to use the Anonymous Logon account instead of the logged on user’s account.
An example error is below:
Exception in sending email: Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at SendEmail.Send(SendEmail* )
To overcome this issue in a C Sharp application, adjust the initiated SmtpClient to not use the default credentials, such as:
MailMessage mail = new MailMessage("firstname.lastname@example.org", "email@example.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.appadmintools.com";
mail.Subject = "Test Mail from C Sharp.";
mail.Body = "This is the mail body";
Via PowerShell, you should create a new PSCredential and use that with the Send-MailMessage Cmdlet, for instance:
$s = New-Object System.Security.SecureString
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $S
Send-MailMessage -From $fromaddress -To $mailtoaddress -Subject $messageheader -Body $messagebody -Attachments $attachments -Credential $creds
If you have an application that allows you to specify an account to use for SMTP communication, simply set it to use “NT Authority\Anonymous Logon” account, similar to the image below taken from System Center Orchestrator 2012R2.
Bottom line, if you are having issues sending emails and you confirmed all the settings, try to capture the network traffic you are using (default smtp port is 25), which should give you the error that is preventing the sending of emails.