.NET 2.0: Sending email on local domain to local exchange 2K server

J

Jim in Arizona

I was trying to have an email sent when the page loads but having some
trouble with it.

Our server is an IIS 6.0 with Framework 2.0. It is set to windows
authentication (not anonymous).

I don't really know what I'm doing and so far my tests haven't been so good.
Here's the code I tried:

Dim test As New System.Net.Mail.SmtpClient
test.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
test.UseDefaultCredentials = True
test.Host = "mailserver"
test.Send("Jim Morrison", "(e-mail address removed)", "Test Sub", "Test
Body")

The error message I get is:

The specified string is not in the form required for an e-mail address.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.FormatException: The specified string is not in
the form required for an e-mail address.

Source Error:


Line 16: test.UseDefaultCredentials = True
Line 17: test.Host = "exfs"
Line 18: test.Send("Jim Morrison", "(e-mail address removed)",
"Test Sub", "Test Body")
Line 19:
Line 20: End Sub


Source File: c:\inetpub\wwwroot\tests\winauth1.aspx.vb Line: 18

Stack Trace:


[FormatException: The specified string is not in the form required for an
e-mail address.]
System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset,
String& displayName) +872611
System.Net.Mail.MailAddress.ParseValue(String address) +245
System.Net.Mail.MailAddress..ctor(String address, String displayName,
Encoding displayNameEncoding) +87
System.Net.Mail.Message..ctor(String from, String to) +127
System.Net.Mail.MailMessage..ctor(String from, String to) +117
System.Net.Mail.SmtpClient.Send(String from, String recipients, String
subject, String body) +31
tests_winauth1.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\tests\winauth1.aspx.vb:18
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
Version:2.0.50727.42

TIA,
Jim
 
P

Paul Henderson

The error message I get is:
The specified string is not in the form required for an e-mail address.

That probably means your 'for' field (first parameter to test.Send) is
not a correctly formed email address; use (e.g.)
test.Send("(e-mail address removed)", whatever...) instead. Other than that it
should work (unless your Exchange server requires extra
authentication).
 
J

Jim in Arizona

Paul Henderson said:
That probably means your 'for' field (first parameter to test.Send) is
not a correctly formed email address; use (e.g.)
test.Send("(e-mail address removed)", whatever...) instead. Other than that it
should work (unless your Exchange server requires extra
authentication).

You're right, I had to change it to a valid email address. When I did, I got
a new error:

Client does not have permission to submit mail to this server. The server
response was: 5.7.3 Client was not authenticated.

I don't know how I would authenticate it properly. Do you or anyone else
reading this have any experience with domain authentication? I read this
article but I couldn't follow it very well; one reason is that the coding
examples were in C# and I only use VB. I tried some of it (see code below):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/pagexplained0001.asp

The full code of my email attempt is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim WinID As WindowsIdentity = HttpContext.Current.User.Identity
Dim ctx As WindowsImpersonationContext = Nothing

ctx = WinID.Impersonate()

Dim test As New System.Net.Mail.SmtpClient
test.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
test.UseDefaultCredentials = True
test.Host = "exfs"
test.Send("(e-mail address removed)", "(e-mail address removed)", "Test
Sub", "Test Body")

End Sub
 
P

Paul Henderson

I don't know how I would authenticate it properly. Do you or anyone else
reading this have any experience with domain authentication?

If you have IIS set to do Integrated Windows Authentication, and
ASP.NET set up to impersonate for the relevant area of the application
(<identity impersonate="true" /> or similar) in web.config, then the
page should automatically run under the impersonation context of the
requesting user, and so you shouldn't need to do what you are with
WindowsIdentity.Impersonate (effectively, by passing
HttpContext.Current.User.Identity, you are telling it to impersonate
whoever is being impersonated, i.e. to do nothing). I should also
perhaps point out that you should stop the impersonation (using Revert)
at the end of the sub, or other code could start failing as it's
running under a strange user context.

So, check that your web.config is set up to impersonate, and also
perhaps check that WindowsIdentity.GetCurrent().Name matches the
username of the connected domain user. If both are fine, then your
Exchange server is likely set up *particularly* strangely, and isn't
just using Kerberos (integrated) authentication, but needs some other
credentials
 
J

Jim in Arizona

Paul Henderson said:
If you have IIS set to do Integrated Windows Authentication, and
ASP.NET set up to impersonate for the relevant area of the application
(<identity impersonate="true" /> or similar) in web.config, then the
page should automatically run under the impersonation context of the
requesting user, and so you shouldn't need to do what you are with
WindowsIdentity.Impersonate (effectively, by passing
HttpContext.Current.User.Identity, you are telling it to impersonate
whoever is being impersonated, i.e. to do nothing). I should also
perhaps point out that you should stop the impersonation (using Revert)
at the end of the sub, or other code could start failing as it's
running under a strange user context.

So, check that your web.config is set up to impersonate, and also
perhaps check that WindowsIdentity.GetCurrent().Name matches the
username of the connected domain user. If both are fine, then your
Exchange server is likely set up *particularly* strangely, and isn't
just using Kerberos (integrated) authentication, but needs some other
credentials

I went into my web.config file in the root directory and added what you
listed, so now my web config looks like this:

<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<identity impersonate="true" />
<customErrors mode="Off"/>
<compilation debug="true"/>
</system.web>
</configuration>

I also simplified my aspx.vb code to this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Dim test As New System.Net.Mail.SmtpClient

test.Host = "exfs"
test.Send("(e-mail address removed)", "(e-mail address removed)",
"Test Sub", "Test Body")

Catch ex As Exception
Response.Write(HttpContext.Current.User.Identity.Name.ToString)
End Try
End Sub

The exception is still thrown because the catch appears, which does show my
current AD logon (HHS\jmorrison).

Our exchange server, which is an Exchange 2000 Standard, is set up basic. We
haven't made any unusual changes to it. In fact, I can't think of any
changes made at all after its initial install.

Our IIS server (IIS 6.0), has authentication set to Integrated Windows
Authentication and anonymous access disabled. Digest Authentication for
Windows Domain Servers is not checked though. Would that matter?

So, when I load the webpage (called winauth1.aspx) into my web browser from
a computer on our domain (my workstation), it should use my username to
authenticate to our exchange server, right? But it doesn't for some reason.

Our exchange server is not connected to the outside world. It just acts as
our mail server for our LAN.

Thanks.
 
P

Paul Henderson

The exception is still thrown because the catch appears, which does show my
current AD logon (HHS\jmorrison).

Hmm, that's interesting [I'm guessing you've checked it's still the
*same* exception].
Digest Authentication for Windows Domain Servers is not checked though
Would that matter?

