Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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
 
Old September 2nd, 2010, 02:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You make this very easy for me. All I need to do is keep posting the same answer over and over again, as you keep posting the same error and same broken code ;-)

Quote:
I don't see you setting EnableSsl to true in your code.
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!
 
Old September 2nd, 2010, 11:51 PM
Authorized User
 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thanks for pointing that Imar. :)

However at step 12 on page 330 when i fill my contact form in and press send, it starts to say Please Wait.... and appears as if the message is sending but the debugger flashes up saying webpage error:
Line: 5
Error: Sys.WebForms.PageRequestManagerServerErrorExceptio n: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated


Heres my code as you will see I have made sure enable ssl is set to true. To clarify i successfully managed to send an email to my inbox as per page 310, step 5 but now after filling the contact form in and pressing send, the debugger comes up as please wait looks like it is being pressed and tells me the above error in the debugger. Strange.

Heres my code Imar. Many thanks.

Code:
protected void buttonSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string fileName = Server.MapPath("~/App_Data/ContactUs.txt");
            string mailBody = System.IO.File.ReadAllText(fileName);

            mailBody = mailBody.Replace("##Name##", textName.Text);
            mailBody = mailBody.Replace("##Email##", textEmailAddress.Text);
            mailBody = mailBody.Replace("##HomePhone##", homePhone.Text);
            mailBody = mailBody.Replace("##MobilePhone##", mobilePhone.Text);
            mailBody = mailBody.Replace("##Comments##", textComments.Text);

            MailMessage myMessage = new MailMessage();
            myMessage.Subject = "Query from Salford Student Books User";
            myMessage.Body = mailBody;

            myMessage.From = new MailAddress("leemark2k3@hotmail.com", "Farid Menziane");
            myMessage.To.Add(new MailAddress("leemark2k3@hotmail.com", "Lee Bell"));

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

            LabelMessage.Visible = true;
            FormTable.Visible = false;
            System.Threading.Thread.Sleep(4500);
        }

    }
Code:
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Lee Bell &lt;leemark2k3@hotmail.com&gt;">
        <network host="smtp.live.com" port="25"
        userName="leemark2k3@hotmail.com" password="*******" defaultCredentials="true"  />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
(ive deleted my password out above per your previous recommendations).
 
Old September 3rd, 2010, 02:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I am not sure I get it. Or I am not sure why you don't get it. First, you post the code and config for an alomost working implementation. All that was missing is EnableSsl. Then you fixed it. Then you posted new code with different config code, and different code which didn't work. Again, EnableSsl was missing. However, that doesn't completely fix it because you also broke your config code compared to the original example.

Again, you make it easy for me as all I need to do is simulate a broken record and post what I posted before earlier in this thread:

Quote:
You also need to take out defaultCredentials="true"; then it works for me with this config:
Two questions for you: did you actually read this post, and my recommendations before trying stuff out and posting here? And why would you mess up a perfectly working config section? Once you have one in your site that works, there's no need to change it for other examples. If later code doesn't work, it must be with the code, not with the config section as that used to work before...... I am little confused here.....

I try to help you where I can, but you seem to be making this very hard for yourself......

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!
The Following User Says Thank You to Imar For This Useful Post:
leemark2k3 (September 3rd, 2010)
 
Old September 3rd, 2010, 04:18 AM
Authorized User
 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Default

I sat and looked at it for over an hour wondering why id got it to work and send an email to my inbox but why it was suddenly not working again. Ive confused myself here i think because i remember specifically taking default credentials=true out of the web.config too and setting enable ssl to true so looking at the code i sent you am not quite sure how ive managed to have default credentials=true there.
Perhaps i hadnt saved it correctly at the time i thought id taken default credentials=true out the first time.

Many thanks for pointing out my mistake.

 
Old September 3rd, 2010, 04:30 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
am not quite sure how ive managed to have default credentials=true there
The wonders of IT.... ;-)

Glad to hear it's fixed now....

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!
 
Old September 5th, 2010, 12:35 AM
Registered User
 
Join Date: Feb 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default ContactForm.txt

I have a question in the same arena.

On the PlanetWrox ContactForm I have added a checkbox.
That displays fine but, I have not succeeded yet in having my "Response from Website" email tell me whether the box is checked or not.

I plan to create a form page with radio buttons, list boxes, drop down lists, checkboxes etc. Is it possible to have all those values sent in my "Response from Website" email?

Thanks,
tompatamcat

I have enclosed my code below:
Code:
ContactForm.txt

Hi there,

