文件附件形式c#

发布于 2024-09-29 16:22:50 字数 1495 浏览 6 评论 0原文

我正在创建一个 Web 表单,要求用户上传 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        
    }
}

I'm creating a web form that requires an user to upload a file in doc, docx or pdf format.
On submit, the form gets posted to an email address along with the attached file. I have successfully implemented the posting of the form to the email address but don't know how to attach the file with it. Please help.

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        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-10-06 16:22:50

MailMessage 类有一个 Attachments 属性,可用于将附件添加到信息。

The MailMessage class has an Attachments property which could be used to add attachments to the message.

双手揣兜 2024-10-06 16:22:50

检查一下,

// 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

Check this out,

// 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);

and check this link on msdn for more information,
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文