EmailHelper

 using System;

using System.Web;

using System.Web.Mvc;

using System.Collections;

using System.Data;

using System.Collections.Specialized;

using InsiderTrading.Infrastructure.Common;

using InsiderTrading.DataObject;

using System.Data.SqlClient;

using InsiderTrading.Infrastructure.Enums.Emails;

using System.IO;

using System.IO.Compression;

using System.IO.Packaging;

using System.Collections.Generic;

using InsiderTrading.Models;


namespace InsiderTrading.Infrastructure.Helpers

{

    public static class EmailHelper

    {

        #region  Variable

        private static string _SMTPClient = string.Empty;

        private static string _BCCEmail = string.Empty;

        private static string _defaultMailFrom = string.Empty;

        private static string _conString = CommonFunction.GetConnectionString();

        private static string _uploadPath = CommonFunction.GetUploadPath();

        #endregion


        #region   Constructor 


        /// <summary>

        /// Initializes a new instance of the <see cref="ExceptionHelper"/> class.

        /// </summary>

        static EmailHelper()

        {

            _SMTPClient = ConfigurationHelper.GetFWConfiguration("MIND.Web.Application.Dafault.SMTPSERVER");

            _BCCEmail = ConfigurationHelper.GetFWConfiguration("MIND.Web.Application.Dafault.BCCEmail");

            _defaultMailFrom = ConfigurationHelper.GetFWConfiguration("MIND.Web.Application.Dafault.MailFrom");

        }


        #endregion


        #region Public Static Methods


        /// <summary>

        /// Method For Send Email 

        /// </summary>

        /// 


        public static void SendEmail

            (

                string emailType, string mailTo, string subject, string msgBody, string mailId,

                bool isAttachment = false, string ccEmail = null, List<DocumentDetails> docsData = null,

                List<string> documentIds = null, string userName = null

            )

        {

            try

            {

                string attachmentPath = string.Empty;

                int Msg;


                if ((docsData != null && docsData.Count > 0) || (documentIds != null && documentIds.Count > 0))

                {

                    byte[] docData = null;

                    string docType = string.Empty;

                    string docName = string.Empty;

                    Guid SaveDocName;

                    List<string> docPaths = new List<string>();

                    List<byte[]> lstDocData = new List<byte[]>();


                    if (docsData == null)

                    {

                        docsData = new List<DocumentDetails>();

                    }


                    if (documentIds != null && documentIds.Count > 0)

                    {

                        foreach (string docId in documentIds)

                        {

                            SaveDocName = new Guid();

                            docPaths.Add(Convert.ToString(SaveDocName));

                            docData = CommonFunction.GetDocumentData(docId, ref docName, ref docType);

                            if (docData != null)

                            {

                                DocumentDetails doc = new DocumentDetails();

                                doc.DocumentData = docData;

                                doc.DocumentId = Guid.Parse(docId);

                                doc.DocumentName = docName;

                                docsData.Add(doc);

                            }

                        }

                    }

                    attachmentPath = ConvertToZip(docsData, mailId);

                }


                using (SqlHelper sqlHelper = new SqlHelper(_conString))

                {

                    SqlParameter[] oParams = new SqlParameter[11];

                    oParams[0] = new SqlParameter("@p_MailID_To", mailTo);

                    if (isAttachment)

                    {

                        oParams[1] = new SqlParameter("@p_Attachment", attachmentPath);

                    }

                    else

                    {

                        oParams[1] = new SqlParameter("@p_Attachment", "");

                    }

                    if (string.IsNullOrEmpty(ccEmail))

                    {

                        ccEmail = string.Empty;

                    }

                    oParams[2] = new SqlParameter("@p_Bcc_ID", _BCCEmail);

                    oParams[3] = new SqlParameter("@p_CC_ID", ccEmail);

                    oParams[4] = new SqlParameter("@p_ReplyTo_ID", "");

                    oParams[5] = new SqlParameter("@p_BodyText", msgBody);

                    oParams[6] = new SqlParameter("@p_From_ID", _defaultMailFrom);

                    oParams[7] = new SqlParameter("@p_SourcePCName", "SQL Batch");

                    oParams[8] = new SqlParameter("@p_Subject", subject);

                    oParams[9] = new SqlParameter("@p_EmailType", emailType);

                    oParams[10] = new SqlParameter("@p_UserName", string.IsNullOrEmpty(userName) ? string.Empty : userName);

                    Msg = sqlHelper.ExecuteNonQuery("Proc_IT_Send_Email", ref oParams);

                }

            }

            catch (Exception ex)

            {

                throw (ex);

            }

        }


        //public static void SendEmail(string emailType, string MailSendFrom, string mailTo, string subject, string msgBody, bool isAttachment = false, string ccEmail = null, string attachmentPath = null)

