Beispiel #1
0
        /// <summary>
        /// send a new mail
        /// </summary>
        /// <param name="body"> the body of mail</param>
        public static void Send(string body, string subject, List <string> filePaths, string ccMailAdress, string mailAddress)
        {
            string mailAddreses = mailAddress;

            string[] mails   = mailAddreses.Split(new char[] { ';' });
            string[] ccMails = ccMailAdress.Split(new char[] { ';' });
            if (string.IsNullOrEmpty(mailAddreses))
            {
                Logger.Log("E-mail avrebbe dovuto inviare, nessun indirizzo e-mail definito.", LogType.Error);
                return;
            }
            var message = new MailMessage()
            {
                From = new MailAddress(ConfigUtils.GetSenderAddress(), ConfigUtils.GetSenderAddress()),

                Subject    = subject,
                Body       = body,
                IsBodyHtml = true
            };

            try
            {
                foreach (string filePath in filePaths)
                {
                    try
                    {
                        message.Attachments.Add(new Attachment(filePath, MediaTypeNames.Application.Octet));
                    }
                    catch { }
                }
            }
            catch
            { }
            for (int i = 0; i < mails.Length; i++)
            {
                if (mails[i] != string.Empty)
                {
                    message.To.Add(new MailAddress(mails[i], mails[i]));
                }
            }
            for (int i = 0; i < ccMails.Length; i++)
            {
                if (ccMails[i] != string.Empty)
                {
                    message.CC.Add(new MailAddress(ccMails[i], ccMails[i]));
                }
            }

            try
            {
                var client = new SmtpClient();
                client.EnableSsl = ConfigUtils.GetServerSecurity();
                client.Port      = int.Parse(ConfigUtils.GetServerPort());
                client.Host      = ConfigUtils.GetMailServer();
                System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(ConfigUtils.GetSenderAddress(), ConfigUtils.GetSenderPassword());
                client.UseDefaultCredentials = false;
                client.Credentials           = SMTPUserInfo;
                client.Timeout        = 3000000;
                client.SendCompleted += new
                                        SendCompletedEventHandler(SendCompletedCallback);
                client.Send(message);
                Logger.Log("Sent mail to " + mailAddreses + " and cc:" + ccMailAdress, LogType.Operation);
                try
                {
                    message.Dispose();
                }
                catch { }
            }
            catch (Exception ex)
            {
                Logger.Log("Error sending mail: " + ex.Message + "\n" + ex.StackTrace, LogType.Error);
            }

            try
            {
                message.Dispose();
            }
            catch { }
        }
Beispiel #2
0
        public static string WriteExcel(string name, List <DataTable> dataTables, List <string> SheetsNames)
        {
            string path = ConfigUtils.GetExcelPath() + name + ".xls";

            if (File.Exists(path))
            {
                string lastNumber;
                lastNumber = name.Substring(name.Length - 1, 1);
                name       = name.Substring(0, name.Length - 1);
                int numVal = Convert.ToInt32(lastNumber);
                numVal++;
                name = name + Convert.ToString(numVal);
                path = ConfigUtils.GetExcelPath() + name + ".xls";
            }

            Logger.Log("Creating file " + AppDomain.CurrentDomain.BaseDirectory + name + ".xls", LogType.Operation);

            Excel.Application xlApp;
            Excel.Workbook    xlWorkBook;

            object misValue = System.Reflection.Missing.Value;

            try
            {
                xlApp      = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                for (int i = 0; i < dataTables.Count; i++)
                {
                    Excel.Worksheet xlWorkSheet;
                    xlWorkSheet      = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(i + 1);
                    xlWorkSheet.Name = SheetsNames[i];
                    fiilHeadersWorkSheet(xlWorkSheet, dataTables[i]);
                    fillWorkSheet(xlWorkSheet, dataTables[i]);
                    releaseObject(xlWorkSheet);
                }

                try
                {
                    File.Delete(path);
                }
                catch { }
                try
                {
                    xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                }
                catch
                {
                    try
                    {
                        string directory = AppDomain.CurrentDomain.BaseDirectory;
                        try
                        {
                            File.Delete(directory + name + ".xls");
                        }
                        catch { }
                        xlWorkBook.SaveAs(directory + name + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    }
                    catch (Exception ex)
                    {
                        MailUtils.Send("Impossibile creare il file di excel",
                                       "PRESTITEMPO ESITI: errori nell’inserimento degli esiti su sito committente",
                                       new List <string>(),
                                       ConfigUtils.GetCaricoAddress(), ConfigUtils.GetPostAddress());
                        throw (ex);
                    }
                }
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
                Logger.Log("Created file" + name + ".xls", LogType.Operation);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
            }
            catch (Exception ex)
            {
                Logger.Log("Could not create file " + name + ".xls" + "\n" + ex.StackTrace.ToString(), LogType.Error);
            }

            return(path);
        }