Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用asp.net发送邮件失败_C#_Asp.net - Fatal编程技术网

C# 使用asp.net发送邮件失败

C# 使用asp.net发送邮件失败,c#,asp.net,C#,Asp.net,下面是我发送确认链接到邮件的代码,我收到了一些错误,比如发送邮件失败 发送确认链接到电子邮件后,用户将重置密码 string uniqueCode = string.Empty; SqlCommand cmd = new SqlCommand(); SqlDataReader dr; try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionSt

下面是我发送确认链接到邮件的代码,我收到了一些错误,比如发送邮件失败

发送确认链接到电子邮件后,用户将重置密码

 string uniqueCode = string.Empty;
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;
    try
    {
        SqlConnection con = new      SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        // get the records matching the supplied username or email id.         
        cmd = new SqlCommand("select * from Tbl_Login where UserName COLLATE Latin1_general_CS_AS=@username or EmailId COLLATE Latin1_general_CS_AS=@emailId", con);

        cmd.Parameters.AddWithValue("@username", Convert.ToString(txtUserName.Text.Trim()));
        cmd.Parameters.AddWithValue("@emailId", Convert.ToString(txtEmailId.Text.Trim()));
        dr = cmd.ExecuteReader();
        cmd.Dispose();
        if (dr.HasRows)
        {
            dr.Read();
            //generate unique code
            uniqueCode = Convert.ToString(System.Guid.NewGuid());
            //Updating an unique random code in then UniquCode field of the database table
            cmd = new SqlCommand("update Tbl_Login set Uniquecode=@uniqueCode where UserName=@username or EmailId=@emailid", con);
            cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode);
            cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
            cmd.Parameters.AddWithValue("@emailid", txtEmailId.Text.Trim());

            StringBuilder strBody = new StringBuilder();
            //Passing emailid,username and generated unique code via querystring. For testing pass your localhost number and while making online pass your domain name instead of localhost path.
            strBody.Append("<a href=http://localhost:2464/SampleApplication/ResetPassword.aspx?emailId=" + txtEmailId.Text + "&uName=" + txtUserName.Text + "&uCode=" + uniqueCode + ">Click here to change your password</a>");
            // sbody.Append("&uCode=" + uniqueCode + "&uName=" + txtUserName.Text + ">Click here to change your password</a>");

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("SenderEmailIAddress@gmail.com", dr["EmailId"].ToString(), "Reset Your Password", strBody.ToString());
            //pasing the Gmail credentials to send the email

            System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("SenderEmailIAddress@gmail.com", "SenderPassword");

            System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
            mailclient.EnableSsl = true;
            mailclient.UseDefaultCredentials = false;
            mailclient.Credentials = mailAuthenticaion;
            mail.IsBodyHtml = true;
            mailclient.Send(mail);
            dr.Close();
            dr.Dispose();
            cmd.ExecuteReader();
            cmd.Dispose();
            con.Close();
            lblStatus.Text = "Reset password link has been sent to your email address";
            txtEmailId.Text = string.Empty;
            txtUserName.Text = string.Empty;
        }
        else
        {
            lblStatus.Text = "Please enter valid email address or username";
            txtEmailId.Text = string.Empty;
            txtUserName.Text = string.Empty;
            con.Close();
            return;
        }
    }
    catch (Exception ex)
    {
        lblStatus.Text = "Error Occured: " + ex.Message.ToString();
    }
    finally
    {
        cmd.Dispose();
    }