        //{

        //    try

        //    {

        //        int Msg;

        //        using (SqlHelper sqlHelper = new SqlHelper(_conString))

        //        {

        //            SqlParameter[] oParams = new SqlParameter[9];

        //            oParams[0] = new SqlParameter("@p_MailID_To", mailTo);

        //            if (isAttachment)

        //            {

        //                oParams[1] = new SqlParameter("@p_Attachment", HttpContext.Current.Server.MapPath(attachmentPath));

        //            }

        //            else

        //            {

        //                oParams[1] = new SqlParameter("@p_Attachment", "");

        //            }

        //            if (string.IsNullOrEmpty(ccEmail))

        //            {

        //                ccEmail = string.Empty;

        //            }

        //            oParams[2] = new SqlParameter("@p_Bcc_ID", _BCCEmail);

        //            oParams[3] = new SqlParameter("@p_CC_ID", ccEmail);

        //            oParams[4] = new SqlParameter("@p_ReplyTo_ID", "");

        //            oParams[5] = new SqlParameter("@p_BodyText", msgBody);

        //            oParams[6] = new SqlParameter("@p_From_ID", MailSendFrom);

        //            oParams[7] = new SqlParameter("@p_SourcePCName", "SQL Batch");

        //            oParams[8] = new SqlParameter("@p_Subject", subject);

        //            oParams[9] = new SqlParameter("@p_EmailType", emailType);

        //            Msg = sqlHelper.ExecuteNonQuery("Proc_IT_Send_Email", ref oParams);

        //        }

        //    }

        //    catch (Exception ex)

        //    {

        //        throw (ex);

        //    }

        //}


        public static string ConvertToZip(List<DocumentDetails> lstDocList, string mailId)

        {

            string strAttachmentUploadPath = CommonFunction.GetFunctionControlKeyValue("ALERT_EMAIL_DOCUMENT_UPLOAD_PATH");

            string strAttachmentAccessPath = CommonFunction.GetFunctionControlKeyValue("ALERT_EMAIL_DOCUMENT_ACCESS_PATH");

            string strAccessPath = String.Format("{0}{1}{2}", strAttachmentAccessPath, mailId, ".zip");

            string zipPath = String.Format("{0}{1}{2}", strAttachmentUploadPath, mailId, ".zip");

            ///string alertEmailAttachmentZipUploadPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AlertEmailAttachmentZip/");


            //if (!System.IO.Directory.Exists(fileFolder))

            //{

            //    System.IO.Directory.CreateDirectory(fileFolder);

            //}

            ////fileFolder = "~/Template/TempDocs";

            //fname = Path.Combine(fileFolder, Convert.ToString(file.FileName).Trim());

            //if ((System.IO.File.Exists(fname)))

            //{

            //    System.IO.File.Delete(fname);

            //}

            //file.SaveAs(fname);




            using (MemoryStream zipMS = new MemoryStream())

            {

                using (ZipArchive zipArchive = new ZipArchive(zipMS, ZipArchiveMode.Create, true))

                {

                    foreach (DocumentDetails docData in lstDocList)

                    {

                        ZipArchiveEntry zipFileEntry = zipArchive.CreateEntry(docData.DocumentName);


                        using (Stream zipEntryStream = zipFileEntry.Open())

                        using (BinaryWriter zipFileBinary = new BinaryWriter(zipEntryStream))

                        {

                            zipFileBinary.Write(docData.DocumentData);

                        }

                    }

                }


                using (FileStream finalzipfilestream = new FileStream(zipPath, FileMode.Create))

                {

                    zipMS.Seek(0, SeekOrigin.Begin);

                    zipMS.CopyTo(finalzipfilestream);

                }


                ////////////////

                // string zipPath1 = String.Format("{0}{1}{2}", "Y:/common/insider-Documents/", mailId, ".zip");

                // string zipPath1 = String.Format("{0}{1}{2}", _uploadPath, mailId, ".zip");

                //  byte[] bytes = zipMS.ToArray();

                //System.IO.File.WriteAllBytes(zipPath1, bytes);

                /// System.IO.File.WriteAllBytes(context + "/" + legl_entity_id + "_" + employeeCode + "_" + policyid + "_" + empuid + "_" + DateTime.Now.ToString("ddMMyyyy") + ".pdf", bytes);

                ///filename = legl_entity_id + "_" + employeeCode + "_" + policyid + "_" + empuid + "_" + DateTime.Now.ToString("ddMMyyyy") + ".pdf";


                ///////////////


            }





            return strAccessPath;

        }

    }

    #endregion

}







Comments

Popular posts from this blog

Npgsql query and format with output parameters

Npgsql Helper class