Example #1
0
        /// <summary>
        /// Send the e-mail.
        /// </summary>
        /// <param name="umbraco"></param>
        /// <param name="emailFrom"></param>
        /// <param name="emailFromName"></param>
        /// <param name="emailTo"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="emailCc"></param>
        /// <param name="emailBcc"></param>
        /// <param name="emailType"></param>
        /// <param name="addFiles"></param>
        /// <param name="attachments"></param>
        public static void SendEmail(
            this UmbracoHelper umbraco,
            string emailFrom,
            string emailFromName,
            string emailTo,
            string subject,
            string body,
            string emailCc      = "",
            string emailBcc     = "",
            EmailType?emailType = null,
            bool addFiles       = false,
            Dictionary <string, MemoryStream> attachments = null
            )
        {
            //Make the MailMessage and set the e-mail address.
            MailMessage message = new MailMessage();

            message.From = new MailAddress(emailFrom, emailFromName);

            //Split all the e-mail addresses on , or ;.
            char[] splitChar = { ',', ';' };

            //Remove all spaces.
            emailTo  = emailTo.Trim();
            emailCc  = emailCc.Trim();
            emailBcc = emailBcc.Trim();

            //Split emailTo.
            string[] toArray = emailTo.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
            foreach (string to in toArray)
            {
                //Check if the e-mail is valid.
                if (umbraco.IsValidEmail(to))
                {
                    //Loop through all e-mail addressen in toArray and add them in the to.
                    message.To.Add(new MailAddress(to));
                }
            }

            //Split emailCc.
            string[] ccArray = emailCc.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
            foreach (string cc in ccArray)
            {
                //Check if the e-mail is valid.
                if (umbraco.IsValidEmail(cc))
                {
                    //Loop through all e-mail addressen in ccArray and add them in the cc.
                    message.CC.Add(new MailAddress(cc));
                }
            }

            //Split emailBcc.
            string[] bccArray = emailBcc.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
            foreach (string bcc in bccArray)
            {
                //Check if the e-mail is valid.
                if (umbraco.IsValidEmail(bcc))
                {
                    //Loop through all e-mail addressen in bccArray and add them in the bcc.
                    message.Bcc.Add(new MailAddress(bcc));
                }
            }

            if (addFiles && attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    var att = new Attachment(attachment.Value, attachment.Key);
                    message.Attachments.Add(att);
                }
            }

            //Set the rest of the e-mail data.
            message.Subject         = subject;
            message.SubjectEncoding = Encoding.UTF8;
            message.Body            = body;
            message.BodyEncoding    = Encoding.UTF8;
            message.IsBodyHtml      = true;

            //Only send the email if there is a to address.
            if (message.To.Any())
            {
                if (emailType.HasValue)
                {
                    try
                    {
                        //Get the database.
                        var database = ApplicationContext.Current.DatabaseContext.Database;

                        //Create the email object and set all properties.
                        var email = new EmailDto()
                        {
                            Type      = emailType.Value.ToString(),
                            FromName  = emailFromName,
                            FromEmail = emailFrom,
                            ToEmail   = emailTo,
                            CCEmail   = emailCc,
                            BCCEmail  = emailBcc,
                            Date      = DateTime.Now,
                            Subject   = subject,
                            Message   = body
                        };

                        //Insert the email into the database.
                        database.Insert(email);
                    }
                    catch (Exception ex)
                    {
                        Umbraco.LogException <UmbracoHelper>(ex);
                    }
                }

                //Make the SmtpClient.
                SmtpClient smtpClient = new SmtpClient();

                //Send the e-mail.
                smtpClient.Send(message);
            }

            //Clear the resources.
            message.Dispose();
        }