Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 2nd, 2012, 10:23 AM
Authorized User
Points: 170, Level: 3
Points: 170, Level: 3 Points: 170, Level: 3 Points: 170, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
Default E-mail problem

Hi, recently finished the topics of the book so started to go back and fix problems that have popped up along the way, the last (i hope) problem i've got is the email in my Contact form. It wont send and the error message says there is a problem but I can't figure out what.

The email functionality works in my Email.aspx page in my Pages folder and my Email.aspx page in my Demo folder.

Am I right in then thinking that there must be a problem with either the send button in the contact form, because the web.config file must be working with regards to the other exercises.

Any help would be appreciated,

Nick
Reply With Quote
  #2 (permalink)  
Old April 2nd, 2012, 10:31 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
Am I right in then thinking that there must be a problem with either the send button in the contact form
Yes, I think so. Can you post the code for the page that works, and for the code of the User Control? Can you also post your error message?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old April 2nd, 2012, 10:37 AM
Authorized User
Points: 170, Level: 3
Points: 170, Level: 3 Points: 170, Level: 3 Points: 170, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
Default

Thanks for the help again Imar, here is the code now:
Contact form send button..
Code:
  protected void SendButton_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
      string mailBody = File.ReadAllText(fileName);

      mailBody = mailBody.Replace("##Name##", Name.Text);
      mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
      mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
      mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
      mailBody = mailBody.Replace("##Comments##", Comments.Text);

      try
      {
          MailMessage myMessage = new MailMessage();
          myMessage.Subject = "Response from web site";
          myMessage.Body = mailBody;

          myMessage.From = new MailAddress(AppConfiguration.FromAddress,AppConfiguration.FromName);
          myMessage.To.Add(new MailAddress(AppConfiguration.ToName,AppConfiguration.ToName));

          SmtpClient mySmtpClient = new SmtpClient();
          mySmtpClient.Send(myMessage);

          Message.Visible = true;
          MessageSentPara.Visible = true;
          FormTable.Visible = false;
          System.Threading.Thread.Sleep(5000);
      }
      catch
      {
          Message.Text = "An error occurred while sending your e-mail. Please try again";
      }
      finally
      {
          Message.Visible = true;
      }
    }
  }
Demo email example..
Code:
public partial class Pages_Email : BasePage
{
  protected void Page_Load(object sender, EventArgs e)
  {
    MailMessage myMessage = new MailMessage();
    myMessage.Subject = "Test Message";
    myMessage.Body = "Hello world, from Planet Wrox";
    myMessage.From = new MailAddress("nick.makin91@gmail.com", "Nick");
    myMessage.To.Add(new MailAddress("nick.makin@btinternet.com", "Nick Makin BT"));

    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);
  }
}
Error message:
The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Thanks Imar,

Nick
Reply With Quote
  #4 (permalink)  
Old April 2nd, 2012, 11:50 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Nick,

Try removing the UpdatePanel and its ContentTemplate from the user control. That should give you the real error. Maybe the file cannot be read, or one of the addresses in web.config is invalid?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old April 2nd, 2012, 12:10 PM
Authorized User
Points: 170, Level: 3
Points: 170, Level: 3 Points: 170, Level: 3 Points: 170, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
Default

I have checked the addresses in the web.config file and im sure they are ok, here are the settings for the appconfiguration just incase there is a problem with one of them..
Code:
    <appSettings>
        <add key="FromAddress" value="nick.makin91@gmail.com"/>
        <add key="FromName" value="Planet Wrox"/>
        <add key="ToAddress" value="nick.makin@btinternet.com"/>
        <add key="ToName" value="Me"/>
        <add key="SendMailOnError" value="true"/>
    </appSettings>
I tried to remove the Update Panel, but by doing that it brought up a load more errors saying it cant find the namespaces of the items that are in the table but thats it really, unless I havent done something correctly.

Here is the smtp code from my web.config, im sure this is ok but you never know..
Code:
       <smtp deliveryMethod="Network" from="Nick Makin &lt;nick.makin91@gmail.com&gt;">
            <network enableSsl="true" userName="nick.makin91@gmail.com" password="" host="smtp.gmail.com" />
Cheers Imar,

Nick

Last edited by Nick Makin; April 2nd, 2012 at 12:11 PM.. Reason: missed end off
Reply With Quote
  #6 (permalink)  
Old April 2nd, 2012, 12:51 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Can you post to more things?

1. The User Control code with the UpdatePanel removed? You should be able to remove it without problems.

