สอบถาม เรื่องการส่งEmail เวลาลืมรหัสผ่าน c# โปรแกรมแจ้งว่า An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code Additional information: Cannot get IIS pickup directory.
อยากให้ช่วยดูให้หน่อยค่ะ แบบแรก ติดเออเร่อ ว่า
Code (C#)
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;
protected void btnPass_Click(object sender, EventArgs e)
{
string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strSelect = "SELECT Emp_ID,Password FROM Employee WHERE Email = @Email";
SqlConnection connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = strSelect;
SqlParameter email = new SqlParameter("@Email", SqlDbType.NVarChar, 50);
email.Value = txtEmail.Text.Trim().ToString();
command.Parameters.Add(email);
DataSet dsPwd = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
connection.Open();
dAdapter.Fill(dsPwd);
connection.Close();
if (dsPwd.Tables[0].Rows.Count > 0)
{
MailMessage loginInfo = new MailMessage();
loginInfo.To.Add(txtEmail.Text.ToString());
loginInfo.From = new MailAddress("[email protected] ");
loginInfo.Subject = "Forgot Password Information";
loginInfo.Body = "Emp_ID: " + dsPwd.Tables[0].Rows[0]["Emp_ID"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br><br>";
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("[email protected] ", "0973453452");
smtp.Send(loginInfo);
lblMessage.Text = "Password is sent to you email id";
}
else
{
lblMessage.Text = "Email Address Not Registered";
}
}
===========================================================================
แบบที่ 2 คือไม่ขึ้นเออเร่อ แต่เมลไม่เข้า
Code (C#)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
protected void Button1_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection("Data Source = WINCTRL-Q43GRTD ;Initial Catalog=Honey;Persist Security Info=True;User ID=sa;Password=0384758347"))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Emp_ID,Password,Email FROM Employee Where Email= '" + TextBox1.Text.Trim() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
if (ds.Tables[0].Rows.Count > 0)
{
//Label1.Text = "Your Password Details Sent to your mail";
//TextBox1.Text = "";
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(TextBox1.Text);
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("[email protected] ", "0973453452");
smtp.Host = "smtp.gmail.com";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
Msg.To.Add(TextBox1.Text);
Msg.IsBodyHtml = true;
Msg.Subject = "Your Password Details";
Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["Emp_ID"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["Password"] + "<br/><br/>";
smtp.Send(Msg);
Label1.Text = "Your Password Details Sent to your mail";
TextBox1.Text = "";
}
else
{
Label1.Text = "The Email you entered not exists.";
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#
Date :
2016-07-16 17:59:19
By :
ัyoyo
View :
1186
Reply :
1
มันมี Error แน่นอนครับ อันที่ 2 คุณใช้ try แล้วดักจับ Error Message เลยครับ
Date :
2016-07-18 09:38:13
By :
mr.win
Load balance : Server 05