开发者

file attachment form c#

开发者 https://www.devze.com 2023-01-22 11:32 出处:网络
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 implem

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 he开发者_Go百科re        
    }
}


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消