簡體   English   中英

在電子郵件中發送忘記的用戶密碼在本地機器上成功運行,但在使用 c# asp.net 的服務器上失敗

[英]Sending forgot password of user in an email works successfully in local machine but fails on server using c# asp.net

我正在使用 C# 在 asp.net 中開發一個小應用程序,如果用戶忘記密碼,我想在單擊忘記密碼按鈕時將密碼從數據庫發送到用戶的郵件。 我成功地通過電子郵件但在本地計算機中發送了密碼。 我在本地機器上使用的設置,我也在服務器上使用過。 但是,如果我嘗試在服務器上嘗試,則會出現“發送電子郵件失敗”的錯誤消息。 任何人都可以告訴我我在哪里犯了錯誤以及如果它必須在服務器中工作該怎么做?

下面是我的代碼:

try
        {
                if (ds.Tables[0].Rows.Count > 0)
                 {
                    MailMessage Msg = new MailMessage();

                    Msg.From = new MailAddress("mymail@gmail.com");

                    Msg.To.Add(new MailAddress(txtboxforgotemail.Text));
                    Msg.Subject = "Forgot Password :";
                    Msg.Body = "Hi, Dear '" + ds.Tables[0].Rows[0]["username"] + "' , your password is:" + ds.Tables[0].Rows[0]["password"] + "";
                    Msg.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.UseDefaultCredentials = false;
                    smtp.Port = 587;
                    smtp.Credentials = new System.Net.NetworkCredential ("mymail@gmail.com","mypassword");
                    smtp.Send(Msg);
                    lblforgotemail.Text = "Your Password Details Sent to your mail";
                    txtboxforgotemail.Text = "";
             }
             else
                    {
                        lblforgotemail.Text = "The email you entered does not exist.";
                    }  
        }
        catch (Exception ex)
        {
            txtboxforgotemail.Text = "";
            lblforgotemail.Text = ex.Message;
        }

我在實時環境中使用了完全相同的東西,唯一的區別是我的 smtp 類也設置了以下屬性:

smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

幾天前我遇到了同樣的問題。 從您的代碼中刪除 smtp.Port = 587 它將起作用

 try
        {
            string admin_email = "youremail"
            string admin_emailpwd = "your pwd"

            MailMessage mail = new MailMessage();
            mail.To.Add(SendTo);
            mail.From = new MailAddress(admin_email);
            mail.Subject = Subject;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "SMTP.gmail.com";
          //  smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(admin_email, admin_emailpwd);
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch
        {
            return false;
        }
        return true;
    }

這是我的代碼,它也有效。 (注意:我已經評論了 smtp.port)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM