Example #1
0
        /// <summary>
        /// Create and save a new email with an image attachment, just to test the mail system out one time!
        /// </summary>
        internal void CreateNewTestEmails()
        {
            // Construct a new email message and set data from test configuration in exe.config file
            var newEmail = new Mail();

            newEmail.From = this.configData.TestData.From;
            newEmail.ToAddresses = this.configData.TestData.To;
            newEmail.CcAddresses = this.configData.TestData.Cc;
            newEmail.BccAddresses = this.configData.TestData.Bcc;
            newEmail.Subject = this.configData.TestData.Subject;
            newEmail.SubjectPrefix = this.configData.TestData.SubjectPrefix;
            newEmail.Body = this.configData.TestData.Body;
            newEmail.HasAttachments = this.configData.TestData.HasAttachments;

            // Email is ready to be sent as-is if we don't have any attachments!
            newEmail.MarkMailAsReady = !this.configData.TestData.HasAttachments;

            // Save this email to DB
            this.dbConnect.SaveEmail(newEmail);

            // Add attachment data only if TestData had this configured
            if (newEmail.HasAttachments.GetValueOrDefault()) {

                // Add attachment
                var mailAttachment = new MailAttachment();
                mailAttachment.MailId = newEmail.MailId;
                mailAttachment.Filename = this.configData.TestData.Filename;

                // Sample image that is a concentric circle, to demonstrate that attachments work well!
                mailAttachment.Bytes = Convert.FromBase64String(this.configData.TestData.Attachment);

                mailAttachment.Filesize = (uint)mailAttachment.Bytes.Length;

                // Save attachment to DB
                dbConnect.SaveAttachment(mailAttachment);

                // Mark email as ready to be processed and sent!
                dbConnect.MarkEmailAsReady(newEmail.MailId.GetValueOrDefault());
            }

            // Construct a new email message and set data from test configuration in exe.config file
            newEmail = new Mail();

            newEmail.ToAddresses = this.configData.TestData.To;
            newEmail.Subject = this.configData.TestData.Subject;
            newEmail.Body = this.configData.TestData.Body;
            newEmail.HasAttachments = false;
            newEmail.Direct = true;
            newEmail.Timestamp = DateTime.UtcNow.AddMinutes(3);
            newEmail.Importance = true;

            // Email is ready to be sent as-is if we don't have any attachments!
            newEmail.MarkMailAsReady = true;

            // Save this email to DB
            this.dbConnect.SaveEmail(newEmail);
        }
Example #2
0
        /// <summary>
        /// Get Next Mail to send out
        /// </summary>
        internal Mail GetNextMailToSend()
        {
            var mail = null as Mail;

            if (this.OpenConnection()) {

                string query = Constants.Table.Queries.GetEmailToSend;

                // Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);

                // Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();

                // Read the data and store them in the list
                if (dataReader.Read()) {

                    mail = new Mail();

                    mail.MailId = dataReader[Constants.Table.Mails.MailId] as uint?;
                    mail.From = dataReader[Constants.Table.Mails.Sender] as string;
                    mail.ToAddresses = dataReader[Constants.Table.Mails.Recipients] as string;
                    mail.CcAddresses = dataReader[Constants.Table.Mails.CcRecipients] as string;
                    mail.BccAddresses = dataReader[Constants.Table.Mails.BccRecipients] as string;
                    mail.Subject = dataReader[Constants.Table.Mails.Subject] as string;
                    mail.SubjectPrefix = dataReader[Constants.Table.Mails.SubjectPrefix] as string;
                    mail.Body = dataReader[Constants.Table.Mails.Body] as string;
                    mail.HasAttachments = (dataReader[Constants.Table.Mails.HasAttachments] as byte?).GetValueOrDefault() == 1;
                    mail.Created = (DateTime)dataReader[Constants.Table.Mails.Created];
                    mail.Direct = (dataReader[Constants.Table.Mails.Direct] as byte?).GetValueOrDefault() == 1;
                    mail.Importance = (dataReader[Constants.Table.Mails.Importance] as byte?).GetValueOrDefault() == 1;
                }

                //close Connection
                this.CloseConnection();
            }

            // Read attachments for this mail, if you have any!
            if ((mail != null) && (mail.HasAttachments.GetValueOrDefault())) {

                if (this.OpenConnection()) {

                    string query = Constants.Table.Queries.GetAttachmentsForEmail;

                    // Create Command
                    MySqlCommand cmd = new MySqlCommand(query, connection);

                    cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.MailId, mail.MailId);

                    // Create a data reader and Execute the command
                    MySqlDataReader dataReader = cmd.ExecuteReader();

                    // Read the data and store them in the list
                    while (dataReader.Read()) {

                        var attachment = new MailAttachment();

                        attachment.MailId = dataReader[Constants.Table.Mails.MailId] as uint?;
                        attachment.Filename = dataReader[Constants.Table.Attachments.Filename] as string;
                        attachment.Filesize = dataReader[Constants.Table.Attachments.Filesize] as uint?;
                        attachment.Bytes = dataReader[Constants.Table.Attachments.Attachment] as byte[];

                        mail.AddAttachment(attachment);
                    }

                    //close Connection
                    this.CloseConnection();
                }
            }

            return mail;
        }
