Ejemplo n.º 1
0
        /// <summary>
        /// Generates a mail message for the given target
        /// </summary>
        /// <param name="recipient">The message target</param>
        /// <returns>A mail message generated for the given target</returns>
        public MailMessage GenerateMessage(MailMergeRecipient recipient)
        {
            // BUILD MESSAGE GOING TO SPECIFIED ADDRESSES
            MailMessage mailMessage = new MailMessage();

            foreach (MailAddress item in recipient.RecipientAddresses)
            {
                mailMessage.To.Add(item);
            }
            foreach (MailAddress item in _CCList)
            {
                mailMessage.CC.Add(item);
            }
            foreach (MailAddress item in _BCCList)
            {
                mailMessage.Bcc.Add(item);
            }

            // SET MESSAGE DATA
            mailMessage.From = _FromAddress;
            NVelocityEngine velocityEngine = NVelocityEngine.Instance;

            MergeSharedParameters(this.Parameters, recipient.Parameters);
            mailMessage.Subject    = velocityEngine.Process(recipient.Parameters, this.Subject);
            mailMessage.Body       = velocityEngine.Process(recipient.Parameters, this.Body);
            mailMessage.IsBodyHtml = this.IsHTML;

            // RETURN THE GENERATED MESSAGE
            return(mailMessage);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates mail messages for this email template
        /// </summary>
        /// <returns>An array of MailMessage objects generated</returns>
        public MailMessage[] GenerateMailMessages()
        {
            //BUILD AN ARRAY OF MESSAGES TO BE SENT
            List <MailMessage> messages       = new List <MailMessage>();
            NVelocityEngine    velocityEngine = NVelocityEngine.Instance;

            //PARSE THE ADDRESSES
            bool hasVendor;
            List <MailAddress> addressList = this.ParseAddresses(this.ToAddress, out hasVendor);
            List <MailAddress> ccList      = this.ParseAddresses(this.CCList);
            List <MailAddress> bccList     = this.ParseAddresses(this.BCCList);

            //CHECK FOR TO ADDRESSES
            if (addressList.Count > 0)
            {
                //BUILD MESSAGE GOING TO SPECIFIC ADDRESSES
                MailMessage mailMessage = new MailMessage();
                //ADD TO ADDRESSES
                foreach (MailAddress item in addressList)
                {
                    mailMessage.To.Add(item);
                }
                //ADD CC ADDRESSES
                foreach (MailAddress item in ccList)
                {
                    mailMessage.CC.Add(item);
                }
                //ADD BCC ADDRESSES
                foreach (MailAddress item in bccList)
                {
                    mailMessage.Bcc.Add(item);
                }
                //SET FROM ADDRESS
                mailMessage.From = new MailAddress(this.FromAddress);
                //PROCESS THE SUBJECT AND BODY
                mailMessage.Subject    = velocityEngine.Process(this.Parameters, this.Subject);
                mailMessage.Body       = velocityEngine.Process(this.Parameters, this.Body);
                mailMessage.IsBodyHtml = this.IsHTML;
                messages.Add(mailMessage);
            }

            //PROCESS VENDOR MESSAGES IF WE HAVE AN ORDER CONTEXT
            if (hasVendor && this.Parameters.ContainsKey("order"))
            {
                Order order = (Order)this.Parameters["order"];
                //DECIDE WHICH VENDORS NEED TO HAVE MESSAGES COMPILED
                VendorCollection vendors = VendorDataSource.LoadForOrder(order.OrderId);
                if (vendors.Count > 0)
                {
                    //VENDORS ARE FOUND, WE MUST COMPILE ONE MESSAGE FOR EACH VENDOR
                    foreach (Vendor vendor in vendors)
                    {
                        List <string> emailList = GetValidEmailList(vendor.Email);
                        if (emailList != null && emailList.Count > 0)
                        {
                            //BUILD VENDOR SPECIFIC ITEM COLLECTIONS
                            OrderItemCollection vendorItems            = new OrderItemCollection();
                            OrderItemCollection vendorNonShippingItems = new OrderItemCollection();
                            foreach (OrderItem item in order.Items)
                            {
                                if (item.Product != null)
                                {
                                    //SEE IF THIS IS A PRODUCT THAT BELONGS TO THE VENDOR
                                    if (item.Product.VendorId == vendor.VendorId)
                                    {
                                        vendorItems.Add(item);
                                        if (item.OrderShipmentId == 0)
                                        {
                                            vendorNonShippingItems.Add(item);
                                        }
                                    }
                                }
                                else
                                {
                                    //SEE IF THIS IS A NON-PRODUCT ITEM THAT APPLIES
                                    //E.G. A DISCOUNT ON A VENDOR ITEM
                                    if (vendorItems.IndexOf(item.ParentItemId) > -1)
                                    {
                                        vendorItems.Add(item);
                                        if (item.OrderShipmentId == 0)
                                        {
                                            vendorNonShippingItems.Add(item);
                                        }
                                    }
                                }
                            }
                            //BUILD VENDOR SPECIFIC SHIPMENT COLLECTION
                            OrderShipmentCollection vendorShipments = new OrderShipmentCollection();
                            foreach (OrderItem item in vendorItems)
                            {
                                if ((item.OrderShipment != null) && (vendorShipments.IndexOf(item.OrderShipmentId) < 0))
                                {
                                    vendorShipments.Add(item.OrderShipment);
                                }
                            }
                            //CREATE VENDOR SPECIFIC PARAMETERS
                            Hashtable vendorParameters = new Hashtable(this.Parameters);
                            vendorParameters["Vendor"]                 = vendor;
                            vendorParameters["VendorShipments"]        = vendorShipments;
                            vendorParameters["VendorItems"]            = vendorItems;
                            vendorParameters["VendorNonShippingItems"] = vendorNonShippingItems;
                            //COMPILE VENDOR SPECIFIC MAIL MESSAGE
                            MailMessage mailMessage = new MailMessage();
                            //ADD TO ADDRESSES
                            foreach (string email in emailList)
                            {
                                mailMessage.To.Add(email);
                            }
                            //ADD CC ADDRESSES
                            foreach (MailAddress item in ccList)
                            {
                                mailMessage.CC.Add(item);
                            }
                            //ADD BCC ADDRESSES
                            foreach (MailAddress item in bccList)
                            {
                                mailMessage.Bcc.Add(item);
                            }
                            //SET FROM ADDRESS
                            mailMessage.From = new MailAddress(this.FromAddress);
                            //PROCESS THE SUBJECT AND BODY
                            mailMessage.Subject    = velocityEngine.Process(vendorParameters, this.Subject);
                            mailMessage.Body       = velocityEngine.Process(vendorParameters, this.Body);
                            mailMessage.IsBodyHtml = this.IsHTML;
                            messages.Add(mailMessage);
                        }
                        else
                        {
                            Logger.Warn("Could not process vendor notification for '" + vendor.Name + "'; email address is invalid.");
                        }
                    }
                }
            }
            else
            if (hasVendor && this.Parameters.ContainsKey("vendors"))
            {
                Vendor[]         vendors  = (Vendor[])this.Parameters["vendors"];
                Product[]        products = (Product[])this.Parameters["products"];
                ProductVariant[] variants = (ProductVariant[])this.Parameters["variants"];

                if (vendors != null && vendors.Length > 0)
                {
                    foreach (Vendor vendor in vendors)
                    {
                        List <string> emailList = GetValidEmailList(vendor.Email);
                        if (emailList != null && emailList.Count > 0)
                        {
                            List <Product>        vendorProducts = new List <Product>();
                            List <ProductVariant> vendorVariants = new List <ProductVariant>();

                            if (products != null)
                            {
                                foreach (Product product in products)
                                {
                                    if (product.Vendor != null && product.VendorId == vendor.VendorId)
                                    {
                                        vendorProducts.Add(product);
                                    }
                                }
                            }

                            if (variants != null)
                            {
                                foreach (ProductVariant variant in variants)
                                {
                                    if (variant.Product.Vendor != null && variant.Product.Vendor.VendorId == vendor.VendorId)
                                    {
                                        vendorVariants.Add(variant);
                                    }
                                }
                            }

                            //CREATE VENDOR SPECIFIC PARAMETERS
                            Hashtable vendorParameters = new Hashtable(this.Parameters);
                            vendorParameters["vendor"]   = vendor;
                            vendorParameters["products"] = vendorProducts;
                            vendorParameters["variants"] = vendorVariants;

                            //COMPILE VENDOR SPECIFIC MAIL MESSAGE
                            MailMessage mailMessage = new MailMessage();
                            //ADD TO ADDRESSES
                            foreach (string email in emailList)
                            {
                                mailMessage.To.Add(email);
                            }
                            //ADD CC ADDRESSES
                            foreach (MailAddress item in ccList)
                            {
                                mailMessage.CC.Add(item);
                            }
                            //ADD BCC ADDRESSES
                            foreach (MailAddress item in bccList)
                            {
                                mailMessage.Bcc.Add(item);
                            }
                            //SET FROM ADDRESS
                            mailMessage.From = new MailAddress(this.FromAddress);
                            //PROCESS THE SUBJECT AND BODY
                            mailMessage.Subject    = velocityEngine.Process(vendorParameters, this.Subject);
                            mailMessage.Body       = velocityEngine.Process(vendorParameters, this.Body);
                            mailMessage.IsBodyHtml = this.IsHTML;
                            messages.Add(mailMessage);
                        }
                        else
                        {
                            Logger.Warn("Could not process vendor notification for '" + vendor.Name + "'; email address is invalid.");
                        }
                    }
                }
            }

            //RETURN THE GENERATED MAIL MESSAGE
            if (messages.Count == 0)
            {
                return(null);
            }
            return(messages.ToArray());
        }