文件附件形式c#
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MailMessage 类有一个 Attachments 属性,可用于将附件添加到信息。
The MailMessage class has an Attachments property which could be used to add attachments to the message.
检查一下,
并检查 msdn 上的此链接以获取更多信息,
http://msdn.microsoft.com/ en-us/library/system.net.mail.mailmessage.attachments.aspx
Check this out,
and check this link on msdn for more information,
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx