繁体   English   中英

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

[英]Send email when 'submit' button clicked

我有一个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详细信息,但始终失败

我已经更新了您的代码,由于您是将其发送给自己,因此请尝试对发件人和发件人使用相同的电子邮件,并在网络凭据实例化的过程中使用您的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.";
        }
    }

还要确保465端口号对于smtp.live.com是正确的。 您可以尝试587,而不是从Yahoo!,GMail,Hotmail(C#)发送电子邮件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM