You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 21:40:55