Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a registration page and I want that any user registers than a mail should come on my email address. I am on a WiFi network. So please please help me out.

my .cs code is as follows:


C#
protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         try
        {
            string str ="Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Earth;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string insertQuery = "insert into Corporate (Name,Emailaddress,Contact,Highestqualification,Specialization,Institutename,Description) values (@Name,@Emailaddress,@Contact,@Highestqualification,@Specialization,@Institutename,@Description)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue("@Name", txtName.Text);
            com.Parameters.AddWithValue("@Emailaddress", txtEmailaddress.Text);
            com.Parameters.AddWithValue("@Contact", txtContactno.Text);
            com.Parameters.AddWithValue("@Highestqualification", txtHighestqualification.Text);
            com.Parameters.AddWithValue("@Specialization", ddlspecialization.SelectedItem.ToString());
            com.Parameters.AddWithValue("@Institutename", txtInstitutename.Text);
            com.Parameters.AddWithValue("@Description", txtDescription.Text);
            

            com.ExecuteNonQuery();
           
            

            con.Close();
            
        }

        catch (Exception ex)
        {
            Response.Write("Error Occured! Try Again" + ex.ToString());
        }

        
        Response.Write("Thanks for Applying, We will response on your application shortly");
    }
}
Posted
Updated 3-Mar-14 18:53pm
v2
Comments
Ankur\m/ 4-Mar-14 0:51am    
Why don't you do some basic reading before coding? You previous questions were very basic like incorrect query and incorrect parameter name and you were not able to figure out the issue. And now you ask the next step. This is not how programming is done. Please spend some time learning.
For sending a mail you need SMPT server configured. Google and read about it.

C#
protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "ml27.santos@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString();
    //Password of your gmail address
    const string fromPassword = "Password";
    // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done
        SendMail();
        DisplayMessage.Text = "Message sent!";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}}
 
Share this answer
 
Comments
manishmns12 4-Mar-14 1:02am    
thanks alot for this...
but as my registrtn page is..if any users registers to me, the mail with his details should come to my address and one thanks mail to him...

sorry but i know i m asking a lot n might b irritating u...but please help me out...i really need it importantly...
Amalraj Ramesh 4-Mar-14 1:18am    
hey im happy to helpe you

string subject = "Hai UserName <br/> Thanks .... constaract your mail body";

You can use HTML tag to constact the email subject.

Happy coding..!
C#
string res ="";

       
        try
        {
           
                string mailid = string.Empty;
                MailMessage msg = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                mailid = emailid.ToString();
                msg.From = new MailAddress("yr_adress");
                msg.To.Add(mailid);
               

                msg.Subject = msgSubject.ToString();
                msg.Body = msgbody.ToString();
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.Normal;
                smtp.Credentials = new NetworkCredential("youremailid ", "Password"); // send  form yr email address..
                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                try
                {
                    smtp.Send(msg);
                    res = "Sucsessfully send";
                }
                catch (SmtpFailedRecipientsException ex)
                {
                    for (int i = 0; i < ex.InnerExceptions.Length; i++)
                    {
                        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                        if (status == SmtpStatusCode.MailboxBusy ||status == SmtpStatusCode.MailboxUnavailable)
                        {
                            Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                            System.Threading.Thread.Sleep(5000);
                            smtp.Send(msg);
                            res = "Sucsessfully send";
                        }
                        else
                        {
                            Console.WriteLine("Failed to deliver message to {0}",
                                ex.InnerExceptions[i].FailedRecipient);
                            res = "send Faild";
                        }
                    }
                }
            }
        }
        
            
            catch (Exception ex)
            {
             Console.WriteLine("Exception caught in RetryIfBusy(): {0}", ex.Message.ToString());
             res = "send Faild";
            }

        return res;
    }
 
Share this answer
 
v3
Comments
manishmns12 4-Mar-14 1:05am    
thanks alot for this...
but as my registrtn page is..if any users registers to me, the mail with his details should come to my address and one thanks mail to him...
"will it work for what i m asking

sorry but i know i m asking a lot n might b irritating u...but please help me out...i really need it importantly...
mukesh mourya 4-Mar-14 1:11am    
ok .. you can collect all information about user registered from database(which are add recently register ) and send on your email id ,as msgbody.
you can do as
manishmns12 4-Mar-14 1:32am    
so i may just copy n paste, n do some modifications accordng to my mail n etc..n it may work....
i should paste it in button click na???
or any othr place???

as i m a novice so..asking this much
mukesh mourya 4-Mar-14 1:36am    
you can pest this code in class(.cs)page and coll its on button click
manishmns12 4-Mar-14 1:38am    
means....i may just paste the code after mine button click code na????
please sir...just lil more help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900