即时创建一个Web表单,要求用户以doc,docxorpdf格式上传文件 在提交表单时,会将该表单连同附件一起发布到电子邮件地址,我已成功将表单发布到电子邮件地址,但不知道如何附上该文件... plz 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
}
}
答案 0 :(得分:3)
MailMessage类具有Attachments属性,可用于向邮件添加附件。
答案 1 :(得分: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