0

I'm developing an ASP.Net MVC 3 web application. One of my Razor Views allows a user to upload a file (selected from their computer) and then this is attached to an outgoing email.

Below is my code to date, but unfortunately it does not work. By this I mean the email (and of course the attachment) never gets sent. Although, when I step threw/ debug my code locally no errors occur, and none as well on the live server.

Does anyone see what I'm missing? Any advice would be greatly appreciated.

Thanks.

Controller

    [HttpPost]
    public ActionResult CvUpload(HttpPostedFileBase file)
    {
        //Get logged in user
        User user = _accountService.GetUser(_formsAuthService.GetLoggedInUserID());

        if (file != null && file.ContentLength > 0)
        {
                _emailService.SendUpload(user, file);

                return RedirectToAction("CvUpload", new { feedBack = "Success" });

        }
        return RedirectToAction("CvUpload", new { feedBack = "Failed" });
    }

Email Service

public void SendUpload(User user, HttpPostedFileBase file)
        {
            string messageBody = "";
            string subject = "Upload";
            string[] toAddress = new string[1];
            string ToBCC = "";

            if (isProduction.Equals("true"))
            {
                toAddress[0] = "myemail@test.com";
                sendEmail(toAddress, ToBCC, adminFromEmail, adminFromEmail, subject, messageBody, true, emailServer, file);
            }
            else
            {
                // DO NOT SEND EMAIL
            }

        }

private bool sendEmail(string[] toAddresses, string ToBCC, string fromAddress, string replyto, string subject, string body, bool ishtml, string emailHost, HttpPostedFileBase file)
    {
        bool mailSent = false;
        try
        {
            MailMessage mail = new MailMessage();
            foreach (string addresss in toAddresses)
                mail.To.Add(addresss);

            mail.From = new MailAddress(fromAddress);
            mail.ReplyToList.Add(replyto);
            mail.Bcc.Add(ToBCC);
            mail.Subject = subject;
            mail.Body = body;

            if(file != null && file.ContentLength > 0)
            {
                string fileName = Path.GetFileName(file.FileName);
                mail.Attachments.Add(new Attachment(file.InputStream, fileName));
            }

            if (ishtml)
                mail.IsBodyHtml = true;
            else
                mail.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = emailHost;
            smtp.Send(mail);
            mailSent = true;
        }
        catch (Exception)
        {
            mailSent = false;
        }

        return mailSent;
    }
tcode
  • 5,055
  • 19
  • 65
  • 124

1 Answers1

0

I have a similar service and this is what I have although the attachments are saved so they can be reused later.

            var attachment = new Attachment(path);
            ContentDisposition disposition = attachment.ContentDisposition;
            disposition.CreationDate = File.GetCreationTime(path);
            disposition.ModificationDate = File.GetLastWriteTime(path);
            disposition.ReadDate = File.GetLastAccessTime(path);
            disposition.FileName = attachmentName.Name;
            disposition.Size = new FileInfo(path).Length;
            disposition.DispositionType = DispositionTypeNames.Attachment;
            mailMessage.Attachments.Add(attachment);
Judge Bread
  • 501
  • 1
  • 4
  • 13
  • Thanks for your help, although I'm trying to see if it's possible to attach the upload without saving it first. Not sure if it's possible though. – tcode Mar 26 '15 at 08:55
  • As my boss says... [anything is possible](http://stackoverflow.com/questions/527214/how-do-i-add-an-attachment-to-an-email-using-system-net-mail) or [google] (https://www.google.com/search?q=c%23+create+attachment+from+stream&ie=utf-8&oe=utf-8) :) – Judge Bread Mar 26 '15 at 18:47
  • [This](http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-to-byte) might help too although I think you got the gist of it. – Judge Bread Mar 26 '15 at 18:52