Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP 2.0, first chance exception, Failure sending mail

Status
Not open for further replies.

dldev

Programmer
Sep 4, 2007
33
0
0
US
Hello,

When trying to send mail through my application I am getting this error:

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll System.Net.Mail.SmtpException: Failure sending mail.

I am using ASP.NET 2.0 and the project is in Visual Studio 2005 Standard. I've seen many others having problems with this as I've gone through and researched online, but no definitive answers. Seems that one possibility is that the 2.0 framework uses enhanced smtp (EHLO) with the System.Net.Mail api so I wonder if it might be that the smtp server is not configured to handle this. I've checked throug however, and cannot find anything to support this theory. In any case, if anyone has been able to solve this issue could you please share your thoughts?

Thanks,
Dennis
 
Here is the code:

Code:
mailer.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;

namespace Referral
{
    public class Mailer
    {
        private static string MailFrom = ConfigurationManager.AppSettings["MailFrom"];
        private static string MailSubject = ConfigurationManager.AppSettings["MailSubject"];
        private static string MailHost = ConfigurationManager.AppSettings["MailHost"];
        private static string MailUsername = ConfigurationManager.AppSettings["MailUserName"];
        private static string MailPassword = ConfigurationManager.AppSettings["MailPassword"];

        public static void Send(MailMessage message)
        {
            if (message.From == null)
            {
                message.From = new MailAddress(MailFrom);
            }
            if (string.IsNullOrEmpty(message.Subject))
            {
                message.Subject = MailSubject;
            }

            SmtpClient client = new SmtpClient();
            client.Host = MailHost;
            if (!string.IsNullOrEmpty(MailUsername))
            {
                client.Credentials = new NetworkCredential(MailUsername, MailPassword);
            }

            bool isLocal = true;

            bool.TryParse(ConfigurationManager.AppSettings["IsLocal"], out isLocal);

            if (!isLocal)
            {
                client.Send(message);
            }
        }
    }
    }

Code:
web.config:
<?xml version="1.0"?>

<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </configSections>
  <dataConfiguration defaultDatabase="referralConnectionString" />

  <appSettings>
    <add key="IsLocal" value="false" />
    <add key="MailFrom" value="test@test.com" />
    <add key="MailTo" value="me@me.com" />
    <add key="MailSubject" value="Test Mail with HTML file" />
    <add key="MailFormat" value="HTML" />
    <add key="MailHost" value="localhost" />
    <add key="MailUserName" value="" />
    <add key="MailPassword" value="" />
    <add key="Url" value="[URL unfurl="true"]http://localhost/referral/default.aspx?id="/>[/URL]
  </appSettings>

  <connectionStrings>
    <add name="referralConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\referral-intake.mdb;Persist Security Info=True;" providerName="System.Data.OleDb"/>
  </connectionStrings>
  
    <system.web>
        <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
        <compilation debug="true" />
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
      <authentication mode="Forms">
        <forms loginUrl="~/Login.aspx"  defaultUrl="~/admin/List.aspx" />
      </authentication>
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
  <location path="Admin">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization >
    </system.web>
  </location>
    
</configuration>
 
you need to catch the exception and check the inner exception. also you can use the system.net.mail web.config settings instead of all the app settings.

check out for more information.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top