在smtp/中发送邮件失败smtp服务器需要安全连接,或者客户端未通过身份验证

本文关键字:smtp 或者 安全 客户端 连接 身份验证 服务器 失败 | 更新日期: 2023-09-27 18:29:23

我正在尝试重置密码并恢复密码功能。因此,我使用smtp方式发送邮件。然而,我在尝试接球时出错了。

Error Occured: Failure sending mail.

我也不确定我应该在web.config文件的哪一部分添加这个连接代码

<network host="smtp.gmail.com" enableSsl="true"  />

下面是我在使用smtp逻辑时与Azure数据库的连接。

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string uniqueCode = string.Empty;
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            // get the records matching the supplied username or email id.         
            cmd = new SqlCommand("select * from MemberAccount where nric COLLATE Latin1_general_CS_AS=@nric or email COLLATE Latin1_general_CS_AS=@email", con);
            cmd.Parameters.AddWithValue("@nric", Convert.ToString(txtUserName.Text.Trim()));
            cmd.Parameters.AddWithValue("@email", 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 MemberAccount set UniqueCode=@uniqueCode where nric=@nric or email=@email", con);
                cmd.Parameters.AddWithValue("@uniqueCode", uniqueCode);
                cmd.Parameters.AddWithValue("@nric", txtUserName.Text.Trim());
                cmd.Parameters.AddWithValue("@email", 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://sipolice.azurewebsites.net/MemberRecoverPassword.aspx" + 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@hotmail.com", dr["email"].ToString(), "Reset Your Password", strBody.ToString());
                //pasing the Gmail credentials to send the email
                System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("SenderEmailIAddress@hotmail.com", "SenderPassword");
                System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.hotmail.com", 587);
                mailclient.EnableSsl = true;
                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();
        }
    }

更新我刚刚意识到为什么它不能工作。它不起作用,因为我的帐户信息不正确

在smtp/中发送邮件失败smtp服务器需要安全连接,或者客户端未通过身份验证

为什么有这么多废话代码。尝试简单的方式。,

例如:

在您的Web.Config/App.Config:

      <system.net>
        <mailSettings>
          <smtp from="Your Email ID">
            <network host="Your Host Namecom" defaultCredentials="false" password="*********" userName="Your User Name"  enableSsl="true" />
          </smtp>
        </mailSettings>
      </system.net>

在您的代码背后:

        private void SendMail(string Address, string Body)
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(Address);
                mailMessage.Subject = "Your Subject";
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = Body;
                SmtpClient smtpClient = new SmtpClient(); // This will take the Credentioals from the WEB/APP Config 
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                Extention.Log(ex.Message + "/=> " + ex.StackTrace);
            }
        }

这样调用函数:

SendMail("To Address Email Id", strBody.ToString());

它将工作:)

我在您的代码中看到您使用了hotmail smtp。对于我的网站,我也使用该服务器。

如果您使用这些服务器,您必须知道您的凭据是您的hotmail登录名。邮件的发件人也必须是相同的地址。

我写了一个发送邮件的快速脚本并进行了测试。它很有效:

using (var client = new System.Net.Mail.SmtpClient("smtp.live.com", 25))
{
    client.Credentials = new System.Net.NetworkCredential("example@hotmail.com", "******");
    client.EnableSsl = true;
    var from = new System.Net.Mail.MailAddress("example@hotmail.com", "Your name");
    var to = new System.Net.Mail.MailAddress("target@example.com", "Receiver name");
    var message = new System.Net.Mail.MailMessage(from, to);
    message.Subject = "Test mail";
    message.Body = "Content";
    client.Send(message);
}

如果您在代码中指定了所有SMTP信息,则不需要在web.config中指定。


Microsoft smtp(hotmail/live/outlook等)

主机:smtp.live.com
端口:587或如果587被阻止25
TSL/SSL:
身份验证:您的hotmail/live/outlook帐户
发件人:始终使用您的hotmail/live/outlook地址

Gmail smtp

主机:smtp.gmail.com
端口:465
TSL/SSL:
身份验证:您的gmail帐户
发件人:始终是您的gmail地址

相关文章: