views:

162

answers:

2

Hi,

I'm experiencing a to me mysterious error when sending e-mails through a SMTP-server from an ASP.NET web application. I get the famous error "unable to relay for [email protected]". What's mysterious to me is that when I cut and paste the exact same code that sends the e-mail into an usual .NET Windows Forms application, send the e-mail with this application, it all works just fine. This made me think that perhaps the problem is that the ASP.NET application runs as NETWORK SERVICE while the Windows Forms application runs on a domain user account, but it turns out that I have another ASP.NET application sending e-mail through the same SMTP-server running under NETWORK SERVER at the same IIS, and this application does not experience this problem.

I've further tried to send e-mails through the SMTP-server manually by telnet the smtp-server on port 25 and running the SMTP-protocol manually, and it all works fine. The SMTP-server is not configured with any kind of authentication or SSL.

Another mysterious fact is that the ASP.NET application can send e-mails from an adress within the same domain to an e-mail adress within the same domain, but not to any adress outside of the domain. But the Windows Forms application, that uses the exact same code, can send e-mails from any adress to any adress both within AND outside of the domain.

So to summarize:

  • The ASP.NET application can send e-mails from addresses within the domain to adresses within the domain, but not to addresses outside of the domain.
  • A Windows Forms application running the same code on the same computer can send e-mails from ANY address to ANY address.
  • Another ASP.NET application on the same IIS running under the same account (NETWORK SERVICE) can send e-mails using the same SMTP-server from ANY adress to ANY adress.
  • There is no authentication configured on the SMTP-Server.
  • Both the ASP.NET application and the Windows Forms application utilizes the System.Net.Mail.SmtpClient class to send a System.Net.Mail.MailMessage.

The code that sends the e-mail massage is:

private void button1_Click(object sender, EventArgs e)
        {
            MailMessage mesasge = new MailMessage(txtFrom.Text, txtTo.Text, "Test mail", txtBody.Text);
            SmtpClient client = new SmtpClient();
            if (!(string.IsNullOrEmpty(txtUserName.Text)))  //Is false since txtUserName.Text is empty
                client.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
            client.EnableSsl = false;
            client.Host = txtServer.Text;
            client.Port = 25;
            try
            {
                client.Send(mesasge);
            }
            catch (Exception ex)
            {
                txtResponse.Text = ex.Message;
            }
        }

As far as I can understand, this should be a matter of configuration rather than coding issues. Do anyone have an idea what the problem might be, and perhaps how this could be solved? Thanx!

A: 

Do you have impersonate = true in the web.config?

Doing this:

new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);

As far as I know requires that the web.config be set to allow impersonate.

Try adding this to you web.config (if you dont have it there)

    <system.web>
    <identity impersonate="true"  />
Rihan Meij
I'm not specifying any credentials. The if (!(string.IsNullOrEmpty(txtUserName.Text))) is false so those lines are never hit. However, I did do the suggested entry in the web.config file. However, the problem remained. Thank you for the tip anyway!
Clean
Okay :( Well what normally happend on my side, if you do use that method to authenticate and you dont have the identity impersonate=true, then the Active directory ticket is invalid, and will give you things like invalid userid/password errors. I have also often troubleshooted SMTP issues, with just telnet commands, but it does not sound like this will help you at all, because it works via the windows app.
Rihan Meij
+1  A: 

Hi, This most definitely sounds like an authentication issue or, you are accidently using 2 different SMTP servers.

I recommend you enable logging in both applications, and view the log. When you view the log, you should be able to see the differences, and hopefully trace it back to your code.

To enalbe logging in System.Net.Mail, you need to add some entries to your .config file.

<configuration>
  <system.diagnostics>
    <trace autoflush="true" />

    <sources>

      <source name="System.Net" >
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

      <source name="System.Net.Sockets">
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

    </sources>


    <sharedListeners>
      <add
        name="MyTraceFile"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="System.Net.trace.log"                />
    </sharedListeners>

    <switches>
      <add name="System.Net" value="Verbose" />
      <add name="System.Net.Sockets" value="Verbose" />
    </switches>
 </configuration>

Here is a link with more info:

http://systemnetmail.com/faq/4.10.aspx

Cheers!

Dave

dave wanta
Thank you for this suggestion. This was new to me and I'm sure I'll find use for it on many coming occasions. This worked wonderful on my dev machine, however, it did not work in my customers production environment. There were no log written to the filesystem. In production the application runs on a Windows Server 2003 machine on IIS 6. The application runs within an application pool as NETWORK SERCVICE, perhaps this has something to do with the log file not being written?
Clean
It's possible your log file ended up in your System32 directory.Try had coding the path. So rather than using System.Net.trace.log, try: c:\System.Net.trace.logIf that doesn't work, manually create the file in notepad, and give the network service permissions to write to it.
dave wanta
Thanx for the advice. I checked in the System32 directory, but it's not there. I also c:\System.Net.trace.log, but no success. I also tried to manually create the file in notepad and give network service permissions to write to it, but still no success.
Clean
hmm...depending upon how busy your server is, you may want to use something like Filemon to see if/where your file is trying to be created. -- Dave
dave wanta