Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 April 1st, 2008, 09:54 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Contact Form and Gmail

Arrggggggggggg!

I'm using The Beer House starter kit and trying to configure it to use Gmail to send email. The contact form will send an email that uses the name specified in the txtName textbox, but the email address that is in the txtEmail textbox is ignored. Instead of using the txtEmail address for the "from" address, it uses "contact@gmail.com" - which is the email to authenticate w/ gmail.

So, the "From address and name" should come from the "msg.From = new MailAddress(txtEmail.Text, txtName.Text);" but only the name does...

What am I missing?

 Below is my code for the pages:

Contact.aspx:
<%@ Page Language="C#" MasterPageFile="~/Template.master" AutoEventWireup="true" CodeFile="Contact.aspx.cs" Inherits="MB.TheBeerHouse.UI.Contact" Title="The Beer House - Contact Us" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" Runat="Server">
   <div class="sectiontitle">Contact Us</div>
   <p></p>


   <table cellpadding="2">
      <tr>
         <td style="width: 80px;" class="fieldname"><asp:Label runat="server" ID="lblName" AssociatedControlID="txtName" Text="Your name:" /></td>
         <td style="width: 400px;"><asp:TextBox runat="server" ID="txtName" Width="100%" /></td>
         <td>
               <asp:RequiredFieldValidator runat="server" Display="dynamic" ID="valRequireName" SetFocusOnError="true"
                  ControlToValidate="txtName" ErrorMessage="Your name is required">*</asp:RequiredFieldValidator>
         </td>
      </tr>
      <tr>
         <td class="fieldname"><asp:Label runat="server" ID="lblEmail" AssociatedControlID="txtEmail" Text="Your e-mail:" /></td>
         <td><asp:TextBox runat="server" ID="txtEmail" Width="100%" /></td>
         <td>
               <asp:RequiredFieldValidator runat="server" Display="dynamic" ID="valRequireEmail" SetFocusOnError="true"
                  ControlToValidate="txtEmail" ErrorMessage="Your e-mail address is required">*</asp:RequiredFieldValidator>
               <asp:RegularExpressionValidator runat="server" Display="dynamic" ID="valEmailPattern" SetFocusOnError="true"
                  ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="The e-mail address you specified is not well-formed">*</asp:RegularExpressionValidator>
         </td>
      </tr>
      <tr>
         <td class="fieldname"><asp:Label runat="server" ID="lblSubject" AssociatedControlID="txtSubject" Text="Subject:" /></td>
         <td><asp:TextBox runat="server" ID="txtSubject" Width="100%" /></td>
         <td>
               <asp:RequiredFieldValidator runat="server" Display="dynamic" ID="valRequireSubject" SetFocusOnError="true"
                  ControlToValidate="txtSubject" ErrorMessage="The subject is required">*</asp:RequiredFieldValidator>
         </td>
      </tr>
      <tr>
         <td class="fieldname"><asp:Label runat="server" ID="lblBody" AssociatedControlID="txtBody" Text="Body:" /></td>
         <td><asp:TextBox runat="server" ID="txtBody" Width="100%" TextMode="MultiLine" Rows="8" /></td>
         <td>
               <asp:RequiredFieldValidator runat="server" Display="dynamic" ID="valRequireBody" SetFocusOnError="true"
                  ControlToValidate="txtBody" ErrorMessage="The body is required">*</asp:RequiredFieldValidator>
         </td>
      </tr>
      <tr>
         <td colspan="3" style="text-align: right;">
               <asp:Label runat="server" ID="lblFeedbackOK" Text="Your message has been successfully sent." SkinID="FeedbackOK" Visible="false" />
               <asp:Label runat="server" ID="lblFeedbackKO" Text="Sorry, there was a problem sending your message." SkinID="FeedbackKO" Visible="false" />
               <asp:Button runat="server" ID="btnSubmit" Text="Send" OnClick="btnSubmit_Click" />
               <asp:ValidationSummary runat="server" ID="valSummary" ShowSummary="false" ShowMessageBox="true" />
         </td>
      </tr>
   </table>
</asp:Content>


Code-Behind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 MB.TheBeerHouse;
namespace MB.TheBeerHouse.UI
{
   public partial class Contact : BasePage
   {
      protected void Page_Load(object sender, EventArgs e)
      {

      }

      protected void btnSubmit_Click(object sender, EventArgs e)
      {
         try
         {
            // send the mail
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = false;
            msg.From = new MailAddress(txtEmail.Text, txtName.Text);
            msg.To.Add(new MailAddress(Globals.Settings.ContactForm.MailTo));
            if (!string.IsNullOrEmpty(Globals.Settings.ContactFor m.MailCC))
               msg.CC.Add(new MailAddress(Globals.Settings.ContactForm.MailCC));
            msg.Subject = string.Format(Globals.Settings.ContactForm.MailSub ject, txtSubject.Text);
            msg.Body = txtBody.Text;

            //start add to enagle gmail
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(msg);
            //end enable gmail

            // show a confirmation message, and reset the fields
            lblFeedbackOK.Visible = true;
            lblFeedbackKO.Visible = false;
            txtName.Text = "";
            txtEmail.Text = "";
            txtSubject.Text = "";
            txtBody.Text = "";
         }
         catch (Exception)
         {
            lblFeedbackOK.Visible = false;
            lblFeedbackKO.Visible = true;
         }
      }
   }
}


Web.config:
        <mailSettings>
            <smtp deliveryMethod="Network">
                <network host="smtp.gmail.com" port="587" userName="contact@gmail.com" password="mypwd" defaultCredentials="false"/>
            </smtp>
        </mailSettings>
 
Old April 1st, 2008, 01:31 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Sorry, I have no idea what this would be the case. Maybe GMail changes the address and simply ignores the field you set?

Have you tried it with a different mail provider?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 1st, 2008, 01:40 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works correctly for the email sent from Register and PasswordRecovery pages, but for some reason the Contact page doesn't pick up the correct "From" address.
 
Old April 1st, 2008, 02:12 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

??? I assume the PasswordRecovery doesn't use the user's e-mail address as the From address, does it?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 16th, 2008, 07:47 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the mail is actually coming from ur website mail server in other words you are sending an email from contact@gmail.com to contact@gmail.com thats why its using the host email address(contact@gmail.com) when u reply to the message the reply address should be the address that the user type in the mail from box. if it doesnt add msg.replyTo but u should have to

 
Old April 16th, 2008, 07:58 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Just so everyone is aware -- this is a well-known issue when using Google as a portable SMTP server. Nothing wrong with your code -- it's Google that's doing it.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Error With Contact Form ASP.NET 3.5 CSharpCoder BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 July 28th, 2008 04:08 PM
Contact form XML attachments. retroviz BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 March 16th, 2008 09:52 AM
Gmail vicoldan Need help with your homework? 2 January 29th, 2008 03:24 PM
Contact Form myself Classic ASP Basics 1 November 18th, 2006 11:16 AM
PHP contact form probe4 PHP How-To 1 September 14th, 2005 12:52 PM





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