//------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Send Emails using smtp settings
        /// </summary>
        /// <param name="transportInfo">The transport info.</param>
        /// <param name="emails">The emails.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="message">The message.</param>
        /// <param name="attachment">The attachments list. Send null in case there is no attachment</param>
        public void SmtpSend(SmtpInfo smtpInfo, List <string> toEmails, List <string> ccEmails, string subject, string message, List <string> attachments)
        {
            SmtpClient smtp = new SmtpClient();
            Attachment updatesAttachement = null;

            if (smtpInfo.ProtectPassword)
            {
                smtpInfo.Password = OperationUtils.EncryptDecrypt(smtpInfo.Password);
            }

            try
            {
                smtp.Credentials = new System.Net.NetworkCredential(smtpInfo.UserName, smtpInfo.Password);
                smtp.Host        = smtpInfo.SmtpServer;
                smtp.Port        = smtpInfo.Port;
                smtp.EnableSsl   = smtpInfo.SSL;
                smtp.Timeout     = smtpInfo.TimeOut;

                mail      = new MailMessage();
                mail.From = new MailAddress(smtpInfo.UserName);

                foreach (string toEmail in toEmails)
                {
                    if (toEmail != string.Empty)
                    {
                        mail.To.Add(toEmail);
                    }
                }

                if (ccEmails != null)
                {
                    foreach (string ccEmail in ccEmails)
                    {
                        if (ccEmail != string.Empty)
                        {
                            mail.CC.Add(ccEmail);
                        }
                    }
                }

                mail.Subject    = subject;
                mail.IsBodyHtml = smtpInfo.IsBodyHtml;
                mail.Body       = message;
                if (attachments != null)
                {
                    foreach (string attachment in attachments)
                    {
                        if (File.Exists(attachment))
                        {
                            updatesAttachement = new Attachment(attachment);
                            mail.Attachments.Add(updatesAttachement);
                        }
                        else
                        {
                            throw new ArgumentException("One or more attachment provided are not valid");
                        }
                    }
                }

                if (SendAsynchronous)
                {
                    smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
                    smtp.SendAsync(mail, null);
                }
                else
                {
                    smtp.Send(mail);
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            finally
            {
                if (!SendAsynchronous)
                {
                    mail.Dispose();
                }
                if (smtpInfo.ProtectPassword)
                {
                    smtpInfo.Password = OperationUtils.EncryptDecrypt(smtpInfo.Password);
                }
                if (updatesAttachement != null)
                {
                    updatesAttachement.Dispose();
                }
            }
        }
Пример #2
0
        private void SerializeDefaultObject()
        {
            //PMAInfo
            pmaInfo = new PMAInfo();
            pmaInfo.MailingTime = DateTime.Now.ToString("d/M/yyyy HH:mm");
            pmaInfo.ReportsIntervalHours = 12;
            pmaInfo.ClientName = System.Environment.MachineName;
            pmaInfo.DisposeLogFile = false;
            pmaInfo.TriggerSeed = 20;
            pmaInfo.UseFTP = true;
            pmaInfo.UseSMTP = false;

            // EMAIL INFO
            emailsInfo = new Emails();
            emailsInfo.EmailTo = new List<string>();
            emailsInfo.EmailTo.Add("*****@*****.**");
            emailsInfo.EmailCC = new List<string>();
            emailsInfo.EmailCC.Add("*****@*****.**");
            emailsInfo.AttachmentPath = "";
            emailsInfo.Subject = "Server Report";
            emailsInfo.BodyContent = "Please Find the Report Attached";

            // SMTP Info
            smtpInfo = new SmtpInfo();
            smtpInfo.ProtectPassword = true;
            smtpInfo.UserName = "******";
            smtpInfo.Password = "******";
            smtpInfo.Port = 587;
            smtpInfo.SmtpServer = "smtp.gmail.com";
            smtpInfo.SSL = true;
            smtpInfo.TimeOut = 100000;

            //FTP Info
            ftpInfo = new FTPInfo();
            ftpInfo.FTPServer = "ftp://202.54.213.231";
            ftpInfo.FTPServerFolder = "PerformanceReports";
            ftpInfo.Password = "******";
            ftpInfo.Port = 21;
            ftpInfo.ProtectPassword = true;
            ftpInfo.SSL = false;
            ftpInfo.TimeOut = 100000;
            ftpInfo.UserName = "******";

            SerializedInfo();
        }
 //--------------------------------------------------------------------------------------------
 /// <summary>
 /// Initilizes the SMTP object.
 /// </summary>
 private void InitilizeSMTPObject()
 {
     if (SmtpInfo == null)
     {
         if (File.Exists(Path.Combine(CurrentAppConfigDir, SmtpInfo.SMTP_INFO_FILE)))
         {
             SmtpInfo = SmtpInfo.Deserialize(File.ReadAllText(Path.Combine(CurrentAppConfigDir, SmtpInfo.SMTP_INFO_FILE)));
         }
         else SmtpInfo = new SmtpInfo();
     }
 }
Пример #4
0
 private void DeserilizeObjects()
 {
     try
     {
         pmaInfo = PMAInfo.Deserialize(File.ReadAllText(Path.Combine(configManager.CurrentAppConfigDir, PMAInfo.PMA_INFO_FILE)));
         emailsInfo = Emails.Deserialize(File.ReadAllText(Path.Combine(configManager.CurrentAppConfigDir, Emails.EMAILS_INFO_FILE)));
         smtpInfo = SmtpInfo.Deserialize(File.ReadAllText(Path.Combine(configManager.CurrentAppConfigDir, SmtpInfo.SMTP_INFO_FILE)));
         ftpInfo = FTPInfo.Deserialize(File.ReadAllText(Path.Combine(configManager.CurrentAppConfigDir, FTPInfo.FTP_INFO_FILE)));
     }
     catch(Exception ex)
     {
         throw ex;
     }
 }