0

i want sent an email(gmail) using my application, but i have an error, my code is syntactically correct. Here is my code

public void send(string body , string objet)
   try
        {
            NetworkCredential log = new NetworkCredential();

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

            MailMessage msg = new MailMessage();
           msg.To.Add("you@gmail.com"); 
            msg.Body =  body;
           msg.From = new MailAddress("me@gmail.com");
            msg.Subject = objet;         
            smtp.EnableSsl = true;

            smtp.Port = 465;
            smtp.Credential= new NetworkCredential("me@gmail", "passwdgmail");

            smtp.Send(msg); 
            MessageBox.Show("succés");
        }
        catch  (Exception ex)
            {MessageBox.Show(ex.StackTrace);}
        }

  }

when i did the ex.message in the try..catch: the error message is: sending failure some mail when I did ex.StackTrace, the error is in smtp.Send(msg);

Zied.M
  • 215
  • 4
  • 17
  • 2
    Your code compiles, but is not logicall. You declare `NetworkCredential`, assign its properties, and never use those credentials. Check this answer. https://stackoverflow.com/questions/2766928/how-to-set-username-and-password-for-smtpclient-object-in-net – Cleptus Jul 26 '18 at 11:03
  • yes , i forgot it , i add smtp.Credentials = new NetworkCredential("****","****"); but the same error : failure to sending mail – Zied.M Jul 26 '18 at 11:12
  • Possible duplicate of [Sending email through Gmail SMTP server with C#](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – Cleptus Jul 26 '18 at 11:14

0 Answers0