Example #1
0
            ///// <summary>
            ///// Редактирование значения
            ///// </summary>
            ///// <param name="val">значение</param>
            ///// <returns>измененное знач.</returns>
            //private string fewerValue(double val)
            //{
            //    return Convert.ToString(val * Math.Pow(10F, 1 * vsRatio));
            //}

            /// <summary>
            /// Создание. Подготовка. Отправка письма.
            /// </summary>
            /// <param name="tableSource">таблица с данными</param>
            /// <param name="dtSend">выбранный промежуток</param>
            /// <param name="to">получатель</param>
            public void SendMailToNSS(IEnumerable <HandlerDbTaskCalculate.PUT_PARAMETER> parameters
                                      , IEnumerable <TepCommon.HandlerDbTaskCalculate.VALUE> values
                                      , Func <int, int, float, int, float> fGetValueAsRatio
                                      , DateTime dtSend
                                      , string to)
            {
                string bodyMsg      = string.Empty
                , sbjct             = string.Empty;
                DateTime reportDate = DateTime.MinValue;

                reportDate = dtSend.AddHours(6).Date;//??
                sbjct      = @"Отчет о выработке электроэнергии НТЭЦ-5 за " + reportDate.ToShortDateString();
                bodyMsg    = createBodyToSend(parameters, values, fGetValueAsRatio, reportDate);

                if (sbjct.Equals(string.Empty) == false)
                {
                    using (OutlookMessage outlookMessage = new OutlookMessage()) {
                        if (outlookMessage.Format(sbjct, bodyMsg, to) == true)
                        {
                            outlookMessage.Send();
                        }
                        else
                        {
                            ;
                        }
                    }
                }
                else
                {
                    ;
                }
            }
            //ExEnd:SourceOutlookFilePath

            /// <summary>
            /// Gets Outlook email file's metadata
            /// </summary>
            public static void GetOutlookEmailMetadata()
            {
                try
                {
                    //ExStart:GetOutlookEmailMessageMetadata
                    // initialize outlookFormat
                    using (OutlookMessage msgFormat = new OutlookMessage(Common.MapSourceFilePath(filePath)))
                    {
                        // get metadata
                        OutlookMessageMetadata metadata = msgFormat.GetMsgInfo();

                        // display metadata
                        Console.WriteLine("Body: " + metadata.Body);
                        Console.WriteLine("DeliveryTime: " + metadata.DeliveryTime);
                        Console.WriteLine("Recipients: " + metadata.Recipients[0]);
                        Console.WriteLine("Subject: " + metadata.Subject);
                        Console.WriteLine("Attachments: " + metadata.Attachments[0]);
                    }
                    //ExEnd:GetOutlookEmailMessageMetadata
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes email metadata
            /// </summary>
            public static void RemoveOutlookEmailMetadata()
            {
                try
                {
                    //ExStart:RemoveOutlookEmailMetadata
                    // initialize outlookFormat 
                    OutlookMessage outlookFormat = new OutlookMessage(Common.MapSourceFilePath(filePath));

                    // remove metadata
                    outlookFormat.CleanMetadata();

                    // commit changes
                    outlookFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveOutlookEmailMetadata
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes Outlook email attachments
            /// </summary>
            public static void RemoveOutlookEmailAttachments()
            {
                try
                {
                    //ExStart:RemoveOutlookEmailAttachments
                    // initialize outlookFormat 
                    OutlookMessage outlookFormat = new OutlookMessage(Common.MapSourceFilePath(filePath));

                    // remove attachments
                    outlookFormat.RemoveAttachments();

                    // commit changes
                    outlookFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveOutlookEmailAttachments
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes email metadata
            /// </summary>
            public static void RemoveOutlookEmailMetadata()
            {
                try
                {
                    //ExStart:RemoveOutlookEmailMetadata
                    // initialize outlookFormat
                    OutlookMessage outlookFormat = new OutlookMessage(Common.MapSourceFilePath(filePath));

                    // remove metadata
                    outlookFormat.CleanMetadata();

                    // commit changes
                    outlookFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveOutlookEmailMetadata
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes Outlook email attachments
            /// </summary>
            public static void RemoveOutlookEmailAttachments()
            {
                try
                {
                    //ExStart:RemoveOutlookEmailAttachments
                    // initialize outlookFormat
                    OutlookMessage outlookFormat = new OutlookMessage(Common.MapSourceFilePath(filePath));

                    // remove attachments
                    outlookFormat.RemoveAttachments();

                    // commit changes
                    outlookFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveOutlookEmailAttachments
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            //ExEnd:SourceOutlookFilePath

            /// <summary>
            /// Gets Outlook email file's metadata
            /// </summary>
            public static void GetOutlookEmailMetadata()
            {
                try
                {
                    //ExStart:GetOutlookEmailMessageMetadata
                    // initialize outlookFormat
                    OutlookMessage msgFormat = new OutlookMessage(Common.MapSourceFilePath(filePath));

                    // get metadata
                    OutlookMessageMetadata metadata = msgFormat.GetMsgInfo();

                    // display metadata
                    Console.WriteLine("Body: " + metadata.Body);
                    Console.WriteLine("DeliveryTime: " + metadata.DeliveryTime);
                    Console.WriteLine("Recipients: " + metadata.Recipients[0]);
                    Console.WriteLine("Subject: " + metadata.Subject);
                    Console.WriteLine("Attachments: " + metadata.Attachments[0]);
                    //ExEnd:GetOutlookEmailMessageMetadata
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
        public JsonResult <List <PropertyItem> > Get(string file)
        {
            try
            {
                FileStream        original          = File.Open(Utils._storagePath + "\\" + file, FileMode.OpenOrCreate);
                FileFormatChecker fileFormatChecker = new FileFormatChecker(original);

                DocumentType        documentType = fileFormatChecker.GetDocumentType();
                List <PropertyItem> values       = new List <PropertyItem>();

                if (fileFormatChecker.VerifyFormat(documentType))
                {
                    switch (documentType)
                    {
                    case DocumentType.Doc:

                        DocFormat docFormat = new DocFormat(original);
                        values = AppendMetadata(docFormat.GetMetadata(), values);

                        break;

                    case DocumentType.Xls:

                        XlsFormat xlsFormat = new XlsFormat(original);
                        values = AppendMetadata(xlsFormat.GetMetadata(), values);

                        break;

                    case DocumentType.Pdf:

                        PdfFormat pdfFormat = new PdfFormat(original);
                        values = AppendMetadata(pdfFormat.GetMetadata(), values);
                        values = AppendXMPData(pdfFormat.GetXmpData(), values);

                        break;

                    case DocumentType.Png:

                        PngFormat pngFormat = new PngFormat(original);
                        values = AppendMetadata(pngFormat.GetMetadata(), values);
                        values = AppendXMPData(pngFormat.GetXmpData(), values);

                        break;

                    case DocumentType.Jpeg:

                        JpegFormat jpegFormat = new JpegFormat(original);
                        values = AppendMetadata(jpegFormat.GetMetadata(), values);
                        values = AppendXMPData(jpegFormat.GetXmpData(), values);

                        break;

                    case DocumentType.Gif:

                        GifFormat gifFormat = new GifFormat(original);
                        values = AppendMetadata(gifFormat.GetMetadata(), values);
                        values = AppendXMPData(gifFormat.GetXmpData(), values);

                        break;

                    case DocumentType.Bmp:

                        BmpFormat bmpFormat = new BmpFormat(original);
                        values = AppendMetadata(bmpFormat.GetMetadata(), values);

                        break;

                    case DocumentType.Msg:

                        OutlookMessage outlookMessage = new OutlookMessage(original);
                        values = AppendMetadata(outlookMessage.GetMsgInfo(), values);
                        break;

                    case DocumentType.Eml:

                        EmlFormat emlFormat = new EmlFormat(original);
                        values = AppendMetadata(emlFormat.GetEmlInfo(), values);
                        break;

                    case DocumentType.Dwg:

                        DwgFormat dwgFormat = new DwgFormat(original);
                        values = AppendMetadata(dwgFormat.GetMetadata(), values);
                        break;

                    case DocumentType.Dxf:

                        DxfFormat dxfFormat = new DxfFormat(original);
                        values = AppendMetadata(dxfFormat.GetMetadata(), values);
                        break;

                    default:

                        DocFormat defaultDocFormat = new DocFormat(original);
                        values = AppendMetadata(defaultDocFormat.GetMetadata(), values);

                        break;
                    }

                    return(Json(values));
                }
                else
                {
                    throw new Exception("File format not supported.");
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        public HttpResponseMessage Get(string file)
        {
            try
            {
                File.Copy(Utils._storagePath + "\\" + file, Utils._storagePath + "\\Cleaned_" + file, true);
                FileStream        original          = File.Open(Utils._storagePath + "\\Cleaned_" + file, FileMode.Open, FileAccess.ReadWrite);
                FileFormatChecker fileFormatChecker = new FileFormatChecker(original);
                DocumentType      documentType      = fileFormatChecker.GetDocumentType();


                if (fileFormatChecker.VerifyFormat(documentType))
                {
                    switch (documentType)
                    {
                    case DocumentType.Doc:

                        DocFormat docFormat = new DocFormat(original);
                        docFormat.CleanMetadata();
                        docFormat.ClearBuiltInProperties();
                        docFormat.ClearComments();
                        docFormat.ClearCustomProperties();
                        docFormat.RemoveHiddenData(new DocInspectionOptions(DocInspectorOptionsEnum.All));

                        docFormat.Save(Utils._storagePath + "\\Cleaned_" + file);
                        break;

                    case DocumentType.Xls:

                        XlsFormat xlsFormat = new XlsFormat(original);
                        xlsFormat.CleanMetadata();
                        xlsFormat.ClearBuiltInProperties();
                        xlsFormat.ClearContentTypeProperties();
                        xlsFormat.ClearCustomProperties();
                        xlsFormat.RemoveHiddenData(new XlsInspectionOptions(XlsInspectorOptionsEnum.All));

                        xlsFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Pdf:

                        PdfFormat pdfFormat = new PdfFormat(original);
                        pdfFormat.CleanMetadata();
                        pdfFormat.ClearBuiltInProperties();
                        pdfFormat.ClearCustomProperties();
                        pdfFormat.RemoveHiddenData(new PdfInspectionOptions(PdfInspectorOptionsEnum.All));
                        pdfFormat.RemoveXmpData();

                        pdfFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Png:

                        PngFormat pngFormat = new PngFormat(original);
                        pngFormat.CleanMetadata();
                        pngFormat.RemoveXmpData();

                        pngFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Jpeg:

                        JpegFormat jpegFormat = new JpegFormat(original);
                        jpegFormat.CleanMetadata();
                        jpegFormat.RemoveExifInfo();
                        jpegFormat.RemoveGpsLocation();
                        jpegFormat.RemoveIptc();
                        jpegFormat.RemovePhotoshopData();
                        jpegFormat.RemoveXmpData();

                        jpegFormat.Save(original);

                        break;

                    case DocumentType.Bmp:

                        BmpFormat bmpFormat = new BmpFormat(original);
                        bmpFormat.CleanMetadata();

                        bmpFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Gif:

                        GifFormat gifFormat = new GifFormat(original);
                        gifFormat.CleanMetadata();
                        gifFormat.RemoveXmpData();

                        gifFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Msg:

                        OutlookMessage outlookMessage = new OutlookMessage(original);
                        outlookMessage.CleanMetadata();
                        outlookMessage.RemoveAttachments();

                        outlookMessage.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Eml:

                        EmlFormat emlFormat = new EmlFormat(original);
                        emlFormat.CleanMetadata();
                        emlFormat.RemoveAttachments();

                        emlFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Dwg:

                        DwgFormat dwgFormat = new DwgFormat(original);
                        dwgFormat.CleanMetadata();

                        dwgFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Dxf:

                        DxfFormat dxfFormat = new DxfFormat(original);
                        dxfFormat.CleanMetadata();

                        dxfFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    default:

                        DocFormat defaultDocFormat = new DocFormat(original);
                        defaultDocFormat.CleanMetadata();
                        defaultDocFormat.ClearBuiltInProperties();
                        defaultDocFormat.ClearComments();
                        defaultDocFormat.ClearCustomProperties();
                        defaultDocFormat.RemoveHiddenData(new DocInspectionOptions(DocInspectorOptionsEnum.All));

                        defaultDocFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;
                    }
                }
                else
                {
                    throw new Exception("File format not supported.");
                }

                using (var ms = new MemoryStream())
                {
                    original = File.OpenRead(Utils._storagePath + "\\Cleaned_" + file);
                    original.CopyTo(ms);
                    var result = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(ms.ToArray())
                    };
                    result.Content.Headers.ContentDisposition =
                        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                    {
                        FileName = "Cleaned_" + file
                    };
                    result.Content.Headers.ContentType =
                        new MediaTypeHeaderValue("application/octet-stream");

                    original.Close();
                    File.Delete(Utils._storagePath + "\\Cleaned_" + file);
                    return(result);
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
Example #10
0
        static void SendOutlookMail(string strSubject, string message, string fullFileName, string txtEmail, string txtEmailcc)
        {
            outlook.MailItem    OutlookMessage;
            outlook.Application AppOutlook = new outlook.Application();
            LogWriter           log        = new LogWriter();

            try
            {
                OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem);
                outlook.Recipients Recipients = OutlookMessage.Recipients;
                //OutlookMessage.From = "*****@*****.**";
                OutlookMessage.Subject    = strSubject;
                OutlookMessage.Body       = message;
                OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML;
                if (!string.IsNullOrWhiteSpace(fullFileName))
                {
                    OutlookMessage.Attachments.Add(fullFileName, outlook.OlAttachmentType.olByValue);
                }

                if (!string.IsNullOrWhiteSpace(txtEmail))
                {
                    string[] arrAddTos = txtEmail.Split(new char[] { ';', ',' });
                    foreach (string strAddr in arrAddTos)
                    {
                        if (!string.IsNullOrWhiteSpace(strAddr) &&
                            strAddr.IndexOf('@') != -1)
                        {
                            Recipients.Add(strAddr.Trim());
                        }
                        else
                        {
                            if ((Utilities.IsBitSet(Program.iLoc, 0)) || (Utilities.IsBitSet(Program.iLoc, 1)))    // Toronto or Montreal
                            {
                                log.LogWrite(Program.torbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Bad to-address: " + strAddr);
                            }

                            if ((Utilities.IsBitSet(Program.iLoc, 2)) || (Utilities.IsBitSet(Program.iLoc, 3)))    // Vancouver or Calgary
                            {
                                log.LogWrite(Program.vanbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Bad to-address: " + strAddr);
                            }
                            // throw new Exception("Bad to-address: " + txtEmail);
                        }
                    }
                }
                else
                {
                    if ((Utilities.IsBitSet(Program.iLoc, 0)) || (Utilities.IsBitSet(Program.iLoc, 1)))    // Toronto or Montreal
                    {
                        log.LogWrite(Program.torbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Must specify to-address.");
                    }

                    if ((Utilities.IsBitSet(Program.iLoc, 2)) || (Utilities.IsBitSet(Program.iLoc, 3)))    // Vancouver or Calgary
                    {
                        log.LogWrite(Program.vanbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Must specify to-address.");
                    }
                }
                //throw new Exception("Must specify to-address");

                //Parse 'sCc'
                if (!string.IsNullOrWhiteSpace(txtEmailcc))
                {
                    string[] arrAddTos = txtEmailcc.Split(new char[] { ';', ',' });
                    foreach (string strAddr in arrAddTos)
                    {
                        if (!string.IsNullOrWhiteSpace(strAddr) &&
                            strAddr.IndexOf('@') != -1)
                        {
                            Recipients.Add(strAddr.Trim());
                        }
                        else
                        {
                            if ((Utilities.IsBitSet(Program.iLoc, 0)) || (Utilities.IsBitSet(Program.iLoc, 1)))    // Toronto or Montreal
                            {
                                log.LogWrite(Program.torbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Bad cc-address: " + strAddr);
                            }

                            if ((Utilities.IsBitSet(Program.iLoc, 2)) || (Utilities.IsBitSet(Program.iLoc, 3)))    // Vancouver or Calgary
                            {
                                log.LogWrite(Program.vanbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Bad cc-address: " + strAddr);
                            }
                        }
                        //throw new Exception("Bad CC-address: " + txtEmailcc);
                    }
                }
                else
                {
                    if ((Utilities.IsBitSet(Program.iLoc, 0)) || (Utilities.IsBitSet(Program.iLoc, 1)))    // Toronto or Montreal
                    {
                        log.LogWrite(Program.torbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Must specify cc-address.");
                    }

                    if ((Utilities.IsBitSet(Program.iLoc, 2)) || (Utilities.IsBitSet(Program.iLoc, 3)))    // Vancouver or Calgary
                    {
                        log.LogWrite(Program.vanbackupPath, Program.txtOutputFile, Environment.UserName + "," + DateTime.Now + "," + fullFileName + "," + "Must specify cc-address.");
                    }
                }
                //throw new Exception("Must specify to-address");

                OutlookMessage.Send();
            }

            catch (Exception ex)
            {
            }

            // MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line
            finally
            {
                OutlookMessage = null /* TODO Change to default(_) if this is not a reference type */;
                AppOutlook     = null /* TODO Change to default(_) if this is not a reference type */;
            }

            return;
        }