hi developers!!
Recently I am working on sending mail by c# coding and I tryed to send mail by c# coding on visual studio platform and it's work totally fine in my localhost of visual studio. but when I run that same code over other hosting website like myASP.NET or Somee.com that time that code getting error.
error like (if I used smtp.gmail.com then error like
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at
) and (if I used smtp.somee.com then error generate like
Failure sending mail.
)
Sending mail Code in c# is,
string username = "";
string password = "";
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["cstring"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("select umail, lpwd from uregister where umail=@email", con);
cmd.Parameters.AddWithValue("email", txtEmail.Text);
con.Open();
using(OleDbDataReader sdr = cmd.ExecuteReader()) {
if (sdr.Read()) {
username = sdr["umail"].ToString();
password = sdr["lpwd"].ToString();
}
}
con.Close();
if (!string.IsNullOrEmpty(password)) {
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtEmail.Text);
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Your Password Details";
Msg.Body = ("Your Username is:" + username + "<br/><br/>" + "Your Password is:" + password);
Msg.IsBodyHtml = true;
SmtpClient Smtp = new SmtpClient();
Smtp.Host = "";
Smtp.Port = 587;
NetworkCredential ntwd = new NetworkCredential();
ntwd.UserName = "xyz@gmail.com";
ntwd.Password = "xyz123.";
Smtp.UseDefaultCredentials = false;
Smtp.Credentials = ntwd;
Smtp.EnableSsl = true;
Smtp.Send(Msg);
lbltxt.Text = "Your Password Details Sent to your mail.";
txtEmail.Text = "";
}
else
{
lbltxt.Text = "The Email you entered not exists.";
}
this code is not work on hosting site somee.com/myASP.NET. so please let me know what is problem in this code..
What I have tried:
string username = "";
string password = "";
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["cstring"].ConnectionString);
OleDbCommand cmd = new OleDbCommand("select umail, lpwd from uregister where umail=@email", con);
cmd.Parameters.AddWithValue("email", txtEmail.Text);
con.Open();
using(OleDbDataReader sdr = cmd.ExecuteReader()) {
if (sdr.Read()) {
username = sdr["umail"].ToString();
password = sdr["lpwd"].ToString();
}
}
con.Close();
if (!string.IsNullOrEmpty(password)) {
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtEmail.Text);
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Your Password Details";
Msg.Body = ("Your Username is:" + username + "<br/><br/>" + "Your Password is:" + password);
Msg.IsBodyHtml = true;
SmtpClient Smtp = new SmtpClient();
Smtp.Host = "smtp.gmail.com";
Smtp.Port = 587;
NetworkCredential ntwd = new NetworkCredential();
ntwd.UserName = "xyz@gmail.com";
ntwd.Password = "xyz123.";
Smtp.UseDefaultCredentials = false;
Smtp.Credentials = ntwd;
Smtp.EnableSsl = true;
Smtp.Send(Msg);
lbltxt.Text = "Your Password Details Sent to your mail.";
txtEmail.Text = "";
}
else
{
lbltxt.Text = "The Email you entered not exists.";
}