 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

April 2nd, 2012, 10:23 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 2nd, 2012, 10:31 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 2nd, 2012, 10:37 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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("[email protected]", "Nick");
myMessage.To.Add(new MailAddress("[email protected]", "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
|
|

April 2nd, 2012, 11:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 2nd, 2012, 12:10 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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="[email protected]"/>
<add key="FromName" value="Planet Wrox"/>
<add key="ToAddress" value="[email protected]"/>
<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 <[email protected]>">
<network enableSsl="true" userName="[email protected]" password="" host="smtp.gmail.com" />
Cheers Imar,
Nick
Last edited by Nick Makin; April 2nd, 2012 at 12:11 PM..
Reason: missed end off
|
|

April 2nd, 2012, 12:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 2nd, 2012, 02:37 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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="[email protected]"/>
<add key="FromName" value="Planet Wrox"/>
<add key="ToAddress" value="[email protected]"/>
<add key="ToName" value="Me"/>
<add key="SendMailOnError" value="true"/>
</appSettings>
Thanks Imar,
Nick
|
|

April 2nd, 2012, 02:50 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 11th, 2012, 08:30 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 43
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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!
|
|
 |
|