This is my code and it is giving me an SMTP Exception. I tried to change my port to 25 but didn't work. Plus i am connected to the internet i can't figure out whats the problem

private void RecovaryEmail(string username)
        {
            ManagerUserAccount mgrUserAccount = new ManagerUserAccount();
            UserAccount ua = mgrUserAccount.SearchUserAccount(username);

            ManagerStaff mgrStaff = new ManagerStaff();
            Staff s = mgrStaff.SearchStaff(ua.StaffID);


            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("restaurant.reservation.and.order.system@gmail.com");
                mail.To.Add(s.Email);//members email
                mail.Subject = "Password Recovary";
                mail.Body = "Dear " + s.FirstName + " " + s.LastName + ",\nIn this email you have your account details.\n\nUsername: " + ua.Username + "\nPassword: " + ua.Password + "\nRecovery Question: " + ua.SecurityQuestion + "\nRecovery Answer: " + ua.SecurityAnswer + ".";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("restaurant.reservation.and.order.system@gmail.com", "password");
                SmtpServer.EnableSsl = true;
                SmtpServer.Host = "smtp.gmail.com";

                SmtpServer.Send(mail);
                MessageBox.Show("Email has been sent to " + s.Email);
            }
            catch (SmtpException se)
            {
                MessageBox.Show("Please, connect to the internet to be able too send email.");//se.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Try this:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(strEmailStranke);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(strObvEmail);
message.Body = "body text"
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("userName, "password"); //of gmail access
smtp.Send(message);

it works for me.

Try this:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(strEmailStranke);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(strObvEmail);
message.Body = "body text"
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("userName, "password"); //of gmail access
smtp.Send(message);

it works for me.

I found the mistake.. I have toghout i registered the email with gmail but i didn't.. What a FAIL!! :D thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.