【问题标题】:could not send email in asp.net无法在 asp.net 中发送电子邮件
【发布时间】:2021-04-22 00:08:28
【问题描述】:
<div id="id04" class="modal">
                <form class="modal-content" action="#">
                    <span onclick="document.getElementById('id04').style.display='none'" class="close" title="Close Modal">&times;</span>
                    <div class="modalcontainer">
                        <p style="font-family: 'Playfair Display', serif; font-size: 20px;">Reset Password</p>
                        <asp:TextBox ID="txt_resetEmail" CssClass="inputtxt" PlaceHolder="Email Address" runat="server" TextMode="Email"></asp:TextBox>
                        <asp:TextBox ID="txt_resetPassword" CssClass="inputtxt" PlaceHolder="New Password" runat="server" TextMode="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" ></asp:TextBox>
                        <asp:TextBox ID="txt_confirmNewPassword" CssClass="inputtxt" PlaceHolder="Confirm Password" runat="server" TextMode="password"></asp:TextBox>
                        <asp:CompareValidator
                            ID="CompareValidator2"
                            runat="server"
                            ErrorMessage="Passwords do not match."
                            ControlToValidate="txt_confirmNewPassword"
                            ControlToCompare="txt_resetPassword"
                            Operator="Equal" Type="String"
                            ForeColor="Red">
                        </asp:CompareValidator>

                        <asp:Button ID="Button1" class="btnsignin" runat="server" Text="Confirm" OnClick="btnSignIn_Confirm"/>
                        //when the user clicks on this button, email is being sent

                    </div>

                    <div class="register">
                        Don't have an account? 
                        <a onclick="document.getElementById('id01').style.display='none';document.getElementById('id02').style.display='block';">Create an Account
                        </a>
                        <br />
                        <a style="color: #EC7063; font-size: 12px;"
                            onclick="document.getElementById('id01').style.display='none';document.getElementById('id03').style.display='block';">Sign in as Admin</a>
                    </div>

                    

                </form>
            </div>

//MasterPage.master.cs中的后端代码

protected void btnSignIn_Confirm(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("come in");

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add("to email");
        mail.From = new MailAddress("from email", "head", System.Text.Encoding.UTF8);
        mail.Subject = "This mail is send from asp.net application";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "This is Email Body Text";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("from email", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;

        try
        {
            client.Send(mail);
            System.Diagnostics.Debug.WriteLine("Can send email");
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.WriteLine("soemthing wrong");
        }
    }

system.Diagnostic.Writeline("") 也没有在我的输出上写任何东西,所以我不知道我的代码要去哪里

当我点击按钮时,我的网页只是刷新并且没有显示任何内容,我在我的代码中为“mail.from”创建了一个真实的电子邮件,并将我的个人电子邮件用作“mail.ToAdd”。但我没有收到任何东西

【问题讨论】:

  • 您的 Google 帐户有多重身份验证吗?然后看看你是否可以在这里创建一个应用密码
  • 创建文件日志以检查异常 - 可能您尝试以错误的方式读取调试信息

标签: c# asp.net email webforms


【解决方案1】:

首先尝试为您的 google 帐户打开不太安全的应用程序,以便在此 link 闲置 如果不工作 尝试在 SmptClient 的构造函数中提供主机值 喜欢做

  client.Host = "smtp.gmail.com";

  SmtpClient client = new SmtpClient("smtp.gmail.com");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
相关资源
最近更新 更多