Search code examples
c#asp.netgmail

Email not being sent using gmail info


ok, so i have been trying to sort this for about 3 hours but to no avail. I have made a simple contact us form. All it needs to do is send the message to my gmail account. It's not sending anything and neither is it giving any error. I tried turning off the 2 step authentication but that hasn't helped either. My aspx code:

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
                        <p>
                            Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
                        </p>
                        <p>
                            Your name:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
        ControlToValidate="YourName" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
                            Your email address:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
        ControlToValidate="YourEmail" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
                            <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
                                SetFocusOnError="true" Text="Example: [email protected]" ControlToValidate="YourEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
                                ValidationGroup="save" ForeColor="Red" /><br />
                            Subject:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
        ControlToValidate="YourSubject" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
                            Your Question:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
        ControlToValidate="Comments" ValidationGroup="save" /><br />
                            <asp:TextBox ID="Comments" runat="server"
                                TextMode="MultiLine" Rows="10" Width="400px" />
                        </p>
                        <p>
                            <asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
                        </p>
                    </asp:Panel>

my C# code:

  try
{
    MailMessage Msg = new MailMessage();
    // Sender e-mail address.
    Msg.From = new MailAddress(YourEmail.Text);
    // Recipient e-mail address.
    Msg.To.Add("*****@gmail.com");
    Msg.Subject = YourSubject.Text;
    Msg.Body = Comments.Text;
    // your remote SMTP server IP.
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential("****@gmail.com", "*********");
    smtp.EnableSsl = true;
    smtp.Send(Msg);
    //Msg = null;
    DisplayMessage.Text = "Thanks for Contacting us";
    // Clear the textbox valuess
    YourName.Text = "";
    YourSubject.Text = "";
    Comments.Text = "";
    YourEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}

any help will be greatly appreciated


Solution

  • You need to put proper credential from which you will be sending the mail.

    See here

    ASPX CODE

    <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
            <p>
                Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
            </p>
            <p>
                Your name:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
        ControlToValidate="YourName" ValidationGroup="save" /><br />
                <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
                Your email address:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
        ControlToValidate="YourEmail" ValidationGroup="save" /><br />
                <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
                <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
                    SetFocusOnError="true" Text="Example: [email protected]" ControlToValidate="YourEmail"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
                    ValidationGroup="save" ForeColor="Red" /><br />
                Subject:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
        ControlToValidate="YourSubject" ValidationGroup="save" /><br />
                <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
                Your Question:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
        ControlToValidate="Comments" ValidationGroup="save" /><br />
                <asp:TextBox ID="Comments" runat="server"
                    TextMode="MultiLine" Rows="10" Width="400px" />
            </p>
            <p>
                <asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
            </p>
        </asp:Panel>
    

    CS CODE

    protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(YourEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add("[email protected]");
                Msg.Subject = YourSubject.Text;
                Msg.Body = Comments.Text;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("YOURGMAILID", "YOURGMAIL PASSWORD");  // IT SHOULD BE CORRECT TO WORK
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                //Msg = null;
               // DisplayMessage.Text = "Thanks for Contacting us";
                // Clear the textbox valuess
                YourName.Text = "";
                YourSubject.Text = "";
                Comments.Text = "";
                YourEmail.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }
        }
    

    hope it helps. :)