我想發送電子郵件中的附件,因此我想先將附件文件保存在文件夾中。如何在Windows應用程序中的文件夾中保存文件
那麼我現在的代碼發生了什麼事情,郵件正在附件中,但我的文件沒有保存到
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();
}
爲什麼你的文件應該保存在附件文件夾中?你如何告訴你的ExcelLibrary在該文件夾中創建文件? – Steve
@Steve:我不知道如何在文件夾中創建該文件?任何幫助 – learner
你不需要SaveFile對話框來做到這一點(並在你的代碼中定義它,但你沒有使用它)。只需使用[File.Copy](https://msdn.microsoft.com/en-us/library/system.io.file.copy(v = vs.110).aspx) – Pikoh