string uniqueCode=string.Empty;
SqlCommand cmd=新的SqlCommand();
SqlDataReader-dr;
尝试
{
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“conn”].ConnectionString);
if(con.State==ConnectionState.Closed)
{
con.Open();
}
//获取与提供的用户名或电子邮件id匹配的记录。
cmd=new SqlCommand(“从Tbl_登录中选择*,其中UserName COLLATE Latin1_general_CS_AS=@UserName或EmailId COLLATE Latin1_general_CS_AS=@EmailId”,con);
cmd.Parameters.AddWithValue(“@username”,Convert.ToString(txtUserName.Text.Trim());
cmd.Parameters.AddWithValue(“@emailId”,Convert.ToString(txtEmailId.Text.Trim());
dr=cmd.ExecuteReader();
cmd.Dispose();
如果(哈斯罗博士)
{
里德博士();
//生成唯一代码
uniqueCode=Convert.ToString(System.Guid.NewGuid());
//在数据库表的UniquCode字段中更新唯一的随机码
cmd=new SqlCommand(“更新Tbl_登录集Uniquecode=@Uniquecode,其中UserName=@UserName或EmailId=@EmailId”,con”);
cmd.Parameters.AddWithValue(“@uniqueCode”,uniqueCode);
cmd.Parameters.AddWithValue(“@username”,txtUserName.Text.Trim());
cmd.Parameters.AddWithValue(“@emailid”,txtEmailId.Text.Trim());
StringBuilder strBody=新的StringBuilder();
//通过querystring传递emailid、用户名和生成的唯一代码。用于测试,请传递本地主机号,并在联机时传递域名而不是本地主机路径。
strBody.Append(“”);
//sbody.Append(“&uCode=“+uniqueCode+”&uName=“+txtUserName.Text+”>单击此处更改密码”);
System.Net.Mail.MailMessage Mail=新的System.Net.Mail.MailMessage(“SenderEmailIAddress@gmail.com,dr[“EmailId”].ToString(),“重置密码”,strobdy.ToString());
//使用Gmail凭据发送电子邮件
System.Net.NetworkCredential MailAuthentication=新的System.Net.NetworkCredential(“SenderEmailIAddress@gmail.com“,“发件人密码”);
System.Net.Mail.SmtpClient-mailclient=new System.Net.Mail.SmtpClient(“smtp.gmail.com”,587);
mailclient.EnableSsl=true;
mailclient.UseDefaultCredentials=false;
mailclient.Credentials=邮件身份验证;
mail.IsBodyHtml=true;
mailclient.Send(mail);
Close博士();
dr.Dispose();
cmd.ExecuteReader();
cmd.Dispose();
con.Close();
lblStatus.Text=“重置密码链接已发送到您的电子邮件地址”;
txtEmailId.Text=string.Empty;
txtUserName.Text=string.Empty;
}
其他的
{
lblStatus.Text=“请输入有效的电子邮件地址或用户名”;
txtEmailId.Text=string.Empty;
txtUserName.Text=string.Empty;
con.Close();
回来
}
}
捕获(例外情况除外)
{
lblStatus.Text=“发生错误:”+ex.Message.ToString();
}
最后
{
cmd.Dispose();
}
网络。con文件代码如下,不知道其正确性

   <system.net>
     <mailSettings>
     <smtp from="abc@somedomain.com">
      <network host="somesmtpserver" port="25" userName="name"         
                                 password="pass" defaultCredentials="true" />
  </smtp>
</mailSettings>


您会遇到哪种错误?您是否尝试降低gmail帐户设置的安全性?发送邮件失败错误,我不知道发生了什么,这是防病毒问题或gmail帐户设置不知道如何做。发送邮件失败@Felicem此方法正在更新数据库、发送电子邮件和更新UI。。。?您可能希望打破这种局面,并用单独的方法关注每项任务。您是否检查了
异常的堆栈跟踪
?从浏览器访问您的gamail帐户并检查您的电子邮件发生了什么。很可能是因为您使用外部脚本发送电子邮件,邮件被gmail安全性阻止。如果是这样,只需在安全设置中设置“不太安全”,它就可以工作了。我用同样的代码遇到同样的问题。
MailMessage mail = new MailMessage();
mail.To.Add(abc222@rty.com);
mail.From = new MailAddress("abc@xyz.com");
mail.CC.Add("def@xyz.com");
mail.Subject = "Write Subject Here";
string body = "Write your body Here";

mail.Body = body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("abc@xyz.com");
mail.Headers.Add("Disposition-Notification-To", "abc@xyz.com");
smtp.Host = "XXX.XXX.XX.XX"; //Or Your SMTP Server Address
smtp.Port = 25;

//Or your Smtp Email ID and Password
smtp.EnableSsl = false;
smtp.Send(mail);

****If you are using Gmail account-**** 

 smtp.Port = 587;
smtp.EnableSsl = true;