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());
            }
        }
예제 #2
0
        /// <summary>
        /// Loads the addresses.
        /// </summary>
        public void LoadAddresses()
        {
            List <Address> addressList          = WebProfile.Current.AddressCollection.FindAll(delegate(Address address) { return(address.AddressType == this.AddressType); });
            AddressCollection addressCollection = new AddressCollection();

            addressCollection.AddRange(addressList);

            if (addressCollection != null)
            {
                if (addressCollection.Count > 0)
                {
                    ddlAddress.Items.Clear();
                    foreach (Address address in addressCollection)
                    {
                        ddlAddress.Items.Add(new ListItem(string.Format("{0} {1}, {2} {3}, {4}, {5} {6}", address.FirstName, address.LastName, address.Address1, address.Address2, address.City, address.StateOrRegion, address.PostalCode), address.AddressId.ToString()));
                    }
                    ddlAddress.Items.Insert(0, new ListItem(LocalizationUtility.GetText("lblSelect"), string.Empty));
                    if (!string.IsNullOrEmpty(_selectedAddress))
                    {
                        if (ddlAddress.Items.FindByValue(_selectedAddress) != null)
                        {
                            ddlAddress.SelectedValue = _selectedAddress;
                        }
                    }
                }
                else//No addresses yet
                {
                    ShowAddressPanel();
                    pnlAddNew.Visible = false;
                }
            }
        }
        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());
            }
        }
예제 #4
0
        /// <summary>
        /// Loads the addresses.
        /// </summary>
        /// <param name="addressType">Type of the address.</param>
        /// <returns></returns>
        private AddressCollection LoadAddresses(AddressType addressType)
        {
            List <Address> addressList = _user.AddressCollection.FindAll(delegate(Address address) {
                return(address.AddressType == addressType);
            });
            AddressCollection addressCollection = new AddressCollection();

            addressCollection.AddRange(addressList);
            return(addressCollection);
        }
        public static void SaveMailContacts(IDbManager db, int tenant, string user, Message message, ILogger log)
        {
            try
            {
                var contacts = new AddressCollection {
                    message.From
                };
                contacts.AddRange(message.To);
                contacts.AddRange(message.Cc);
                contacts.AddRange(message.Bcc);

                var valid_contacts = (from contact in contacts
                                      where MailContactExists(db, tenant, user, contact.Name, contact.Email) < 1
                                      select contact).ToList();

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

                var last_modified = DateTime.Now;

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

                valid_contacts
                .ForEach(contact =>
                         insert_query
                         .Values(user, tenant, contact.Name, contact.Email, last_modified));

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

                var valid_contacts = (from contact in contacts
                                      where MailContactExists(db, tenant, user, contact.Name, contact.Email) < 1
                                      select contact).ToList();

                if (!valid_contacts.Any()) return;

                var last_modified = DateTime.Now;

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

                valid_contacts
                    .ForEach(contact =>
                             insert_query
                                 .Values(user, tenant, contact.Name, contact.Email, last_modified));

                db.ExecuteNonQuery(insert_query);
            }
            catch (Exception e)
            {
                log.Warn("SaveMailContacts(tenant={0}, userId='{1}', mail_id={2}) Exception:\r\n{3}\r\n",
                          tenant, user, message.Id, e.ToString());
            }
        }
