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

时间:2015-03-12 10:59:52

标签: c# asp.net gmail

好的,所以我一直试图对它进行约3小时的排序,但无济于事。我已经联系了一份简单的表格。它需要做的只是将消息发送到我的Gmail帐户。它没有发送任何内容,也没有发出任何错误。我尝试关闭2步验证,但这也没有帮助。 我的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);
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您需要输入正确的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 CODE

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);
        }
    }
希望它有所帮助。 :)