learner
learner

Reputation: 37

How to save file in a folder in windows application

I want to send an attachment in an email, SO for that I want to save the attachment file in a folder first.

So what's happening with my current code is, the mail is going with attachment but my file is not getting saved into the ATTACHMENT folder.

here is the code i tried

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();
                }

Upvotes: 1

Views: 1290

Answers (2)

Dmitry
Dmitry

Reputation: 16825

1) Why you are using SaveFileDialog? User must choose folder and file name to save to? Then you forgot to "display" dialog, and after user close it - retrieve chosen folder/file name from it and (preferably) use full path (C:\Folder\File...) for CreateWorkbook and new Attachment(...).

2) Are you sure that after call to ExcelLibrary.DataSetHelper.CreateWorkbook this file is actually written to disk with content? May be you need to call some Save() methods? (this is library-specific, read in library docs)

3) Are you sure that after call to ExcelLibrary.DataSetHelper.CreateWorkbook new file is closed/unlocked by writing code? May be you need to Dispose something? (again, check library docs)

4) Are you testing on server (website?) or desktop machine? Check that you can write access to folder where you want to store your file.

Upvotes: 0

Pikoh
Pikoh

Reputation: 7713

You don't need a SaveFileDialog to do that (and in your code you define it but you are not using it). Just use File.Copy. So, after creating your Workbook here:

ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);

Just add something like:

File.Copy("Employee_Details.xls","\\Attachment\Employee_Details.xls");

Upvotes: 1

Related Questions