Home > front end >  Sending of Email not working in C# Windows Form Application
Sending of Email not working in C# Windows Form Application

Time:10-26

This is my code:

try
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[email protected]");
    msg.To.Add(txtTo.Text);
    msg.Subject = txtSubject.Text;
    msg.Body = txtBody.Text;

    SmtpClient smt = new SmtpClient();
    smt.Host = "smtp.gmail.com";

    System.Net.NetworkCredential ntcd = new NetworkCredential();
    ntcd.UserName = "[email protected]";
    ntcd.Password = "Password";

    smt.Credentials = ntcd;
    smt.EnableSsl = true;
    smt.Port = 465;
    smt.Send(msg);

    MessageBox.Show("Your mail has been sent");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Everything was working fine. However, when I click the submit button, it prompt me that the email was unable to sent. What is the problem here? I am using C# , Windows Forms application.

CodePudding user response:

try this one, Hope it's work fine

try
        {

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("[email protected]");
            msg.To.Add(textBox1.Text);
            msg.Subject = textBox3.Text;
            msg.Body = textBox2.Text;

            SmtpClient smt = new SmtpClient();
            smt.Host = "smtp.gmail.com";

            smt.Credentials = new NetworkCredential("[email protected]", "Password");
            smt.EnableSsl = true;
            smt.Port = 465;
            smt.Send(msg);

            MessageBox.Show(@"Email sent successfully to: "   textBox1.Text);

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }

CodePudding user response:

You might need to enable 3rd party applications for your email address

  • Related