예제 #7
0
        /// <summary>
        /// Loads the addresses.
        /// </summary>
        public void LoadAddresses()
        {
            List<Address> addressList = WebProfile.Current.AddressCollection.FindAll(delegate(Address address) { return address.AddressType == this.AddressType; });
              AddressCollection addressCollection = new AddressCollection();
              addressCollection.AddRange(addressList);

              if(addressCollection != null) {
            if(addressCollection.Count > 0) {
              ddlAddress.Items.Clear();
              foreach(Address address in addressCollection) {
            ddlAddress.Items.Add(new ListItem(string.Format("{0} {1}, {2} {3}, {4}, {5} {6}", address.FirstName, address.LastName, address.Address1, address.Address2, address.City, address.StateOrRegion, address.PostalCode), address.AddressId.ToString()));
              }
              ddlAddress.Items.Insert(0, new ListItem(LocalizationUtility.GetText("lblSelect"), string.Empty));
              if(!string.IsNullOrEmpty(_selectedAddress)) {
              if (ddlAddress.Items.FindByValue(_selectedAddress) != null)
                  ddlAddress.SelectedValue = _selectedAddress;
              }
            }
            else {//No addresses yet
              ShowAddressPanel();
              pnlAddNew.Visible = false;
            }
              }
        }
 /// <summary>
 /// Loads the addresses.
 /// </summary>
 /// <param name="addressType">Type of the address.</param>
 /// <returns></returns>
 private AddressCollection LoadAddresses(AddressType addressType)
 {
     List<Address> addressList = _user.AddressCollection.FindAll(delegate(Address address) {
     return address.AddressType == addressType;
       });
       AddressCollection addressCollection = new AddressCollection();
       addressCollection.AddRange(addressList);
       return addressCollection;
 }
예제 #9
0
        private void _directSendEmail()
        {
            ButtonSend.Enabled   = false;
            ButtonCancel.Enabled = true;
            _isCancelSending     = false;

            AddressCollection recipients = new AddressCollection();

            recipients.AddRange(new AddressCollection(TextBoxTo.Text));
            recipients.AddRange(new AddressCollection(TextBoxCc.Text));

            if (recipients.Count == 0)
            {
                MessageBox.Show("No recipient found!");
                StatusBarSend.Text = "No recipient found!";

                ButtonSend.Enabled   = true;
                ButtonCancel.Enabled = false;
                return;
            }

            // because each recipient might have different SMTP server,
            // so we have to send email to each recipient one by one.
            for (int i = 0; i < recipients.Count; i++)
            {
                MailAddress address = recipients[i] as MailAddress;

                try
                {
                    SmtpServer server = new SmtpServer("");
                    // If remote SMTP server supports TLS, use TLS, then use plain TCP connection.
                    server.ConnectType = SmtpConnectType.ConnectTryTLS;

                    // create SmtpMail instance for each recipient
                    SmtpMail mail = _createMailForDirectSend(address);
                    StatusBarSend.Text    = string.Format("Connecting server for {0} ... ", address.Address);
                    ProgressBarSend.Value = 0;

                    SmtpClient smtp = new SmtpClient();

                    // Catching the following events is not necessary,
                    // just make the application more user friendly.
                    // If you use the object in asp.net/windows service or non-gui application,
                    // You need not to catch the following events.
                    // To learn more detail, please refer to the code in EASendMail EventHandler region

                    smtp.OnIdle              += new SmtpClient.OnIdleEventHandler(OnIdle);
                    smtp.OnAuthorized        += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized);
                    smtp.OnConnected         += new SmtpClient.OnConnectedEventHandler(OnConnected);
                    smtp.OnSecuring          += new SmtpClient.OnSecuringEventHandler(OnSecuring);
                    smtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream);

                    smtp.SendMail(server, mail);

                    MessageBox.Show(string.Format("The message to <{0}> was sent to {1} successfully!",
                                                  address.Address,
                                                  smtp.CurrentSmtpServer.Server));

                    StatusBarSend.Text = "Completed";
                }
                catch (SmtpTerminatedException exp)
                {
                    StatusBarSend.Text = exp.Message;
                    break;
                }
                catch (Exception exp)
                {
                    MessageBox.Show(string.Format("The message was unable to delivery to <{0}> due to \r\n{1}",
                                                  address.Address, exp.Message));

                    StatusBarSend.Text = string.Format("Exception: {0}", exp.Message);
                }
            }

            ButtonSend.Enabled   = true;
            ButtonCancel.Enabled = false;
        }