Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在'时发送电子邮件;提交';点击按钮_C#_Asp.net_Webforms - Fatal编程技术网

C# 在'时发送电子邮件;提交';点击按钮

C# 在'时发送电子邮件;提交';点击按钮,c#,asp.net,webforms,C#,Asp.net,Webforms,我有一个4页的ASP.NET表单,它在会话中存储数据。单击第3页上的“提交”按钮时,我希望输入的详细信息通过电子邮件发送到我的hotmail帐户,但我似乎无法使其正常工作,因为它总是直接落入我的陷阱中 HTML是 <table> <tr> <td>Name:</td> <td> <asp:TextBox ID="txtName" run

我有一个4页的ASP.NET表单,它在会话中存储数据。单击第3页上的“提交”按钮时,我希望输入的详细信息通过电子邮件发送到我的hotmail帐户,但我似乎无法使其正常工作,因为它总是直接落入我的
陷阱中

HTML是

<table>
        <tr>
            <td>Name:</td>
            <td>
                <asp:TextBox ID="txtName" runat="server" Columns="50"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Subject:</td>
            <td>
                <asp:DropDownList ID="ddlSubject" runat="server">
                    <asp:ListItem>Ask a question</asp:ListItem>
                    <asp:ListItem>Report a bug</asp:ListItem>
                    <asp:ListItem>Customer support ticket</asp:ListItem>
                    <asp:ListItem>Other</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>Message:</td>
            <td>
                <asp:TextBox ID="txtMessage" runat="server" Columns="40" Rows="6" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblResult" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
using System.Net.Mail;
using System.Net;

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        //Create the msg object to be sent
        MailMessage msg = new MailMessage();
        //Add your email address to the recipients
        msg.To.Add("**SHOULD THIS BE MY EMAIL ADDRESS?**");
        //Configure the address we are sending the mail from **- NOT SURE IF I NEED THIS OR NOT?**
        //MailAddress address = new MailAddress("SenderAddress@gmail.com");
        //msg.From = address;
        //Append their name in the beginning of the subject
        msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
        msg.Body = txtMessage.Text;

        //Configure an SmtpClient to send the mail.
        SmtpClient client = new SmtpClient("smtp.live.com", 465);
        client.EnableSsl = true; //only enable this if your provider requires it
        //Setup credentials to login to our sender email address ("UserName", "Password")
        NetworkCredential credentials = new NetworkCredential("MY@EMAIL ADDRESS", "**SHOULD THIS BE MY PASSOWRD?**");
        client.Credentials = credentials;

        //Send the msg
        client.Send(msg);

        //Display some feedback to the user to let them know it was sent
        lblResult.Text = "Your message was sent!";

        //Clear the form
        txtName.Text = "";
        txtMessage.Text = "";
    }
    catch
    {
        //If the message failed at some point, let the user know
        lblResult.Text = "Your message failed to send, please try again.";
    }
}
有人能帮我吗


我从中获得了SMTP详细信息,但总是失败

我已更新了您的代码,尝试使用相同的电子邮件发送发件人和收件人,因为您要将其发送给自己,并且在NetworkCredential实例化中使用hotmail密码:

using System.Net.Mail;
using System.Net;

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("youremail@hotmail.com");
            //Configure the address we are sending the mail from **- NOT SURE IF I NEED THIS OR NOT?**
            MailAddress address = new MailAddress("youremail@hotmail.com");
            msg.From = address;
            //Append their name in the beginning of the subject
            msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
            msg.Body = txtMessage.Text;

            //Configure an SmtpClient to send the mail.
            SmtpClient client = new SmtpClient("smtp.live.com", 465);
            client.EnableSsl = true; //only enable this if your provider requires it
            //Setup credentials to login to our sender email address ("UserName", "Password")
            NetworkCredential credentials = new NetworkCredential("youremail@hotmail.com", "YourPassword");
            client.Credentials = credentials;

            //Send the msg
            client.Send(msg);

            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";

            //Clear the form
            txtName.Text = "";
            txtMessage.Text = "";
        }
        catch
        {
            //If the message failed at some point, let the user know
            lblResult.Text = "Your message failed to send, please try again.";
        }
    }

还要确保smtp.live.com的465端口号正确。您可以改为试试587

您收到的错误是什么?但我似乎无法让它工作,这对任何人都没有帮助。。你需要更详细地解释。是获取无错误应用程序的最佳方法。@Izzy,@Tjaart van der Walt它直接进入我的
catch
update
catch
as
catch(异常示例)
然后阅读ex中的内容。我的失败原因是因为“发件人”,因为我已将其发送出去,并且我已将其更改为使用端口465。您是否可以查看我的新邮件,看看您是否可以在这方面提供帮助