点击Link按钮时,如何将.msg草稿文件从服务器保存到客户端?
需求:点击链接按钮将服务器上的.msg邮件草稿文件下载到客户端
前端ButtonField代码
<asp:ButtonField ButtonType="Link" Text="邮件草稿" CommandName="SendDraft" HeaderText="" HeaderStyle-CssClass="gridview-header" > </asp:ButtonField>
(注:原代码中Text为"Email Draft",可根据业务需求改为中文"邮件草稿")
后台生成.msg文件并尝试下载的C#代码
if (e.CommandName == "SendDraft") { int index = Convert.ToInt32(e.CommandArgument); string JROPID = gvReleaseOrderEmail.Rows[index].Cells[1].Text.Trim(); string ReleaseOrderNo = gvReleaseOrderEmail.Rows[index].Cells[2].Text.Trim(); DataTable Report = Utilities.GetDataTable2("exec SP_JobEstReleaseOrderPress '" + Session["Compcode"].ToString().Trim() + "','" + JROPID.Trim() + "'"); ReportDocument crystalReport; crystalReport = new ReportDocument(); crystalReport.Load(Server.MapPath(@"~\Modules\JobOrder\Reports\JobEstReleaseOrderPress.rpt")); crystalReport.SetDataSource(Report); DataRow dr = Report.Rows[0]; ExportOptions exportOptions = new ExportOptions(); DiskFileDestinationOptions diskFileDestinationOptions = new DiskFileDestinationOptions(); string pdfFilePath = Server.MapPath(@"~/Modules/JobOrder/Reports/" + ReleaseOrderNo.Trim() + ".pdf"); diskFileDestinationOptions.DiskFileName = pdfFilePath; exportOptions.ExportDestinationType = ExportDestinationType.DiskFile; exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; exportOptions.ExportDestinationOptions = diskFileDestinationOptions; crystalReport.Export(exportOptions); crystalReport.Close(); crystalReport.Dispose(); string toEmail = gvReleaseOrderEmail.Rows[index].Cells[9].Text.Trim(); // 假设Email字段位于索引9的单元格 string subject = dr["EmailSubject"].ToString(); string Body = dr["Body"].ToString(); string ccEmail = dr["cc"].ToString(); string attachmentPath = pdfFilePath; // 附件文件路径 try { var senders = new Sender("Nadeem", dr["MailFrom"].ToString()); var msg = new Email(senders, subject); msg.Recipients.AddTo(toEmail); msg.Recipients.AddCc(ccEmail); msg.Subject = subject; msg.BodyHtml = Body; msg.Attachments.Add(attachmentPath); string msgFilePath = Server.MapPath("~/Modules/JobOrder/Reports//" + ReleaseOrderNo.ToString().Trim() + ".msg"); if (File.Exists(msgFilePath)) { File.Delete(msgFilePath); } msg.Save(msgFilePath); msg.Dispose(); if (File.Exists(attachmentPath)) { File.Delete(attachmentPath); } string script = "window.open('" + ResolveUrl("~/Modules/JobOrder/Reports/" + ReleaseOrderNo.Trim() + ".msg") + "', '_blank');"; ScriptManager.RegisterStartupScript(this, GetType(), "DownloadScript", script, true); } catch (System.Exception ex) { Logger.LogError("发生错误: " + ex.Message); } } }
内容的提问来源于stack exchange,提问作者Mohammad Talha
相关产品推荐
相关产品推荐

