ASP.Net MVC文件上传和附加到电子邮件

时间:2015-03-25 16:47:45

标签: asp.net-mvc file-upload email-attachments mailmessage

我正在开发一个ASP.Net MVC 3 Web应用程序。我的一个Razor Views允许用户上传文件(从他们的计算机中选择),然后将其附加到外发电子邮件中。

以下是我迄今为止的代码,但遗憾的是它不起作用。我的意思是电子邮件(当然还有附件)永远不会被发送。虽然,当我在本地执行/调试我的代码时,没有错误发生,并且在实时服务器上也没有错误。

有人看到我失踪了吗?任何建议都将不胜感激。

感谢。

控制器

    [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" });
    }

电子邮件服务

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

1 个答案:

答案 0 :(得分:0)

我有类似的服务,这就是我所拥有的,虽然附件已保存,以便以后可以重复使用。

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