以表格asp.net

时间:2016-10-18 14:34:58

标签: c# asp.net forms attachment

我有一个表单,通过电子邮件将信息发送到工作帐户,但我不知道如何允许用户附加文档并通过联系表单发送。

<p>
    Your name:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
        ControlToValidate="YourName" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
    Your email address:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
        ControlToValidate="YourEmail" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
    <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
        SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
        ValidationGroup="save" /><br />
    Subject:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
        ControlToValidate="YourSubject" ValidationGroup="save" /><br />
    <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
    Your Question:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
        ControlToValidate="Comments" ValidationGroup="save" /><br />
    <asp:TextBox ID="Comments" runat="server" 
            TextMode="MultiLine" Rows="10" Width="400px" /><br />
    Upload a file:
    <input type="file" name="attachment" id="attachment" />
</p>
<p>
    <asp:Button ID="btnSubmit" runat="server" Text="Send" 
                OnClick="Button1_Click" ValidationGroup="save" />
</p>

这是我的代码:

protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "";
        // any address where the email will be sending
        var toAddress = "";
        //Password of your gmail address
        const string fromPassword = "";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp-mail.outlook.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

其他人可以吗?

1 个答案:

答案 0 :(得分:0)

这是我用来发送带附件的邮件的方法。它与你的类似,但我构建了一个MailMessage对象,它有一个Attachments集合,可以将文件添加到...如果你想让用户上传文件,你需要添加代码将它们保存到云端或到您的服务器或可以访问它们的地方。

public void EmailRO(string recipient, string attachmentPath)
{

        var SMTP = new SmtpClient
        {
            Host = "YourHost",
            Port = 12345, //your port
            EnableSsl = true, // or false
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("YourUserName", "YourPassword")
        };

        Thread T1 = new Thread(delegate()
        {
            try 
            {
                using (var message = new MailMessage("YourSmtpUserName", recipient)
                {
                    Subject = "My Subject",
                    Body = "My Body",
                    From = new MailAddress("YourUserName", "YourDisplayName"),
                    IsBodyHtml = true
                })
                {
                    {
                        message.Attachments.Add(new Attachment("YourAttachmentPath"));
                        SMTP.Send(message);
                    }
                }
            }
            catch (ArgumentException)
            {
                // handle exception
            }
        });
        T1.IsBackground = true;
        T1.Start();

}