Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > .NET 2.0: Sending email on local domain to local exchange 2K server

Reply
Thread Tools

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

 
 
Jim in Arizona
Guest
Posts: n/a
 
      01-17-2006
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 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 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(Stri ng 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


 
Reply With Quote
 
 
 
 
Paul Henderson
Guest
Posts: n/a
 
      01-17-2006
> 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 Removed)", whatever...) instead. Other than that it
should work (unless your Exchange server requires extra
authentication).

 
Reply With Quote
 
 
 
 
Jim in Arizona
Guest
Posts: n/a
 
      01-17-2006
"Paul Henderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> 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 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/de...lained0001.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 Removed)", "(E-Mail Removed)", "Test
Sub", "Test Body")

End Sub


 
Reply With Quote
 
Paul Henderson
Guest
Posts: n/a
 
      01-17-2006
> 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

 
Reply With Quote
 
Jim in Arizona
Guest
Posts: n/a
 
      01-17-2006

"Paul Henderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> 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


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 Removed)", "(E-Mail Removed)",
"Test Sub", "Test Body")

Catch ex As Exception
Response.Write(HttpContext.Current.User.Identity.N ame.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.





 
Reply With Quote
 
Paul Henderson
Guest
Posts: n/a
 
      01-17-2006
> 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...

 
Reply With Quote
 
Jim in Arizona
Guest
Posts: n/a
 
      01-17-2006
"Paul Henderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> 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.


 
Reply With Quote
 
Paul Henderson
Guest
Posts: n/a
 
      01-18-2006
> 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.

 
Reply With Quote
 
Jim in Arizona
Guest
Posts: n/a
 
      01-24-2006
Paul Henderson wrote:
>> 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.
>


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 Removed)", "(E-Mail Removed)", "Test
Subject", "Test Message Body")

Thanks for the help!

Jim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a server on one domain the domain controller of a new domain Limited Wisdom MCSA 7 09-13-2006 02:18 AM
sending an email to a exchange server (from inside the local network) Brian Henry ASP .Net 0 10-03-2003 07:49 PM



Advertisments