It shouldn't, no; it just provides a less secure alternative when
Integrated authentication is not appropriate, but you don't need it
here.
So, when I load the webpage (called winauth1.aspx) into my web browser from
a computer on our domain (my workstation), it should use my username to
authenticate to our exchange server, right? But it doesn't for some reason.

Yes, it should (basically). I suspect that it is sending the
credentials now, but the Exchange server is not liking it; the code you
have now is 'correct', as far as I can see. You might want to take a
look at the Exchange server's settings, and see what sort of
authentication it's expecting (e.g. if it wants plaintext [basic] or
digest authentication, then ASP probably won't be able to talk to it
directly like this; you should however be able to tell it to use
Integrated authentication itself, which it should really be doing
anyway).

Other than that, I don't think I can help, but do post again if any new
evidence comes to light...
 
J

Jim in Arizona

Paul Henderson said:
The exception is still thrown because the catch appears, which does show
my
current AD logon (HHS\jmorrison).

Hmm, that's interesting [I'm guessing you've checked it's still the
*same* exception].
Digest Authentication for Windows Domain Servers is not checked though
Would that matter?

It shouldn't, no; it just provides a less secure alternative when
Integrated authentication is not appropriate, but you don't need it
here.
So, when I load the webpage (called winauth1.aspx) into my web browser
from
a computer on our domain (my workstation), it should use my username to
authenticate to our exchange server, right? But it doesn't for some
reason.

Yes, it should (basically). I suspect that it is sending the
credentials now, but the Exchange server is not liking it; the code you
have now is 'correct', as far as I can see. You might want to take a
look at the Exchange server's settings, and see what sort of
authentication it's expecting (e.g. if it wants plaintext [basic] or
digest authentication, then ASP probably won't be able to talk to it
directly like this; you should however be able to tell it to use
Integrated authentication itself, which it should really be doing
anyway).

Other than that, I don't think I can help, but do post again if any new
evidence comes to light...

I checked the properties of the SMTP virtual server on the exchange server.
The authentication was set to Basic and Integrated Windows Authentication. I
unchecked the Basic auth and tried again and I sitll got the same error.

What if we tried coding a different way, like providing a specific set of
credentials in the VB code? I was going to try to use the credentials
property,( ie: test.credentials) but I couldn't figure out how to use it, or
even if I should try.

Thanks for your persistant help.
 
P

Paul Henderson

What if we tried coding a different way, like providing a specific set of
credentials in the VB code? I was going to try to use the credentials
property,( ie: test.credentials) but I couldn't figure out how to use it, or
even if I should try.

Yes, there's a chance that might work. You need to create new
System.Net.NetworkCredential object and set test.Credentials equal to
it, and also to turn off test.UseDefaultCredentials. The constructor
for NetworkCredential takes three parameters: username, password,
domain. Then, it should use those credentials for the authentication.
That should also work if your Exchange server is switched to Basic
authentication only; it might be worthwhile using that rather than
Integrated for a test to see whether it works any better.
 
J

Jim in Arizona

Paul said:
Yes, there's a chance that might work. You need to create new
System.Net.NetworkCredential object and set test.Credentials equal to
it, and also to turn off test.UseDefaultCredentials. The constructor
for NetworkCredential takes three parameters: username, password,
domain. Then, it should use those credentials for the authentication.
That should also work if your Exchange server is switched to Basic
authentication only; it might be worthwhile using that rather than
Integrated for a test to see whether it works any better.

Success Paul! Here's the code that did the trick:

Dim test As New System.Net.Mail.SmtpClient
Dim nco As New System.Net.NetworkCredential("webapplications",
"password", "domain")
test.Credentials = nco
test.Host = "exfs"
test.Send("(e-mail address removed)", "(e-mail address removed)", "Test
Subject", "Test Message Body")

Thanks for the help!

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top