Subject: Contact Form and Gmail
Posted By: Jamessaep Post Date: 4/1/2008 9:54:09 AM
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.ContactForm.MailCC))
               msg.CC.Add(new MailAddress(Globals.Settings.ContactForm.MailCC));
            msg.Subject = string.Format(Globals.Settings.ContactForm.MailSubject, 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>
Reply By: Imar Reply Date: 4/1/2008 1:31:38 PM
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.
Reply By: Jamessaep Reply Date: 4/1/2008 1:40:18 PM
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.
Reply By: Imar Reply Date: 4/1/2008 2:12:52 PM
??? 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.
Reply By: canyiah Reply Date: 4/16/2008 7:47:42 AM
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

Reply By: Lee Dumond Reply Date: 4/16/2008 7:58:19 AM
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.



Go to topic 70638

Return to index page 1