未使用 gmail 信息发送电子邮件

Email not being sent using gmail info

好的,所以我一直试图对此进行排序大约 3 个小时,但无济于事。我做了一个简单的联系我们表格。它所需要做的就是将邮件发送到我的 gmail 帐户。它没有发送任何东西,也没有给出任何错误。我尝试关闭两步验证,但这也无济于事。 我的 aspx 代码:

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
                        <p>
                            Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
                        </p>
                        <p>
                            Your name:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
        ControlToValidate="YourName" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
                            Your email address:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
        ControlToValidate="YourEmail" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
                            <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
                                SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
                                ValidationGroup="save" ForeColor="Red" /><br />
                            Subject:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
        ControlToValidate="YourSubject" ValidationGroup="save" /><br />
                            <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
                            Your Question:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
        ControlToValidate="Comments" ValidationGroup="save" /><br />
                            <asp:TextBox ID="Comments" runat="server"
                                TextMode="MultiLine" Rows="10" Width="400px" />
                        </p>
                        <p>
                            <asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
                        </p>
                    </asp:Panel>

我的 C# 代码:

  try
{
    MailMessage Msg = new MailMessage();
    // Sender e-mail address.
    Msg.From = new MailAddress(YourEmail.Text);
    // Recipient e-mail address.
    Msg.To.Add("*****@gmail.com");
    Msg.Subject = YourSubject.Text;
    Msg.Body = Comments.Text;
    // your remote SMTP server IP.
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential("****@gmail.com", "*********");
    smtp.EnableSsl = true;
    smtp.Send(Msg);
    //Msg = null;
    DisplayMessage.Text = "Thanks for Contacting us";
    // Clear the textbox valuess
    YourName.Text = "";
    YourSubject.Text = "";
    Comments.Text = "";
    YourEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}

任何帮助将不胜感激

您需要放置正确的 credential 来发送邮件。

看这里

ASPX代码

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
        <p>
            Please Fill the Following to Send us an E-Mail. We will get back to you ASAP!
        </p>
        <p>
            Your name:
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
    ControlToValidate="YourName" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
            Your email address:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
    ControlToValidate="YourEmail" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
            <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
                SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
                ValidationGroup="save" ForeColor="Red" /><br />
            Subject:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
    ControlToValidate="YourSubject" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
            Your Question:
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
    ControlToValidate="Comments" ValidationGroup="save" /><br />
            <asp:TextBox ID="Comments" runat="server"
                TextMode="MultiLine" Rows="10" Width="400px" />
        </p>
        <p>
            <asp:Button ID="btnSubmit" runat="server" Text="Send" ValidationGroup="save" Height="36px" OnClick="btnSubmit_Click" Width="86px" />
        </p>
    </asp:Panel>

CS 代码

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(YourEmail.Text);
            // Recipient e-mail address.
            Msg.To.Add("test@gmail.com");
            Msg.Subject = YourSubject.Text;
            Msg.Body = Comments.Text;
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("YOURGMAILID", "YOURGMAIL PASSWORD");  // IT SHOULD BE CORRECT TO WORK
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            //Msg = null;
           // DisplayMessage.Text = "Thanks for Contacting us";
            // Clear the textbox valuess
            YourName.Text = "";
            YourSubject.Text = "";
            Comments.Text = "";
            YourEmail.Text = "";
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }

希望对您有所帮助。 :)