如何继续发送电子邮件aspx.net C#

时间:2014-05-31 06:34:20

标签: c# asp.net email gmail

我编写了一个联系页面代码,允许用户输入4个文本框,然后使用这些详细信息从Gmail帐户发送电子邮件。但是,我有一个错误,我不知道如何修复代码在文本框中读取电子邮件地址的位置。感谢您的时间,感谢您的帮助!

HTML

            <td class="auto-style2">
                <asp:TextBox ID="nameContactUsTextBox" runat="server" 
                    CssClass="boring" ToolTip="Enter Name" Width="441px"></asp:TextBox>

            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ErrorMessage="Name is required" ControlToValidate="nameContactUsTextBox"
                    Text="*"
                    Forecolor="Red">
                </asp:RequiredFieldValidator>           
            </td>
        </tr>
        <tr>
            <td class="boring">E-Mail</td>
            <td class="auto-style3">
                <asp:TextBox ID="emailContactUsTextBox" runat="server" CssClass="boring" 
                    ToolTip="Enter Email" Width="443px"></asp:TextBox>
            </td>
            <td>    
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ErrorMessage="E-Mail field is required" ControlToValidate="emailContactUsTextBox"
                    Text="*"
                    Display="Dynamic"
                    Forecolor="Red">
                </asp:RequiredFieldValidator>

                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                    ErrorMessage="Please enter a valid email" 
                    Display="Dynamic"
                    ControlToValidate="emailContactUsTextBox" ForeColor="Red">*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="boring">Subject</td>
            <td class="auto-style2">
                <asp:TextBox ID="subjectContactUsTextbox" runat="server" 
                    CssClass="boring" ToolTip="Enter Subject" Width="441px"></asp:TextBox>

            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ErrorMessage="Subject is required" ControlToValidate="subjectContactUsTextbox"
                    Text="*"
                    Forecolor="Red">
                </asp:RequiredFieldValidator>           
            </td>
        </tr>

            <tr>
            <td class="boring" style="vertical-align:top">Comments</td>
            <td class="auto-style2">
                <asp:TextBox ID="commentTextBox" runat="server" 
                    CssClass="boring" ToolTip="Enter Comment" Width="448px" Height="150px" TextMode="MultiLine" 
                    ></asp:TextBox>

            </td>
            <td style="vertical-align:top">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                    ErrorMessage="Comments are required" ControlToValidate="commentTextBox"
                    Text="*"
                    Forecolor="Red">
                </asp:RequiredFieldValidator>           
            </td>
        </tr>

        <tr>
            <td colspan="3">
                <asp:ValidationSummary HeaderText="Please fix the following errors:" 
                    Forecolor="Red" ID="ValidationSummary1" runat="server" CssClass="classic" />
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:Label ID="thankYouLabel" Font-Bold="True" runat="server"></asp:Label>
            </td>

        </tr>
        <tr>
        <td colspan="3">
                <asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />
            </td>
        </tr>



    try //indented
    {
        if (Page.IsValid)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("emailContactUsTextBox.Text");
            mailMessage.To.Add("moderator.ad.badshopblog.a2@gmail.com");
            mailMessage.Subject = subjectContactUsTextbox.Text;

            mailMessage.Body = "<b>Sender Name : </b>" + nameContactUsTextBox.Text + "<br/>"
                + "<b>Sender Email : </b>" + emailContactUsTextBox.Text + "<br/>"
                + "<b>Comments : </b>" + commentTextBox.Text + "<br/>";

            mailMessage.IsBodyHtml = true;

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

            smtpClient.EnableSsl = true;

            smtpClient.Credentials = new System.Net.NetworkCredential("moderator.ad.badshopblog.a2@gmail.com", "");
            smtpClient.Send(mailMessage);

            thankYouLabel.ForeColor = System.Drawing.Color.Blue;
            thankYouLabel.Text = "Thank you for contacting us.";

            nameContactUsTextBox.Enabled = false;
            emailContactUsTextBox.Enabled = false;
            subjectContactUsTextbox.Enabled = false;
            commentTextBox.Enabled = false;
            submitButton.Enabled = false;
        }

1 个答案:

答案 0 :(得分:0)

我想问题就在于这一行:

mailMessage.From = new MailAddress("emailContactUsTextBox.Text");

应该是:

mailMessage.From = new MailAddress(emailContactUsTextBox.Text);

此外,更好的日志记录可能会有所帮助 - 将异常写入日志文件,并从那里了解问题。