tags:

views:

346

answers:

6

I'm trying to send an email to an external address as part of a web app. I can send an email fine when using a simple executable running on the server:

private void button1_Click(object sender, EventArgs e)
    {
        MailMessage message = new MailMessage(welcomeMessageFrom, toAddress, welcomeMessageSubject, welcomeMessageSubject);
        SmtpClient emailClient = new SmtpClient("mail.sortuv.com");

        System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(username, password);
        emailClient.UseDefaultCredentials = false;
        emailClient.Credentials = SMTPUserInfo;
        emailClient.Send(message);
    }

However, trying the same code from an ASP.NET page gives the following exception:

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for <user's email>

I'm new to IIS but do you have suggestions on how to debug?

UPDATE: I had to specify the domain for the user as well. Still not sure why a regular .exe was ok without it. Hope this helps someone.

A: 

Try adding

emailClient .DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
zSysop
Thanks -- I forgot to mention that I tried this, but our Exchange server doesn't seem to support it. I'll see if this can be enabled.
kurious
A: 

Try setting this property before sending out:

emailClient.DeliveryMethod  = SmtpDeliveryMethod.PickupDirectoryFromIis
Jay Riggs
+1  A: 

Seems a credential issue. The normal exe runs under your account. The ASP.NET application run under the NETWORK SERVICE in Windows Server and ASPNET under Windows XP. You have to use other credentials in order to successfully send email from ASP.NET

Albert
Thanks, I'll give this a shot.
kurious
A: 

Hi, Are you setting anything your web.config, relating to mail parameters? IIRC, the web.config will override your code settings.

Also, something else you can do, is to enable logging, to actually see what the SmtpClient is sending.

You need to add some values to your .config file. Here is an example:

<configuration>

<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>

Here is a link with more info: http://systemnetmail.com/faq/4.10.aspx

Cheers!

Dave

www.advancedintellect.com

dave wanta
Thanks Dave! I really appreciate the help as this is showing me how to debug a whole class of problems :)
kurious
A: 

Is this for IIS7 as I had a simular issue I had to enable the smtp service on the server

And set up the mail for each domain on there

Paul
Yep, it's for IIS7 -- thanks for the tip.
kurious
A: 

Thanks for all the help guys, I just figured it out. I had to specify the domain:

SmtpClient emailClient = new SmtpClient(servername);

            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(name, pass);
            SMTPUserInfo.Domain = domain; // i.e. "foo.com"
            emailClient.UseDefaultCredentials = false;
            emailClient.Credentials = SMTPUserInfo;
            emailClient.Send(message);
kurious