-2
在我的申请,我有一个用于下载的Excel文件按钮..如何保存在同一个按钮功能的文件后,发送电子邮件在C#中点击一个按钮
OnButtonClick代码:
protected void btnmacdesc_Click(object sender, EventArgs e)
{
if (fileName_macdesc.Length > 0)
{
fileName_macdesc.Remove(0, fileName_macdesc.Length);
}
fileName_macdesc = "daily_macdesc_" + DateTime.Now.ToString("ddMMMyyyyhhmm") + ".xls";
try
{
DataSet ds_macdesc = (DataSet)Session["sessionmachinedesc_ds"];
gv.DataSource = ds_macdesc.Tables[0];
gv.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName_macdesc));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
gv.GridLines = GridLines.Both;
table.GridLines = gv.GridLines;
if (gv.HeaderRow != null)
{
PrepareGridViewForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
for (int k = 0; k < ds_macdesc.Tables[0].Columns.Count; k++)
{
table.Rows[0].Cells[k].BackColor = gv.HeaderStyle.BackColor;
table.Rows[0].Cells[k].ForeColor = gv.HeaderStyle.ForeColor;
table.Rows[0].Cells[k].Font.Bold = true;
}
}
foreach (GridViewRow row in gv.Rows)
{
PrepareGridViewForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
PrepareGridViewForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
bool altColor = false;
for (int i = 1; i < table.Rows.Count; i++)
{
if (!altColor)
{
for (int kl = 0; kl < ds_macdesc.Tables[0].Columns.Count; kl++)
{
table.Rows[i].Cells[kl].BackColor = gv.RowStyle.BackColor;
}
altColor = true;
}
else
{
for (int lk = 0; lk < ds_macdesc.Tables[0].Columns.Count; lk++)
{
table.Rows[i].Cells[lk].BackColor = gv.AlternatingRowStyle.BackColor;
}
altColor = false;
}
}
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
//HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
// send_mail_daily(fileName_macdesc);
}
}
}
catch (Exception ex)
{
CreateLogFiles Err = new CreateLogFiles();
Err.ErrorLog(Server.MapPath("../Logs/ErrorLog"), ex.Message, "Admin_admin_dailyreport==>btnmacdesc_Click");
}
}
我可以下载excel文件没有任何问题。现在,他们要求我将该下载的文件作为电子邮件中的附件发送到同一按钮上。是否有可能这样做?
所以我试图在按钮事件的最后一行调用函数(send_mail_daily),但我无法附加它。在执行最后一行后显示“另存为”对话框选项。请指导我解决这个问题或者是否有任何其他备用解决方案。
功能代码:
public void send_mail_daily(string filename)
{
MailMessage mail = new MailMessage();
mail.To = "[email protected]";
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment(Server.MapPath(filename.ToString())); //create the attachment
mail.Attachments.Add(attachment); //add the attachment
SmtpMail.SmtpServer = "abc.in.def.LOCAL"; //your real server goes here
SmtpMail.Send(mail);
}
@schreidingers:执行最后一行后显示“另存为”对话框选项。如果我在它自己之前指定文件路径显示路径错误 – navbingo 2012-01-11 09:51:53