8

Our users can send emails from our ASP.NET Web application on the click of a button. In our testing environment, there is a strange thing going on.

The first time the application tries to send the email, we get the exception:

Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender

The funny thing is, when the user clicks on the button again (so the application tries to send the email again), it works, and the email is sent.

If you wait a while, you'll get the error again, but clicking for the second time will send the email without problems. If you would be fast enough to send a new email, it would work.

This is the (simplified) code we use:

Dim smtpClient As New SmtpClient(<smtp mail server goes here>)
smtpClient.UseDefaultCredentials = True
mailMessage.From = New MailAddress(<from address>)
smtpClient.Send(mailMessage)

This does work in the production environments though. We're using Microsoft Exchange, and so are our customers (the production environments).

Has anyone ever had anything similar (error the first time, works like a charm the second time)?

Peter
  • 196
  • 1
  • 1
  • 9

4 Answers4

2

I've seen this happen a few times before and here are the causes I have seen. Keep in mind these have only been seen in a large environment. If you are in a single dc, single exchange server environment these will not apply.

First off here is the MS article on this: http://support.microsoft.com/kb/895853. Under the possible cuases you can see there are a lot of things at play.

One of your exchange servers relay permissions are not setup correctly.

This is most likley the cause. The mail may be taking a different route on the first and second try, while this might sound silly run message tracking on both messages, see if they go through the same server. If you see different paths check the settings on the bad one, could be a simple allow relay for authenticated users isnt checked.

AD permissions are wrong

You must have Send As permissions on the account that has the from address from your email. If it is working sometimes, but not others then it may be different pathings on mail routing, with different permissions between them.

Authentication isn't working correctly.

In the first instance of the script it may be that authentication fails, and in this instance the mail is denied as you do not have permission. In the second run of the script authentication is successfull. Unfortunately I do not know ASP well enough to understand the code above or advise on how to fix it. smtpClient.UseDefaultCredentials may not be the correct code for your environment. This will only authenticate if requested by the server and will use the credentials of the locally logged on user. If the server isn't asking for authentication the first time around then you wont be authed and may not have permission to send. Try changing it to some code that always authenticates.

One of your DCs or global catalogs is out of date or has bad information.

This is rare but it is possible that your domain controllers have out of sync information. Double check the server information on the from and to accounts via ldap or adsi edit and ensure they are identical across all dcs.

In all instances check the logs on the exchange server and ensure SMTP logging is enabled with all fields. Use message tracking to see exactly which servers this message is hitting.

pablo
  • 3,040
  • 1
  • 19
  • 23
1

It looks to me like you may have SMTP authentication enabled on the mail server but haven't included it in the code. In such a case the first time you try to send a message it effectively performs the authentication but fails to send the message. The next time the user is authenticated and the message goes through.

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
1

For me the problem was my config file already had the "From" account specified. When I tried to do mailMessage.From = New MailAddress(<from address>) in my code, it threw up because the "from" email in my code didn't match the "from" email in my config file.

codeMonkey
  • 111
  • 3
-1

Try this :

    Dim client = New SmtpClient(<smtp mail server goes here>)
    MailerManager.ServerSMTP = client.Host.ToString()
    Try
        client.Send(Mail)
    Catch ex As Exception
        'Error Message
    End Try