Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form in asp.net in which on click of a button data is sent to a email id. But the email is not sent. Please have a look at the code and suggest..
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

public partial class _Default : 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("receiver@gmail.com");
            //Configure the address we are sending the mail from
            MailAddress address = new MailAddress("sender@gmail.com");
            msg.From = address;
            //Append their name in the beginning of the subject
            msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
            msg.Body = txtMessage.Text;

            //Configure an SmtpClient to send the mail.
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true; //only enable this if your provider requires it
            //Setup credentials to login to our sender email address ("UserName", "Password")
            NetworkCredential credentials = new NetworkCredential("sender@gmail.com", "password");
            client.Credentials = credentials;

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

            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";

            //Clear the form
            txtName.Text = "";
            txtMessage.Text = "";
        }
        catch
        {
            //If the message failed at some point, let the user know
            lblResult.Text = "Your message failed to send, please try again.";
        }

    }
}
Posted
Updated 29-Mar-15 7:04am
v2
Comments
[no name] 29-Mar-15 13:08pm    
k,what error u have occurred ? comment ur catch inside value then use this code.then only we know that the correct exception.

catch(Exception ex)
{
throw ex;
//If the message failed at some point, let the user know
// lblResult.Text = "Your message failed to send, please try again.";
}
Arkadeep De 29-Mar-15 13:19pm    
What exactly error you are getting?

C#
using (MailMessage mm = new MailMessage(fromEmail, toEmail))
{
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(fromEmail, password);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
}


Try this
 
Share this answer
 
v2
Configure sender mail in outlook then send mail when u can send from outlook note settings port no etc then use them in your code.

Give it a try
 
Share this answer
 
Because you are using POP3, so you also need to mention that in your code as well.
 
Share this answer
 

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