0

I created a registration form in the asp.net and everything is working fine, then, I added the following code to be able to send an email to the user and the host, it is still working but I didn't received any email, do you know what is the problem with my code?

  protected void Button1_Click(object sender, EventArgs e)
        {

            try
            {

              

                SmtpClient client = new SmtpClient("smtp.gmail.com",587);
                client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential("##########", "###########");
                MailMessage mailmsg = new MailMessage();
                mailmsg.To.Add(TxtEmail.Text);
                mailmsg.From = new MailAddress("################");
                mailmsg.Subject = "The email is confirmed";
                mailmsg.Body = "Dear "+ TxtName.Text + ",\n\nyou have been registred successfully";
                client.Send(mailmsg);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('message has been sent successfully');", true);

                MailMessage mailmsg2 = new MailMessage();
                mailmsg2.To.Add("################");
                mailmsg.From = new MailAddress("##############");
                mailmsg.Subject = "New user has registred";
                mailmsg.Body = "Dear Admin, \n\nthere are a new user registred in the website";
                client.Send(mailmsg);


            }

            catch (Exception ex)
            {
                Response.Write("Couldn't send email: " + ex.Message);
            }


        }

I am getting the output:

Couldn't send email: Failure sending mail

Aristos
  • 66,005
  • 16
  • 114
  • 150
Mariam
  • 9
  • 6
  • what error message did you get ? If no error message, then the mail server is blocked as spam – Aristos Jul 06 '20 at 09:44
  • it shows me this message "Couldn't send emailFailure sending mail" in the webpage. So I think there is something wrong – Mariam Jul 06 '20 at 09:49
  • 1
    The error message you get is only "Failure"?? That seems weird, error messages are susally far more descriptive – Cleptus Jul 06 '20 at 09:51
  • yes, and I enabled less secure apps, but still I have the same problem – Mariam Jul 06 '20 at 09:56
  • take a look at : https://stackoverflow.com/questions/2209617/smtpclient-failure-sending-mail – Aristos Jul 06 '20 at 10:59
  • and https://stackoverflow.com/questions/32231489/smtp-exception-failure-sending-mail/32231849 – Aristos Jul 06 '20 at 10:59
  • Can you post the whole exception details not just .Message? I'd guess there is more information in the InnerException. – Rup Jul 06 '20 at 11:06
  • in the asp.net I got this error "Exception thrown: 'System.Net.Mail.SmtpException' in System.dll The program '[10996] iisexpress.exe' has exited with code -1 (0xffffffff)." – Mariam Jul 06 '20 at 11:20
  • No, that doesn't help. Change `Response.Write("Couldn't send email: " + ex.Message);` to be `Response.Write("Couldn't send email: " + ex);` and you should get more information. – Rup Jul 06 '20 at 11:21
  • Or try checking the Event Log - IIS might have written the complete exception there. – Rup Jul 06 '20 at 11:23
  • This is what I got "System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at – Mariam Jul 06 '20 at 12:19
  • System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- – Mariam Jul 06 '20 at 12:20
  • at System.Net.Mail.SmtpClient.Send(MailMessage message) at RegistrationForm5.Registration.Button1_Click(Object sender, EventArgs e) in C:\Users\maria\source\repos\RegistrationForm5\RegistrationForm5\Registration.aspx.cs:line 69" – Mariam Jul 06 '20 at 12:20
  • sorry it is too long I couldn't add it in one comment section – Mariam Jul 06 '20 at 12:20
  • You could edit the exception into the question if it's too long to post as a comment, and I think it would be more useful in the exception anyway. But again I think you're missing the important bit: you've given us some stack trace from the inner exception but that's not as important as the actual inner exception itself. Given the stack trace has GetConnection though I'd guess it's a failure to connect to the SMTP server, or a failure to negotiate a TLS connection. – Rup Jul 06 '20 at 12:22

0 Answers0