单击“提交”按钮时发送电子邮件

时间:2015-07-31 11:32:01

标签: c# asp.net webforms

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

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>

onclick的代码

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.";
    }
}

任何人都可以帮助我。

我从http://www.serversmtp.com/en/smtp-hotmail获得了SMTP详细信息,但它总是失败

1 个答案:

答案 0 :(得分:2)

我已更新您的代码,尝试使用相同的电子邮件发送给自己,因为您将其发送给自己,并使用您的Hotmail密码作为NetworkCredential实例化的一部分:

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.";
        }
    }

还要确保465端口号对于smtp.live.com是正确的。您可以尝试587而不是Send Email from Yahoo!, GMail, Hotmail (C#)