Чтение данных из SQL, сохраненных в формате varbinary, и прикрепление файла в электронном письме, отправленном из системы? - PullRequest
0 голосов
/ 29 марта 2019

Я попытался получить файлы из SQL с помощью HttpResponseMessage, и я хочу прикрепить файл как электронное письмо, отправленное системой.

 [Route("GetspecificDocument")]
        [AllowAnonymous]
        public HttpResponseMessage GetspecificDocument(string searchText)
        {
            try
            {
                //DocumentMaster objFileReaderView = iEnquireyRepository.GetDocument(searchText);
                long docid = Convert.ToInt64(searchText);
                DocumentMaster objFileReaderView = coreContext.DocumentMasters.Where(t => t.DocumentId == docid).FirstOrDefault();
                HttpResponseMessage ResponseDownload = new HttpResponseMessage(HttpStatusCode.OK);

                ResponseDownload.Content = new ByteArrayContent(objFileReaderView.DocumentBinData);

                if (objFileReaderView.Type.ToLower().Contains("pdf"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                }
                else if (objFileReaderView.Type.ToLower().Contains("png"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                }
                else if (objFileReaderView.Type.ToLower().Contains("jpg") || objFileReaderView.Type.ToLower().Contains("jpeg"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                }
                else if (objFileReaderView.Type.ToLower().Contains("csv"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/csv");
                }
                else if (objFileReaderView.Type.ToLower().Contains("doc"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");
                }
                else if (objFileReaderView.Type.ToLower().Contains("docx"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                }
                else if (objFileReaderView.Type.ToLower().Contains("xlsx"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                }
                else if (objFileReaderView.Type.ToLower().Contains("xls"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
                }
                else if (objFileReaderView.Type.ToLower().Contains("zip"))
                {
                    ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
                }
                //ResponseDownload.Content.Headers.ContentDisposition.FileName = objFileReaderView.DocumentName;
                String reader = ResponseDownload.Content.ToString();
                return ResponseDownload;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


//Code for email
 public async static Task<bool> SendMail(string toAddress, string[] ccName, string subject, string body)
        {
            try
            {
                string fromAddress = System.Configuration.ConfigurationManager.AppSettings["FromEmailAddr"];

                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(fromAddress);

                msg.To.Add(new MailAddress(toAddress));
                foreach (string ccN in ccName) {
                    msg.CC.Add(new MailAddress(ccN));
                }                
                msg.Subject = subject;
                msg.Body = body;

                //msg.Attachments.Add(attachment);
                msg.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = true;
                smtp.Send(msg);
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }

1 Ответ

0 голосов
/ 29 марта 2019

Вам действительно нужны 2 метода?у вас может быть один метод действия, который получает документ из базы данных и отправляет электронную почту.

public Task SendEmail(RequestObject request)
{
    // get document

   // send email
}

, где RequestObject содержит все параметры, необходимые для запроса документа и отправки электронной почты.

Обновление 1

Не уверен, что это то, что вы ищете

[Route("GetspecificDocument")]
[AllowAnonymous]
public HttpResponseMessage GetspecificDocument(string searchText)
{
    try
    {
        //DocumentMaster objFileReaderView = iEnquireyRepository.GetDocument(searchText);

        HttpResponseMessage ResponseDownload = new HttpResponseMessage(HttpStatusCode.OK);

        ResponseDownload.Content = GetDocument(searchText);

        if (objFileReaderView.Type.ToLower().Contains("pdf"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        }
        else if (objFileReaderView.Type.ToLower().Contains("png"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        }
        else if (objFileReaderView.Type.ToLower().Contains("jpg") || objFileReaderView.Type.ToLower().Contains("jpeg"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
        }
        else if (objFileReaderView.Type.ToLower().Contains("csv"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/csv");
        }
        else if (objFileReaderView.Type.ToLower().Contains("doc"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");
        }
        else if (objFileReaderView.Type.ToLower().Contains("docx"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        }
        else if (objFileReaderView.Type.ToLower().Contains("xlsx"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }
        else if (objFileReaderView.Type.ToLower().Contains("xls"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
        }
        else if (objFileReaderView.Type.ToLower().Contains("zip"))
        {
            ResponseDownload.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        }
        //ResponseDownload.Content.Headers.ContentDisposition.FileName = objFileReaderView.DocumentName;
        String reader = ResponseDownload.Content.ToString();
        return ResponseDownload;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}


//Code for email
public async static Task<bool> SendMail(string toAddress, string[] ccName, string subject, string body, string searchText)
{
    try
    {

        string fromAddress = System.Configuration.ConfigurationManager.AppSettings["FromEmailAddr"];

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(fromAddress);

        msg.To.Add(new MailAddress(toAddress));
        foreach (string ccN in ccName)
        {
            msg.CC.Add(new MailAddress(ccN));
        }
        msg.Subject = subject;
        msg.Body = body;

        if(!string.IsNullOrEmpty(searchText))
        {
            var doc = GetDocument(searchText);

            var attachment = BuildAttachmentFromBlob(doc);

            msg.Attachments.Add(attachment);
        }


        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.EnableSsl = true;
        smtp.Send(msg);
        return true;
    }
    catch (Exception ex)
    {

        return false;
    }
}


private ByteArrayContent GetDocument(string searchText)
{
    long docid = Convert.ToInt64(searchText);
    DocumentMaster objFileReaderView = coreContext.DocumentMasters.Where(t => t.DocumentId == docid).FirstOrDefault();

    new ByteArrayContent(objFileReaderView.DocumentBinData)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...