【问题标题】:Failure sending mail using asp.net使用 asp.net 发送邮件失败
【发布时间】:2015-05-03 20:47:02
【问题描述】:

以下是我用于向邮件发送确认链接的代码,我收到了诸如发送邮件失败之类的错误。

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

 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();
    }

网络。下面给出的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
  • 这个方法是更新数据库发送邮件更新用户界面...?您可能希望打破这一点,并使用单独的方法专注于每项任务。你检查过ExceptionStack Trace吗?
  • 从浏览器访问您的 gamail 帐户并检查您的电子邮件发生了什么。很可能是因为您使用外部脚本发送电子邮件,邮件被 gmail 安全阻止。如果是这样,只需在安全设置中设置“不太安全”,它应该可以工作。我用相同的代码遇到了同样的问题。

标签: c# asp.net


【解决方案1】:
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2014-08-29
    • 2016-04-11
    • 2015-06-23
    相关资源
    最近更新 更多