将文件上传到Intranet上的表单并通过电子邮件发送

时间:2015-08-04 17:03:18

标签: html asp.net file-upload webforms

我们在公司内部网上有一个用户输入数据的表单,然后他们点击提交数据,这些数据被编译成电子邮件并发送到特定的用户列表。我们有site.aspx.cs,我指定通过电子邮件发送的数据。

我们正在添加一个字段,用户可以在执行表单时上传文件,但是我在将此文件添加到.cs文件时遇到问题,以便它可以通过电子邮件发送数据。

这是我创建字段的HTML和上传按钮

<body>
<div class="containerContent">
    <div class="content">
        <div class="outer4ColLeft">
      <ul class="subnav">
        <li>
          <a href="../corporate_resources.aspx">Documents</a>
        </li>
          <li>
           <a href="../mission_ethics.aspx">Mission &amp; Ethics Statements</a>
          </li>
          <li>
           <a href="../history.aspx">History</a>
          </li> 
          <li>
           <a href="../associate_profiles.aspx">Associate Profiles</a>
          </li>           
        <li class="activeSub">
          <a href="../corporate_form.aspx">Corporate Forms</a>
        </li>                 
      </ul>
<!--#include file="../includes/quicklinksForm.aspx"-->       
        </div>
        <div class="outer12ColRight">
            <h1>
                <span></span>
                <img height="45" alt="Odd Occurrence Report" src="../images/corporate/h1_oddOccurrence.jpg" />
            </h1>
            <div class="formContent">

<form id="form1" class="forms" runat="server">
    <asp:Panel ID="formPanel" Visible="true" runat="server">
        <h3>Note: An odd occurrence is any unplanned interruption</h3>
            <asp:ValidationSummary ID="oddValidationSummary"  class=".redText" runat="server" Font-Bold="True" />
                    <fieldset>
                        <legend>General</legend>
                        <div class="profileRow1Col1">
                            <p>
                                <asp:RequiredFieldValidator ID="valPartNumber" runat="server" ControlToValidate="txtPartNumber" ErrorMessage="Part Number is required.">*</asp:RequiredFieldValidator>
                                Part Number</p>
                            <p>
                                <asp:RequiredFieldValidator ID="valEmployeeID" runat="server" ControlToValidate="txtEmployeeID" ErrorMessage="Employee ID is required.">*</asp:RequiredFieldValidator>
                                Employee ID</p>
                            <p>
                                <asp:RequiredFieldValidator ID="valCurrentJobNumber" runat="server" ControlToValidate="txtCurrentJobNumber" ErrorMessage="Current Job Number is required.">*</asp:RequiredFieldValidator>
                                Current Job Number</p>
                            <p>
                                <asp:RequiredFieldValidator ID="valRevisionLevel" runat="server" ControlToValidate="txtRevisionLevel" ErrorMessage="Revision Level is required.">*</asp:RequiredFieldValidator>
                                Revision Level</p>
                            <p>
                                <asp:RequiredFieldValidator ID="valOperationNumber" runat="server" ControlToValidate="txtOperationNumber" ErrorMessage="Operation Number is required.">*</asp:RequiredFieldValidator>
                                Revision Level</p>
                        </div>
                        <div class="profileRow1Col2">
                            <p>
                                <asp:TextBox ID="txtPartNumber" runat="server"></asp:TextBox>
                            </p>
                            <p>
                                <asp:TextBox ID="txtEmployeeID" runat="server"></asp:TextBox>
                            </p>
                            <p>
                                <asp:TextBox ID="txtCurrentJobNumber" runat="server"></asp:TextBox>
                            <p>
                                <asp:TextBox ID="txtRevisionLevel" runat="server"></asp:TextBox>
                            </p>
                            <p>
                                <asp:TextBox ID="txtOperationNumber" runat="server"></asp:TextBox>
                            </p>
                        </div>
                    </fieldset>
                    <fieldset>
                        <legend>Changes</legend>
                        <p>
                            <asp:RequiredFieldValidator ID="valProposedChange" runat="server" ControlToValidate="txtProposedChange" ErrorMessage="Proposed Change description is required." Text="*"></asp:RequiredFieldValidator>
                            Proposed Change</p>
                        <p>
                            <asp:TextBox ID="txtProposedChange" runat="server" height="100px" TextMode="MultiLine" Width="600px"></asp:TextBox>
                        </p>
                         <p>
                            <asp:RequiredFieldValidator ID="valChangeReason" runat="server" ControlToValidate="txtChangeReason" ErrorMessage="Reason for Proposed Change description is required." Text="*"></asp:RequiredFieldValidator>
                            Reason for Proposed Change</p>
                        <p>
                            <asp:TextBox ID="txtChangeReason" runat="server" height="100px" TextMode="MultiLine" Width="600px"></asp:TextBox>
                        </p>


                     </fieldset>
                    <p>
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
                        <asp:Button ID="btnReset" runat="server" CausesValidation="False" OnClick="btnReset_Click" Text="Reset" />
                    </p>

以下是.cs我目前已经说过,电子邮件将在正文中发送这些当前字段。我需要知道如何将HTML发送的文件作为附件发送到此电子邮件中。有什么想法吗?

protected void Button1_Click(object sender, EventArgs e)
{
        String toAddress = ""; String fromAddress = "doNotReply@companyemail.com";


        SmtpClient smtpClient = new SmtpClient("mail.companyemail.com");

        smtpClient.Port = 25;

        MailMessage message = new MailMessage();
        message.IsBodyHtml = false;
        message.Priority = MailPriority.High;
        message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        try
        {

            message.Subject = "Program Change";
            message.Body = "Part Number: " + txtPartNumber.Text.Trim() + "\n";
            message.Body += "Employee ID: " + txtEmployeeID.Text.Trim() + "\n";
            message.Body += "Current Job Number: " + txtCurrentJobNumber.Text.Trim() + "\n" + "\n";
            message.Body += "Revision Level: " + txtRevisionLevel.Text.Trim() + "\n";
            message.Body += "Operation Number: " + txtOperationNumber.Text.Trim() + "\n";
            message.Body += "Proposed Changes: " + txtProposedChange.Text.Trim() + "\n";
            message.Body += "Reason for Proposed Change: " + txtChangeReason.Text.Trim() + "\n";
            HttpPostedFile file = Request.Files["file"];
            Attachment attachment = new Attachment(file.InputStream, file.FileName);
            message.Attachments.Add(attachment);
            toAddress += "user@companyemail.com";


            smtpClient.Send(fromAddress, toAddress, message.Subject, message.Body);
        }
        catch (Exception ex)
        {
            formPanel.Visible = false;
            errorPanel.Visible = true;


        }
        formPanel.Visible = false;
        errorPanel.Visible = false;
        successPanel.Visible = true;

}

1 个答案:

答案 0 :(得分:0)

从发布的表单中抓取文件。

HttpPostedFile file = Request.Files["file"];

然后,将其添加为附件。

Attachment attachment = new Attachment(file.InputStream, file.FileName);
message.Attachments.Add(attachment);

注意,我还没有测试过这段代码。