简体   繁体   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

I am developing a small application in asp.net using C#, where if user forgets password, I want to send the password from database to user's mail when clicked on forget password button.我正在使用 C# 在 asp.net 中开发一个小应用程序,如果用户忘记密码,我想在单击忘记密码按钮时将密码从数据库发送到用户的邮件。 I was successful in sending the password in an email but in local machine.我成功地通过电子邮件但在本地计算机中发送了密码。 The settings which I am using, on the local machine, I used on the server too.我在本地机器上使用的设置,我也在服务器上使用过。 But if I try on the server, I was thrown with an error saying "Failure sending email".但是,如果我尝试在服务器上尝试,则会出现“发送电子邮件失败”的错误消息。 Can anybody please tell me where I am making a mistake and what to do further if it has to work in server.任何人都可以告诉我我在哪里犯了错误以及如果它必须在服务器中工作该怎么做?

Below is my code :下面是我的代码:

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

I've got the exact same thing working in a live environment, the only difference is I have the following properties set on my smtp class as well:我在实时环境中使用了完全相同的东西,唯一的区别是我的 smtp 类也设置了以下属性:

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

I got the same issue few days back.几天前我遇到了同样的问题。 Remove smtp.Port = 587 from your code and it will work从您的代码中删除 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;
    }

This is my code and it works too.这是我的代码,它也有效。 (NOTE: I've commented smtp.port) (注意:我已经评论了 smtp.port)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 本地计算机保存报告/如何使用ASP.NET C#本地计算机路径 - Local Machine Saving Report / How to path of local machine using ASP.NET C# 通过本地主机服务器在asp.net中发送电子邮件 - Sending email in asp.net via local host server 邮件发送代码在本地运行,但在ASP.NET MVC中的服务器上不运行 - Mail sending code works at local but not on server in asp.net mvc asp.net C#Web窗体应用程序中的密码恢复在localhost上有效,但在Godaddy服务器上不起作用 - Password recovery in asp.net c# web forms application works on localhost but not on Godaddy server ASP.NET C#-从Outlook Live SMTP服务器发送电子邮件 - ASP.NET C# - Sending Email from Outlook Live SMTP Server 在电子邮件正文中发送图像,Android作为客户端,Asp.net c#作为服务器端 - Sending image in email body, Android as client side and Asp.net c# as server side C#asp.net电子邮件发送失败! 服务器响应为:5.5.1需要身份验证 - C# asp.net email sending failure! The server response was: 5.5.1 Authentication Required ASP.NET C# - 从Gmail / Yahoo Live SMTP Server发送电子邮件不起作用 - ASP.NET C# - Sending Email from Gmail/Yahoo Live SMTP Server not working asp.net c# 代码(从 VS2022 和 IIS10)在本地机器上工作以连接到 SQL Server DB 它在 Web 服务器上不起作用 - asp.net c# code works (from VS2022 and on IIS10) on local machine to connect to SQL Server DB it does not work on Webserver 在ASP.NET C#中发送电子邮件时出现问题 - Issue while sending email in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM