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>
|