【问题标题】:file attachment form c#文件附件表单c#
【发布时间】:2011-05-02 18:16:12
【问题描述】:

我正在创建一个网络表单,要求用户上传 doc、docx 或 pdf 格式的文件。 提交时,表单连同附件一起发布到电子邮件地址。我已经成功地将表单发布到电子邮件地址,但不知道如何附加文件。请帮忙。

public void ProcessRequest(HttpContext context)
{
    string template = context.Request["template"];

    string responseHtml = BuiltTemplateHtml(context.Request, template, "response", false);
    string reuestEmailHtml = BuiltTemplateHtml(context.Request, template, "request_email", false);
    string contactEmail = GetTagsInnerText(reuestEmailHtml, "to", 0);
    string contactName = GetTagsInnerText(reuestEmailHtml, "toname", 0);

    string responEmailHtml = BuiltTemplateHtml(context.Request, template, "response_email", true, "contactName", contactName, "contactEmail", contactEmail);

    sendEmail(reuestEmailHtml);
    sendEmail(responEmailHtml);
    context.Response.ContentType = "text/html";
    context.Response.Write(responseHtml);

    SaveAttachments(context, reuestEmailHtml);

}

private void SaveAttachments(HttpContext context, string settingFile)
{
    if (context.Request.Files.Count > 0)
    {
        string fileNameformat = GetTagsInnerText(settingFile, "fileNameformat", 0);
        string[] savefiles = GetTagsInnerText(settingFile, "savefiles", 0).Split('|', ',');
        string[] allowextensions = GetTagsInnerText(settingFile, "allowextensions", 0).Split('|', ',');                
        string path = cleanPath(fileNameformat);

        MailMessage mail = new MailMessage();

        // attachment code here        
    }
}

【问题讨论】:

    标签: c# asp.net email mailmessage mail-form


    【解决方案1】:

    MailMessage 类有一个Attachments 属性,可用于向邮件添加附件。

    【讨论】:

      【解决方案2】:

      看看这个,

      // Create  the file attachment for this e-mail message.
      Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
      
      // Add time stamp information for the file.
      ContentDisposition disposition = data.ContentDisposition;
      disposition.CreationDate = System.IO.File.GetCreationTime(file);
      disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
      disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
      
      // Add the file attachment to this e-mail message.
      message.Attachments.Add(data);
      

      并在 msdn 上查看此链接以获取更多信息, http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-19
        相关资源
        最近更新 更多