Why is MailMessage sent to any address from Windows Forms application, but not from ASP.NET application, when both are using the same SMTP-server?

StackOverflow https://stackoverflow.com/questions/2145437

Question

I'm experiencing a mysterious (to me) error when sending e-mails through a SMTP-server from an ASP.NET web application. I get the famous error "unable to relay for xxx@yyy.zzz". 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 address within the same domain to an e-mail address within the same domain, but not to any address outside of the domain. But the Windows Forms application, that uses the exact same code, can send e-mails from any address to any address both within AND outside of the domain.

So to summarize:

  • The ASP.NET application can send e-mails from addresses within the domain to addresses 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 address to ANY address.
  • 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!

Was it helpful?

Solution

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

OTHER TIPS

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"  />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top