Пример #1
0
        /// <summary>
        /// Sends an e-mail (and creates an e-mail audit record).
        /// </summary>
        public static void SendEmail(MailMessage message, EmailCategory? category, int? relatedEntityID, int? userIdReceiver = null)
        {
            int? currentUserId = null;
            if (LogonUserDal.GetCurrentUser()!=null) {
                currentUserId = LogonUserDal.GetCurrentUser().Id;
            }

            EmailAuditDal emailAuditDal = new EmailAuditDal(message, category, currentUserId, userIdReceiver, relatedEntityID);

            SmtpClient smtpClient = new SmtpClient();

            // Try to send the mail.
            try {
                smtpClient.Send(message);

                // When the send succeeds set the status to send and set the time and date.
                emailAuditDal.EmailStatus = EmailStatus.Sent;
                emailAuditDal.DateSent = System.DateTime.Now;
            } catch (SmtpException e) {
                // When sending the mail fails set the status to sendError and set the status message.
                emailAuditDal.EmailStatus = EmailStatus.SendError;
                emailAuditDal.StatusMessage = string.Format("Fout bij het verzenden ('{0}').", e.Message);
            }

            emailAuditDal.Save();
        }
Пример #2
0
        /// <summary>
        /// Sends a previously audited e-mail.
        /// Returns true if sending succeeds, false otherwise.
        /// </summary>
        public static bool ResendEmail(EmailAuditDal emailAuditDal)
        {
            MailMessage message = emailAuditDal.CreateMailMessage();

            SmtpClient smtpClient = new SmtpClient();

            bool isMailSent = false;

            // Try to send the mail.
            try {
                emailAuditDal.DateSent = System.DateTime.Now;
                smtpClient.Send(message);

                // When the send succeeds set the status to send and set the time and date.
                emailAuditDal.EmailStatus = EmailStatus.Sent;
                emailAuditDal.StatusMessage = "Opnieuw verzonden na eerdere fout bij verzenden.";
                isMailSent = true;
            } catch (SmtpException e) {
                // When sending the mail fails set the status to sendError and set the status message.
                emailAuditDal.EmailStatus = EmailStatus.SendError;
                emailAuditDal.StatusMessage = e.Message;
            } finally {
                emailAuditDal.Save();
            }

            return isMailSent;
        }