A user has left the following feedback at the site:

Name:             ##Name##
E-mail address:   ##Email##
Home phone:       ##HomePhone##
Business phone:   ##BusinessPhone##
Comments:         ##Comments##
Smallpox:          ##Smallpox##
"none are so blind as those who will not see"
Code:
WEbForm1.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

  <uc1:WebUserControl1 ID="WebUserControl1" runat="server" />

</asp:Content>
Code:
WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication2.WebUserControl1" %>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
    function ValidatePhoneNumbers(source, args) {
        var txtPhoneHome = document.getElementById('<%= txtPhoneHome.ClientID %>');
        var txtPhoneBusiness = document.getElementById('<%= txtPhoneBusiness.ClientID %>');
        if (txtPhoneHome.value != '' || txtPhoneBusiness.value != '') {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="style1" runat="server" id="FormTable">
  <tr>
    <td class="style4" colspan="3">
      <span class="style5">Get in touch with us<br />
      </span>
      <br />
      <span class="style6">Use the form below to get in touch with us. Enter your 
      name, email address and your home or phone number to get in touch with us.</span></td>
  </tr>
  <tr>
    <td class="style2">
      Your name</td>
    <td class="style3">
      <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="txtName" ErrorMessage="Please enter your name">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
    <td class="style2">
      Your email address</td>
    <td class="style3">
      <asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ErrorMessage="Please enter an e-mail address" 
        ControlToValidate="txtEmailAddress" Display="Dynamic">*</asp:RequiredFieldValidator>
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ErrorMessage="Please enter a valid e-mail address" 
        ControlToValidate="txtEmailAddress" Display="Dynamic" 
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
    </td>
    
  </tr>
  <tr>
    <td class="style2">
      Your email address again</td>
    <td class="style3">
      <asp:TextBox ID="txtEmailAddressConfirm" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
        ErrorMessage="Please confirm the e-mail address" 
        ControlToValidate="txtEmailAddressConfirm">*</asp:RequiredFieldValidator>
      <asp:CompareValidator ID="CompareValidator1" runat="server" 
        ErrorMessage="Please retype the e-mail address" 
        ControlToCompare="txtEmailAddress" ControlToValidate="txtEmailAddressConfirm" 
        Display="Dynamic">*</asp:CompareValidator>
    </td>
  </tr>
  <tr>
    <td class="style2">
      Home phone number</td>
    <td class="style3">
      <asp:TextBox ID="txtPhoneHome" runat="server"></asp:TextBox>
    </td>
    <td>
      <asp:CustomValidator ID="CustomValidator1" runat="server" 
        ClientValidationFunction="ValidatePhoneNumbers" Display="Dynamic" 
        ErrorMessage="Please enter your home or business phone number" 
        OnServervalidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
    </td>
  </tr>
  <tr>
    <td class="style2">
      Business phone number</td>
    <td class="style3">
      <asp:TextBox ID="txtPhoneBusiness" runat="server"></asp:TextBox>
    </td>
    <td>
      &nbsp;</td>
  </tr>
  <tr>
    <td class="style2">
      Comments</td>
    <td class="style3">
      <asp:TextBox ID="txtComments" runat="server" Height="105px" 
        TextMode="MultiLine" Width="354px"></asp:TextBox>
    </td>
    <td>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
        ErrorMessage="Please enter a comment" ControlToValidate="txtComments" 
        Display="Dynamic">*</asp:RequiredFieldValidator>
    </td>
  </tr>
  <tr>
  <td><fieldset><legend>Medical History</legend><asp:CheckBox ID="Smallpox" runat="server"></asp:CheckBox> Smallpox</fieldset></td><td>is</td><td>this</td></tr>
  <tr>
    <td class="style2">
      &nbsp;</td>
    <td class="style3">
      <asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
    </td>
    <td>
      &nbsp;</td>
  </tr>
  <tr>
    <td class="valsum" colspan="3">
      <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
        
        HeaderText="Please correct the following errors before you press the Send button" />
    </td>
  </tr>
</table>

<asp:Label ID="lblMessage" runat="server" Text="Message Sent" Visible="False"></asp:Label>

</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="PleaseWait">
<%--This is an ASP comment block--%>
Please Wait...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
Code:
WebUserControl1.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Web.Caching;

namespace WebApplication2
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (txtPhoneHome.Text != string.Empty || txtPhoneBusiness.Text != string.Empty)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
                string mailbody = string.Empty;
                if (Cache["ContactFormMailBody"] == null)
                {
                    mailbody = System.IO.File.ReadAllText(fileName);
                    Cache.Insert("ContactFormMailBody", mailbody, new CacheDependency(fileName));
                }
                else
                {
                    mailbody = Cache["ContactFormMailBody"].ToString() + "\r\n(File from the cache)";
                }

                mailbody = mailbody.Replace("##Name##", txtName.Text);
                mailbody = mailbody.Replace("##Email##", txtEmailAddress.Text);
                mailbody = mailbody.Replace("##HomePhone##", txtPhoneHome.Text);
                mailbody = mailbody.Replace("##BusinessPhone##", txtPhoneBusiness.Text);
                mailbody = mailbody.Replace("##Comments##", txtComments.Text);
                mailbody = mailbody.Replace("##Smallpox##", Smallpox.Text); 
                MailMessage myMessage = new MailMessage();
                myMessage.Subject = "Web Site Response from " + txtName.Text.ToString();
                myMessage.Body = mailbody;


                 myMessage.From = new MailAddress("tom@mytampawebdesign.com", "Tom Irimescu");
                 myMessage.To.Add(new MailAddress("tom@agentpurple.com", "AgentPurple"));
               

                SmtpClient mySmtpClient = new SmtpClient();
                try
                {
                    mySmtpClient.Send(myMessage);
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "An error occurred while sending your e-mail. Please try again.";
                }
                FormTable.Visible = false;
                System.Threading.Thread.Sleep(5000);
            }
        }
    }
}
 
