2

I want to download an excel file and automatically send the file to a mailadress aswell. Downloading the file works, but when I include the code for sending mails, it throws a FileNotFoundException: 'Could not find file 'C:\Users\User\source\repos\Project\Taijitan\Taijitan\O soto gari.xlsx'.'.

I noticed that when downloading the file, it automatically goes to the Downloads folder on my pc. When I uncomment the mail code, it doesn't seem to download, instead it just loads before throwing the error I mentioned above. I tried using Path.GetFullPath(name) to find the excel file so that I don't have to hardcode any paths (in case any of my colleagues have a different path), but it doesn't seem to work.

Controller

 public IActionResult GenerateList(int id)
    {
        Lesson lesson = _lessonRepository.GetBy(id);
        var download = lesson.GetCommentsList();
        string file = lesson.Name + ".xlsx";
        MailSender.SendListMail(lesson.Name, file);
        return download;
    }

Generate excel + download

    public List<Comment> Comments { get; set; }
    public IActionResult GetCommentsList()
    {
        List<string> list = new List<string>();
        var wb = new XLWorkbook();
        var ws = wb.Worksheets.Add("Comments");
        foreach (var comment in Comments)
        {
            list.Add(comment.Body);
        }

        string name = Name + "comment";
        ws.Cell(1, 1).Value = name;
        ws.Range(1, 1, 1, 1).AddToNamed("Titles");
        var CommentsRange = ws.Cell(2, 1).InsertData(list.AsEnumerable());
        var titlesStyle = wb.Style;
        titlesStyle.Font.Bold = true;
        titlesStyle.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
        titlesStyle.Fill.BackgroundColor = XLColor.Blue;
        titlesStyle.Font.FontColor = XLColor.White;
        wb.NamedRanges.NamedRange("Titles").Ranges.Style = titlesStyle;
        ws.Columns().AdjustToContents();
        wb.SaveAs(name + ".xlsx");

        using (var stream = new MemoryStream())
        {

            wb.SaveAs(stream);
            stream.Flush();
            return new FileContentResult(stream.ToArray(),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = name + ".xlsx"
            };
        }
    }

MailSender

 public static void SendListMail(string list, string file)
    {
        MailAddress Sender = new MailAddress("SenderEmail@gmail.com", "CommentsSender");
        MailAddress Receiver = new MailAddress("ReceiverEmail@gmail.com", "Comments inbox");
        const string SenderPassword = "password123";

        string Attachment = Path.GetFullPath(file);

        SmtpClient smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(Sender.Address, SenderPassword),
            Timeout = 30000
        };
        using (MailMessage message = new MailMessage(Sender, Receiver)
        {
            Subject = list,
            Body = "List in attachment",
            Attachments = {new Attachment(Attachment)}

        })
        {
            smtp.Send(message);
        }
    }

I'm trying to figure out where it goes wrong and how I can solve this. My guess is it has something to do with synchronization (don't send the mail until the download is complete) and Path.GetFullPath() not checking outside the solutionfolder.

EDIT Just noticed that when I try to send an email without the attachment, it sends the mail, but doesn't download anything, and the loading icon in the tab keeps spinning.

EDIT 2 Figured out the first problem, the placement of the return statement in the download method caused a loop. Now I'm still having the could not find file error.

iCV
  • 547
  • 1
  • 8
  • 29

1 Answers1

0

Problem turned out to be a dumb mistake. File got saved with the value of 'name' (file.Name + "comment.xlsx", but the download/email sender was looking for file.Name + ".xlsx" (didn't have "comment" in its name)

iCV
  • 547
  • 1
  • 8
  • 29