我想通过电子邮件发送附件,因此,我想先将附件文件保存在文件夹中。
所以我当前的代码正在发生的事情是,该邮件带有附件,但是我的文件没有保存到该
ATTACHMENT
文件夹中。
这是我尝试过的代码
for (int i = 0; i < table.Rows.Count; i++)
{
if (1 == 1)
{
string StrPriBody = "This is a test mail for checking notification.";
MailMessage mail = new MailMessage();
mail.Subject = "Daily Holding followup scheduler";
mail.From = new System.Net.Mail.MailAddress("[email protected]");
SmtpClient smtp = new SmtpClient();
smtp.Timeout = 1000000;
smtp.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
smtp.UseDefaultCredentials = true;
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"];
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "PS123456");
smtp.EnableSsl = true;
mail.To.Add(new MailAddress("[email protected]"));
mail.IsBodyHtml = true;
mail.Body = StrPriBody;
DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Tables[0].TableName = "Post sale follow up entries auto mailer";
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "\\Attachment";
save.RestoreDirectory = true;
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
mail.Attachments.Add(new Attachment("Employee_Details.xls"));
smtp.Send(mail);
foreach (Attachment attachments in mail.Attachments)
{
attachments.Dispose();
}
您不需要SaveFileDialog来执行此操作(并且在代码中定义了它,但没有使用它)。只需使用File.Copy即可。因此,在此处创建工作簿后:
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
只需添加如下内容:
File.Copy("Employee_Details.xls","\\Attachment\Employee_Details.xls");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句