Old September 6th, 2010, 04:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

To make threads and topics like these easier to find, it would have been better if you created a new thread instead. Your question deals with form controls, while the original topic dealt with SMTP connection problems.

Anyway, take a look at this:

mailbody = mailbody.Replace("##Smallpox##", Smallpox.Text);

You're using the Text property of the check box which you've never set, and which would be static regardless of the checked state, Instead, you need to use the Checked property:

mailbody = mailbody.Replace("##Smallpox##", Smallpox.Checked.ToString());

If you want Yes / No instead you can use this:

mailbody = mailbody.Replace("##Smallpox##", Smallpox.Checked ? "Yes" : "No");

Hope this helps,

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!
 
Old September 6th, 2010, 02:20 PM
Registered User
 
Join Date: Feb 2009
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you Imar! Very helpful.
 
Old September 17th, 2010, 07:49 PM
Authorized User
 
Join Date: Oct 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Imar things that were working yesterday such as contact.aspx and signup.aspx are now giving me the following error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Service not available, closing transmission channel. The server response was: Cannot establish SSL with SMTP server 65.55.162.200:25, SSL_connect error 5
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot establish SSL with SMTP server 65.55.162.200:25, SSL_connect error 5

Source Error:

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

<%@ Page Language="C#" Debug="true" %>

or:

2) Add the following section to the configuration file of your application:

<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.

Stack Trace:


[SmtpException: Service not available, closing transmission channel. The server response was: Cannot establish SSL with SMTP server 65.55.162.200:25, SSL_connect error 5]
System.Net.Mail.SmtpConnection.GetConnection(Strin g host, Int32 port) +1067679
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +159
System.Net.Mail.SmtpClient.GetConnection() +35
System.Net.Mail.SmtpClient.Send(MailMessage message) +1480
Controls_MakeAnOffer.buttonSend2_Click(Object sender, EventArgs e) +466
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565





Do you have any idea whats caused this or if you think it may be my wireless internet or a problem with that? Ive not amended any code that i can think of that would cause this and both pages were working previously and i have moved on to other sections since i got them working?

Lee

ps i can put this in a seperate thread if you like but i thought it was very relevant to email problems.

Last edited by leemark2k3; September 17th, 2010 at 11:33 PM..
 
Old September 18th, 2010, 02:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Service not available, closing transmission channel. The server response was: Cannot establish SSL with SMTP server 65.55.162.200:25, SSL_connect error 5
The "Service not available" may imply there's a problem at the connecting end, or on the netwok.

I've asked this many times before, but why dont' you simply use a local drop folder and be done with it? That way, your messages are saved as files in a folder on disk.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9 Trying to send email out digink BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 March 5th, 2011 05:42 AM
Chapter 9 Can't send email ChuckASP BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 22nd, 2010 12:16 PM
Chapter 9 - email security ChuckASP BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 March 20th, 2010 03:26 PM
Chapter 3 - Email List daibhidh BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 6 December 4th, 2009 06:39 AM
Chapter 11 - Email HELP! Nyika1203 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 28th, 2008 06:45 PM





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