public void SaveMailContacts(int tenant, string user, Message message)
        {
            try
            {
                Func <AddressCollection, AddressCollection> copyAddressesFunc = delegate(AddressCollection addresses)
                {
                    var newAddresses = new AddressCollection();

                    foreach (var address in addresses)
                    {
                        newAddresses.Add(new Address(address.Email,
                                                     !string.IsNullOrEmpty(address.Name) ? Codec.RFC2047Decode(address.Name) : string.Empty));
                    }

                    return(newAddresses);
                };

                var contacts = new AddressCollection();

                contacts.AddRange(copyAddressesFunc(message.To));
                contacts.AddRange(copyAddressesFunc(message.Cc));
                contacts.AddRange(copyAddressesFunc(message.Bcc));

                var contactsList = contacts.Distinct().ToList();

                using (var db = GetDb())
                {
                    var validContacts = (from contact in contactsList
                                         where MailContactExists(db, tenant, user, contact.Name, contact.Email) < 1
                                         select contact).ToList();

                    if (!validContacts.Any())
                    {
                        return;
                    }

                    var lastModified = DateTime.UtcNow;

                    var insertQuery = new SqlInsert(ContactsTable.name)
                                      .InColumns(ContactsTable.Columns.id_user,
                                                 ContactsTable.Columns.id_tenant,
                                                 ContactsTable.Columns.name,
                                                 ContactsTable.Columns.address,
                                                 ContactsTable.Columns.last_modified);

                    validContacts
                    .ForEach(contact =>
                             insertQuery
                             .Values(user, tenant, contact.Name, contact.Email, lastModified));

                    db.ExecuteNonQuery(insertQuery);
                }
            }
            catch (Exception e)
            {
                _log.Error("SaveMailContacts(tenant={0}, userId='{1}', mail_id={2}) Exception:\r\n{3}\r\n",
                           tenant, user, message.Id, e.ToString());
            }
        }
        public void SaveMailContacts(int tenant, string user, Message message)
        {
            try
            {
                var contacts = new AddressCollection();
                contacts.AddRange(message.To);
                contacts.AddRange(message.Cc);
                contacts.AddRange(message.Bcc);

                foreach (var contact in contacts)
                {
                    contact.Name = !String.IsNullOrEmpty(contact.Name) ? Codec.RFC2047Decode(contact.Name) : String.Empty;
                }

                var contactsList = contacts.Distinct().ToList();

                using (var db = GetDb())
                {
                    var validContacts = (from contact in contactsList
                                         where MailContactExists(db, tenant, user, contact.Name, contact.Email) < 1
                                         select contact).ToList();

                    if (!validContacts.Any()) return;

                    var lastModified = DateTime.UtcNow;

                    var insertQuery = new SqlInsert(ContactsTable.name)
                        .InColumns(ContactsTable.Columns.id_user,
                                   ContactsTable.Columns.id_tenant,
                                   ContactsTable.Columns.name,
                                   ContactsTable.Columns.address,
                                   ContactsTable.Columns.last_modified);

                    validContacts
                        .ForEach(contact =>
                                 insertQuery
                                     .Values(user, tenant, contact.Name, contact.Email, lastModified));

                    db.ExecuteNonQuery(insertQuery);
                }
            }
            catch (Exception e)
            {
                _log.Error("SaveMailContacts(tenant={0}, userId='{1}', mail_id={2}) Exception:\r\n{3}\r\n",
                          tenant, user, message.Id, e.ToString());
            }
        }