Exemplo n.º 1
0
        public static bool invioEmail(string indirizzo, messaggio messaggio)
        {
            try
            {
                Outlook.Application olkApp1  = new Outlook.Application();
                Outlook.MailItem    olkMail1 = (MailItem)olkApp1.CreateItem(OlItemType.olMailItem);
                Accounts            accounts = olkApp1.Application.Session.Accounts;
                foreach (Account a in accounts)
                {
                    if (a.SmtpAddress == indirizzo)
                    {
                        olkMail1.SendUsingAccount = a;
                    }
                }

                olkMail1.To      = messaggio.Destinatario;
                olkMail1.CC      = messaggio.Copia;
                olkMail1.BCC     = messaggio.CopiaNascosta;
                olkMail1.Subject = messaggio.Oggetto;
                olkMail1.Body    = messaggio.Messaggio;
                if (messaggio.Allegati.Count > 0)
                {
                    messaggio.Allegati.ForEach(delegate(string allegato)
                    {
                        olkMail1.Attachments.Add(allegato);
                    });
                }
                olkMail1.Send();
                return(true);
            }
            catch (System.Exception ex) {
                MailerLogger.SendLogToDB(1, "Errore Durante Invio Messaggi: " + ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
            public static T Load <T>(string fileName) where T : PersistableObject, new()
            {
                try
                {
                    T result = default(T);

                    using (FileStream stream = File.OpenRead(fileName))
                    {
                        result = new XmlSerializer(typeof(T)).Deserialize(stream) as T;
                    }

                    return(result);
                }
                catch (Exception ex) {
                    MailerLogger.SendLogToDB(1, "Errore Durante Lettura Parametri di configurazione: " + ex.Message);
                    return(null);
                }
            }
Exemplo n.º 3
0
        public List <messaggio> elaboraExcel(string percorso, string messaggioWord)
        {
            try
            {
                List <messaggio> messaggi    = new List <messaggio>();
                List <string>    tags        = new List <string>();
                Application      xlApp       = new Application();
                Workbook         xlWorkBook  = xlApp.Workbooks.Open(@percorso, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                Worksheet        xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
                Range            range       = xlWorkSheet.UsedRange;
                int righe   = range.Rows.Count;
                int colonne = range.Columns.Count;
                int rCnt;
                int cCnt;
                //Prendo la lista dei TAG
                for (cCnt = 1; cCnt <= colonne; cCnt++)
                {
                    if (cCnt > 6)
                    {
                        tags.Add((string)(range.Cells[1, cCnt] as Range).Value2);
                    }
                }
                //Dalla seconda riga in poi preparo i messaggi
                for (rCnt = 2; rCnt <= righe; rCnt++)
                {
                    messaggio mex = new messaggio();
                    mex.errore        = false;
                    mex.MessaggioTest = ((string)(range.Cells[rCnt, 1] as Range).Value2).ToLower().Trim().Equals("si");
                    mex.Destinatario  = (string)(range.Cells[rCnt, 2] as Range).Value2;
                    if (string.IsNullOrEmpty(mex.Destinatario))
                    {
                        mex.errore = true;
                    }
                    mex.Copia         = (string)(range.Cells[rCnt, 3] as Range).Value2;
                    mex.CopiaNascosta = (string)(range.Cells[rCnt, 4] as Range).Value2;
                    mex.Oggetto       = (string)(range.Cells[rCnt, 6] as Range).Value2;
                    if (string.IsNullOrEmpty(mex.Oggetto))
                    {
                        mex.errore = true;
                    }
                    if ((range.Cells[rCnt, 5] as Range).Value2 != null)
                    {
                        List <string> listaAllegati = ((string)(range.Cells[rCnt, 5] as Range).Value2).Split(';').ToList();
                        listaAllegati.ForEach(delegate(string allegato)
                        {
                            if (!File.Exists(allegato))
                            {
                                mex.errore = true;
                            }
                        });
                        mex.Allegati = listaAllegati;
                    }
                    else
                    {
                        mex.Allegati = null;
                    }



                    mex.Messaggio = messaggioWord;

                    for (cCnt = 7; cCnt <= colonne; cCnt++)
                    {
                        mex.Messaggio = mex.Messaggio.Replace("<<" + tags[cCnt - 7] + ">>", (string)(range.Cells[rCnt, cCnt] as Range).Value2);
                    }
                    messaggi.Add(mex);
                }
                return(messaggi);
            }
            catch (Exception EX) {
                MailerLogger.SendLogToDB(1, "Errore Durante Lettura Excel: " + EX.Message);
                return(null);
            }
        }