2. The code for your AppSettings class?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old April 2nd, 2012, 02:37 PM
Authorized User
Points: 170, Level: 3
Points: 170, Level: 3 Points: 170, Level: 3 Points: 170, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
Default

Yeah ofcourse, here they are:
User control:
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="Controls_ContactForm" %>
<% if (false)
   { %>
    <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<% } %>
<style type="text/css">
  .style1
  {
    width: 100%;
  }
</style>
<script type="text/javascript">
  function ValidatePhoneNumbers(source, args)
  {
    var phoneHome = document.getElementById('<%= PhoneHome.ClientID %>');
    var phoneBusiness = document.getElementById('<%= PhoneBusiness.ClientID %>');
    if (phoneHome.value != '' || phoneBusiness.value != '')
    {
      args.IsValid = true;
    }
    else
    {
      args.IsValid = false;
    }
  }
</script>

<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
  <ProgressTemplate>
    <div class="PleaseWait">Please Wait... </div>
  </ProgressTemplate>
</asp:UpdateProgress>
<script src="../Scripts/jquery.updnWatermark.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function ()
  {
    $.updnWatermark.attachAll({ cssClass: 'Watermark' });
    $('form').bind('submit', function ()
    {
      if (Page_IsValid)
      {
        $('#TableWrapper').slideUp(3000);
      }
    });
  });

  function pageLoad()
  {
    $('.Attention').animate({ width: '600px' }, 3000).animate({ width: '100px' }, 3000).fadeOut('slow');
  }
</script>
App settings:
Code:
   <appSettings>
        <add key="FromAddress" value="nick.makin91@gmail.com"/>
        <add key="FromName" value="Planet Wrox"/>
        <add key="ToAddress" value="nick.makin@btinternet.com"/>
        <add key="ToName" value="Me"/>
        <add key="SendMailOnError" value="true"/>
    </appSettings>
Thanks Imar,

Nick
Reply With Quote
  #8 (permalink)  
Old April 2nd, 2012, 02:50 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Where did the table with the input controls go? Those are needed for the form to function correctly. You should have only removed the surrounding UpdatePanel, its Content Template tags (not its content) and the UpdateProgress. Can you try that and then see what error you get?

And with AppSettings class I meant the code that accesses the web.config; not the settings from the config file itself. It should be a class file in your App_Code folder.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old April 11th, 2012, 08:30 AM
Authorized User
Points: 170, Level: 3
Points: 170, Level: 3 Points: 170, Level: 3 Points: 170, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
Default

Sorry for the late reply Imar, exam season!

I must of took it out by accident, ill go back and correct it.

Here is the code for the AppConfiguration.cs file :
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

/// <summary>
/// Summary description for AppConfiguration
/// </summary>
public class AppConfiguration
{
    public static string FromAddress
    {
        get
        {
            string result = WebConfigurationManager.AppSettings.Get("FromAddress");
            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }
            throw new Exception("AppSetting FromAddress not found in web.config file.");
        }
    }

    public static string FromName
    {
        get
        {
            string result = WebConfigurationManager.AppSettings.Get("FromName");
            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }
            throw new Exception("AppSetting FromName not found in web.config file.");
        }
    }

    public static string ToAddress
    {
        get
        {
            string result = WebConfigurationManager.AppSettings.Get("ToAddress");
            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }
            throw new Exception("AppSetting ToAddress not found in web.config file.");
        }
    }

    public static string ToName
    {
        get
        {
            string result = WebConfigurationManager.AppSettings.Get("ToName");
            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }
            throw new Exception("AppSettingToName not found in web.config file.");
        }
    }
    public static bool SendMailOnError
    {
        get
        {
            string result = WebConfigurationManager.AppSettings.Get("SendMailOnError");
            if (!string.IsNullOrEmpty(result))
            {
                return Convert.ToBoolean(result);
            }
            throw new Exception(
                                "AppSetting SendMailOnError not found in this web.config file");
        }
    }
}

Thanks Imar,

Nick

Last edited by Nick Makin; April 11th, 2012 at 08:34 AM.. Reason: pressed enter too early!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in sendig mail ronit ASP.NET 3.5 Professionals 1 July 7th, 2011 01:21 PM
Sending mail problem!!!! NitinJoshi General .NET 2 January 13th, 2005 08:31 AM
Mail Problem krigans VBScript 3 July 18th, 2004 08:08 PM
HTML Mail Problem hugh@kmcnetwork.com HTML Code Clinic 0 September 28th, 2003 10:15 AM
php mail() problem saicon Beginning PHP 2 August 18th, 2003 12:39 PM



All times are GMT -4. The time now is 05:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.