Esempio n. 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);
        }
Esempio n. 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;
        }
Esempio n. 3
0
        /// <summary>
        /// Get Next Mail to send out
        /// </summary>
        /// <param name="attachment">MailAttachment object</param>
        internal void SaveAttachment(MailAttachment attachment)
        {
            if (this.OpenConnection()) {

                string query = Constants.Table.Queries.AddMailAttachment;

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

                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.MailId, attachment.MailId);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Filename, attachment.Filename);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Filesize, attachment.Filesize);
                cmd.Parameters.AddWithValue(Constants.Table.Queries.Parameters.Attachment, attachment.Bytes);

                // Execute Scalar - don't bother reading the mail attachment ID
                cmd.ExecuteScalar();

                //close Connection
                this.CloseConnection();
            }
        }
Esempio n. 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);
        }
Esempio n. 5
0
 /// <summary>
 /// Add the MailAttachment to list of attachments
 /// </summary>
 /// <param name="attachment">MailAttachment instance</param>
 internal void AddAttachment(MailAttachment attachment)
 {
     this.attachments.Add(attachment);
 }