简体   繁体   English

无法在 asp.net 中发送 email

[英]could not send email in asp.net

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

//backend code in the MasterPage.master.cs //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");
        }
    }

the system.Diagnostic.Writeline("") is also not writing anything on my output so I have no idea where my code is going system.Diagnostic.Writeline("") 也没有在我的 output 上写任何东西,所以我不知道我的代码要去哪里

when I click on the button, my webpage just refreshes and nothing is being shown, I have created a real email for the "mail.from" in my code, and use my personal email as the "mail.ToAdd".当我点击按钮时,我的网页刚刚刷新并且没有显示任何内容,我在我的代码中为“mail.from”创建了一个真正的 email,并使用我个人的 email 作为“mail.ToAdd”。 But I did not receive anything但我没有收到任何东西

First try turn on less secure apps on for your google account to that fallow this link if not work's try providing Host value in Constructor of SmptClient insted of doing首先尝试为您的谷歌帐户打开不太安全的应用程序,如果这个链接不起作用,请尝试在 SmptClient 的构造函数中提供 Host 值

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

do

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM