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

Discussion in 'ASP .Net' started by Jim in Arizona, Jan 17, 2006.

  1. 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", "", "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", "",
    "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
     
    Jim in Arizona, Jan 17, 2006
    #1
    1. Advertising

  2. > 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("", whatever...) instead. Other than that it
    should work (unless your Exchange server requires extra
    authentication).
     
    Paul Henderson, Jan 17, 2006
    #2
    1. Advertising

  3. "Paul Henderson" <> wrote in message
    news:...
    >> 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("", 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("", "", "Test
    Sub", "Test Body")

    End Sub
     
    Jim in Arizona, Jan 17, 2006
    #3
  4. > 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
     
    Paul Henderson, Jan 17, 2006
    #4
  5. "Paul Henderson" <> wrote in message
    news:...
    >> 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("", "",
    "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.
     
    Jim in Arizona, Jan 17, 2006
    #5
  6. > 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...
     
    Paul Henderson, Jan 17, 2006
    #6
  7. "Paul Henderson" <> wrote in message
    news:...
    >> 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.
     
    Jim in Arizona, Jan 17, 2006
    #7
  8. > 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.
     
    Paul Henderson, Jan 18, 2006
    #8
  9. Re: .NET 2.0: Sending email on local domain to local exchange 2Kserver

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

    Thanks for the help!

    Jim
     
    Jim in Arizona, Jan 24, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    0
    Views:
    493
    Brian Henry
    Oct 3, 2003
  2. Brian Henry

    Sending an email to exchange server

    Brian Henry, Oct 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,719
    MS Newsgroups
    Oct 22, 2003
  3. Gilbert Cheung

    Sending Email via Exchange Server from ASP.NET

    Gilbert Cheung, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,187
    Christopher Reed
    Mar 25, 2006
  4. =?Utf-8?B?RG9taW5v?=

    Sending email in VB .Net via Exchange Server 2007

    =?Utf-8?B?RG9taW5v?=, Jun 19, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    4,166
    MasterGaurav \(www.edujini-labs.com\)
    Jun 24, 2007
  5. Brent Burkart

    asp.net sending email using exchange server

    Brent Burkart, Aug 18, 2003, in forum: ASP General
    Replies:
    1
    Views:
    170
    Aaron Bertrand - MVP
    Aug 18, 2003
Loading...

Share This Page