Ronan
Ronan

Reputation: 327

Sending an email once a form is submitted using smtp and gmail in asp.net

I'm trying to receive an email once my form is submitted on my webpage. At the moment it submits fine without any errors but I don't receive the email. Does anyone know what code I have to add in the code behind page to make this work?

Here is the html;

<h2>Contact Us</h2>
        <br />
        <table>            
            <tr>
                <td  style="align-items:center">
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td  style="align-items:center">
                    email:</td>
                <td>
                    <asp:TextBox ID="txtEmail"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>



            <!-- Message -->
            <tr>
                <td style="align-items:center">
                    What are you looking for?
                </td>
                <td>
                    <asp:TextBox ID="txtMessage"
                                    runat="server"
                                    Columns="40"
                                    Rows="6"
                                    TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td  style="align-items:center">
                    What would you be willing to pay for this app?</td>
                <td>
                    <asp:TextBox ID="txtPay"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>

            <!-- Submit -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                        onclick="btnSubmit_Click" /><br />
                </td>
            </tr>

            <!-- Results -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Label ID="lblResult" runat="server"></asp:Label>
                </td>
            </tr>
        </table>

this is the code behind;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Telluswhatyouwant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("[email protected]");

//Send the msg
            client.Send(msg);

Upvotes: 1

Views: 12843

Answers (3)

polin
polin

Reputation: 2835

This is a perfectly working code for localhost where mailing option is enabled. You can change the port number.fromWho, toWho are mailing addresses in string format (ie: [email protected])

    string sMailServer = "127.0.0.1";

    MailMessage MyMail = new MailMessage();
    MyMail.From = fromWho;
    MyMail.To = toWho;
    if (toCC != "" || toCC != null)
    {
        MyMail.Cc = toCC;
    }
    if (toBCC != "" || toBCC != null)
    {
        MyMail.Bcc = toBCC;
    }

    MyMail.Subject = Subject;
    MyMail.Body = Body;
    //MyMail.BodyEncoding = Encoding.UTF8;
    MyMail.BodyFormat = MailFormat.Html;

    SmtpMail.SmtpServer = sMailServer;
    try
    {

        SmtpMail.Send(MyMail);


    }
    catch (Exception ex)
    {
        return ex;
    }

Upvotes: 2

Thamotharan Karuppiah
Thamotharan Karuppiah

Reputation: 1795

Where you have configured your client Object. Give complete detail.

Then go through the following post answered by me. You should configure your SmtpClient properly.

How to send email in ASP.NET C#

Upvotes: 1

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

You can try this and make sure you are using valid login credential and you have proper internet connection:

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = 
                  new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
     smtp.Send(mail);                
  }
  catch (Exception ex)
  {   
    // Handle exception here 

  }

Upvotes: 1

Related Questions