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

//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("") 也沒有在我的 output 上寫任何東西,所以我不知道我的代碼要去哪里

當我點擊按鈕時,我的網頁剛剛刷新並且沒有顯示任何內容,我在我的代碼中為“mail.from”創建了一個真正的 email,並使用我個人的 email 作為“mail.ToAdd”。 但我沒有收到任何東西

首先嘗試為您的谷歌帳戶打開不太安全的應用程序,如果這個鏈接不起作用,請嘗試在 SmptClient 的構造函數中提供 Host 值

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

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

暫無
暫無

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

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