发送电​​子邮件时,“提交”按钮,点击 [英] Send email when 'submit' button clicked

查看:82
本文介绍了发送电​​子邮件时,“提交”按钮,点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是在会话存储数据的4页ASP.NET表单。当提交按钮第3页点击我想要的细节进入到通过电子邮件发送给我的Hotmail帐户,但我似乎无法得到它的工作,因为它总是落在挺直了我的

I have a 4 page ASP.NET form which is storing data in the session. When the 'Submit' button on the 3rd page is clicked i want the details entered to be emailed to my hotmail account but i can't seem to get it to work as it always falls stright into my 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>

code为我的的onclick

Code for my onclick is

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

任何人都可以帮助我。

Can anyone help me out.

我从 http://www.serversmtp.com/en/smtp-的SMTP细节Hotmail的但总是失败

推荐答案

我已经更新了您的code,尝试,因为你把它发送给自己使用相同的电子邮件从和到,也可以使用您的Hotmail密码该实例的NetworkCredential的一部分:

I have updated your code, try to use the same email for From and To since you are sending it to yourself and also use your hotmail password as part of the NetworkCredential instantiation:

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,而不是发送邮件从雅虎,Gmail时,Hotmail的(C#)

这篇关于发送电​​子邮件时,“提交”按钮,点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