Ejemplo n.º 1
0
        public static bool SendMail(MailMessage message, MailPriority priority, int auditLogId = 0)
        {
            try
            {
                SmtpClient client   = new SmtpClient();
                string     smtpHost = ConfigurationManager.AppSettings["SmtpHost"];

                if (!String.IsNullOrEmpty(smtpHost))
                {
                    client.Host = smtpHost;
                }
                else
                {
                    client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                }

                message.Priority = priority;

                if (message.From == null)
                {
                    message.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailAddress"]);
                }

                if (message.From == null)
                {
                    throw new InvalidOperationException("Mail can't be without sender");
                }

                client.Send(message);
            }
            catch (Exception ex)
            {
                try
                {
                    EventLog myLog       = new EventLog();
                    string   traceSource = "logging-event-source-name";
                    if (String.IsNullOrEmpty(traceSource))
                    {
                        traceSource = "NOC";
                    }

                    myLog.Source = traceSource;
                    myLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
                }
                catch
                {
                }

                if (auditLogId <= 0)
                {
                    auditLogId = AuditLog.AddLog("SendMail exception").Id;
                }

                IObjectManager manager    = (ManagerCache.GetManager(typeof(AuditLog)));
                AuditLog       log        = (AuditLog)manager.GetObject(auditLogId);
                string         messageLog = "";
                messageLog += "Expection: " + ex.ToString();
                if (ex.InnerException != null)
                {
                    messageLog += "\r\n<br/>Inner Exception: " + ex.InnerException.ToString();
                }
                messageLog += "\r\n<br/>Mail To: " + message.To.ToString();
                messageLog += "\r\n<br/>Mail Body: " + message.Body;
                log.AddLogDetails(AuditLogType.Error, messageLog);
            }

            return(true);
        }