Example #3
0
        /// <summary>
        /// Save this email to database
        /// </summary>
        /// <param name="mail">Mail object</param>
        internal void SaveEmail(Mail mail)
        {
            if (this.OpenConnection()) {

                var query = Constants.Table.Queries.AddEmail;

                // Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);

                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Sender, mail.Direct ? DBNull.Value : mail.From as object);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Recipients, mail.ToAddresses);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.CcRecipients, string.IsNullOrEmpty(mail.CcAddresses) ? DBNull.Value : mail.CcAddresses as object);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.BccRecipients, string.IsNullOrEmpty(mail.BccAddresses) ? DBNull.Value : mail.BccAddresses as object);

                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Subject, mail.Subject);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.SubjectPrefix, string.IsNullOrEmpty(mail.SubjectPrefix) ? DBNull.Value : mail.SubjectPrefix as object);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Body, mail.Body);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.MarkMailAsReady, mail.MarkMailAsReady);

                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.HasAttachments, mail.HasAttachments);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Importance, mail.Importance);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Direct, mail.Direct);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Timestamp, mail.Timestamp.HasValue ? mail.Timestamp.GetValueOrDefault() as object : DBNull.Value);

                // Execute Scalar - don't bother reading the mail attachment ID
                mail.MailId = (uint)((cmd.ExecuteScalar() as ulong?).GetValueOrDefault());

                //close Connection
                this.CloseConnection();
            }
        }
Example #4
0
        /// <summary>
        /// Get Next Mail to send out
        /// </summary>
        internal Mail GetNextMailToSend()
        {
            var mail = null as Mail;

            if (this.OpenConnection())
            {
                string query = Constants.Table.Queries.GetEmailToSend;

                // Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);

                // Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();

                // Read the data and store them in the list
                if (dataReader.Read())
                {
                    mail = new Mail();

                    mail.MailId         = dataReader[Constants.Table.Mails.MailId] as uint?;
                    mail.From           = dataReader[Constants.Table.Mails.Sender] as string;
                    mail.ToAddresses    = dataReader[Constants.Table.Mails.Recipients] as string;
                    mail.CcAddresses    = dataReader[Constants.Table.Mails.CcRecipients] as string;
                    mail.BccAddresses   = dataReader[Constants.Table.Mails.BccRecipients] as string;
                    mail.Subject        = dataReader[Constants.Table.Mails.Subject] as string;
                    mail.SubjectPrefix  = dataReader[Constants.Table.Mails.SubjectPrefix] as string;
                    mail.Body           = dataReader[Constants.Table.Mails.Body] as string;
                    mail.HasAttachments = (dataReader[Constants.Table.Mails.HasAttachments] as byte?).GetValueOrDefault() == 1;
                    mail.Created        = (DateTime)dataReader[Constants.Table.Mails.Created];
                    mail.Direct         = (dataReader[Constants.Table.Mails.Direct] as byte?).GetValueOrDefault() == 1;
                    mail.Importance     = (dataReader[Constants.Table.Mails.Importance] as byte?).GetValueOrDefault() == 1;
                }

                //close Connection
                this.CloseConnection();
            }

            // Read attachments for this mail, if you have any!
            if ((mail != null) && (mail.HasAttachments.GetValueOrDefault()))
            {
                if (this.OpenConnection())
                {
                    string query = Constants.Table.Queries.GetAttachmentsForEmail;

                    // Create Command
                    MySqlCommand cmd = new MySqlCommand(query, connection);

                    cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.MailId, mail.MailId);

                    // Create a data reader and Execute the command
                    MySqlDataReader dataReader = cmd.ExecuteReader();

                    // Read the data and store them in the list
                    while (dataReader.Read())
                    {
                        var attachment = new MailAttachment();

                        attachment.MailId   = dataReader[Constants.Table.Mails.MailId] as uint?;
                        attachment.Filename = dataReader[Constants.Table.Attachments.Filename] as string;
                        attachment.Filesize = dataReader[Constants.Table.Attachments.Filesize] as uint?;
                        attachment.Bytes    = dataReader[Constants.Table.Attachments.Attachment] as byte[];

                        mail.AddAttachment(attachment);
                    }

                    //close Connection
                    this.CloseConnection();
                }
            }

            return(mail);
        }