Exemple #1
0
        public static void SendICalEmail(IICalendar iCal, MailMessage message, TenantEmailSetting emailServerSettings)
        {
            /////
            // Create an instance of the iCalendar serializer.
            /////
            var serializer = new iCalendarSerializer( );

            /////
            // Determine whether an alternate view of type HTML is required.
            /////
            if (message.IsBodyHtml && !string.IsNullOrEmpty(message.Body))
            {
                /////
                // Create the html content type.
                /////
                var htmlContentType = new ContentType(MediaTypeNames.Text.Html);

                /////
                // Add the html alternate view.
                /////
                AlternateView htmlAlternateView = AlternateView.CreateAlternateViewFromString(message.Body, htmlContentType);

                message.AlternateViews.Add(htmlAlternateView);
            }

            /////
            // Create the calendar content type.
            /////
            var calendarContentType = new ContentType("text/calendar");

            if (calendarContentType.Parameters != null)
            {
                calendarContentType.Parameters.Add("method", "REQUEST");
            }

            /////
            // Add the calendar alternate view.
            /////
            AlternateView calendarAlternateView = AlternateView.CreateAlternateViewFromString(serializer.SerializeToString(iCal), calendarContentType);

            message.AlternateViews.Add(calendarAlternateView);

            /////
            // Get a list of MailMessage instances.
            /////
            var messages = new List <MailMessage>
            {
                message
            };

            var emailsender = new SmtpEmailSender(emailServerSettings);

            emailsender.SendMessages(messages);
        }
        public void TenantEmailServerSettingUpgrade()
        {
            var emailSettings = new TenantEmailSetting();
            var oldSecret     = "Test" + DateTime.UtcNow.Ticks;

            emailSettings.SmtpPassword = _crypto.EncryptAndEncode(oldSecret);

            using (var scope = Factory.Current.BeginLifetimeScope(builder => builder.RegisterType <SecuredDataSaveHelper.DisabledSaveHelper>().As <ISecuredDataSaveHelper>()))
                using (Factory.SetCurrentScope(scope))
                {
                    emailSettings.Save();
                }

            SecureDataUpgradeHelper.Upgrade("EDC");

            var secureId = emailSettings.SmtpPasswordSecureId;

            Assert.That(secureId, Is.Not.Null);
            var secret = Factory.SecuredData.Read((Guid)secureId);

            Assert.That(secret, Is.EqualTo(oldSecret));
        }
        /// <summary>
        ///     Sends the email.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        ///     emailEntity
        ///     or
        ///     iCal
        ///     or
        ///     inbox
        /// </exception>
        private static void SendEmail(SentEmailMessage emailEntity, iCalendar iCal, TenantEmailSetting emailServerSettings)
        {
            if (emailEntity == null)
            {
                throw new ArgumentNullException("emailEntity");
            }

            if (iCal == null)
            {
                throw new ArgumentNullException("iCal");
            }

            if (emailServerSettings == null)
            {
                throw new ArgumentNullException("emailServerSettings");
            }


            /////
            // Send the email.
            /////
            iCalEmailHelper.SendICalEmail(iCal, emailEntity.ToMailMessage( ), emailServerSettings);
        }
        /// <summary>
        ///     Generates the email.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        ///     eventEntity
        ///     or
        ///     inbox
        ///     or
        ///     iCal
        /// </exception>
        private static SentICalEmailMessage GenerateEmail(Appointment appointment, TenantEmailSetting emailServerSettings, iCalendar iCal)
        {
            if (appointment.ICalAppt == null)
            {
                throw new ArgumentNullException("eventEntity");
            }

            if (emailServerSettings == null)
            {
                throw new ArgumentNullException("emailServerSettings");
            }

            if (iCal == null)
            {
                throw new ArgumentNullException("iCal");
            }

            /////
            // Create the new email message.
            /////
            var emailMessage = Entity.Create <SentICalEmailMessage>( );

            emailMessage.Name = GlobalStrings.SoftwarePlatformCalendarInvite;

            /////
            // Body.
            /////
            if (appointment.Description != null)
            {
                emailMessage.EmBody = appointment.Description;
            }

            /////
            // From.
            /////
            emailMessage.EmFrom = emailServerSettings.EmailNoReplyAddress;               // This will need to be chagned to something else

            /////
            // Is Html.
            /////
            emailMessage.EmIsHtml = true;

            /////
            // Send date.
            /////
            emailMessage.EmSentDate = DateTime.UtcNow;

            /////
            // Subject.
            /////
            emailMessage.EmSubject = !string.IsNullOrEmpty(appointment.Name) ? string.Format(GlobalStrings.ValidCalendarEmailSubject, appointment.Name) : GlobalStrings.InvalidCalendarEmailSubject;

            /////
            // Indicate that the event was cancelled on the subject line.
            /////
            if (iCal.Method == Methods.Cancel)
            {
                emailMessage.EmSubject = string.Format("{0}: {1}", GlobalStrings.Cancelled, emailMessage.EmSubject);
            }

            /////
            // Attendees.
            /////
            var attendees = new List <string>( );

            /////
            // To add the organizer as a recipient of the email, uncomment the following line.
            /////
            //attendees.Add( "Software Platform " + inbox.InboxEmailAddress );

            var attendeesEmails = GetAttendeesAddresses(appointment.EventEmailAttendees);

            attendees.AddRange(attendeesEmails.Select(a => string.Format("{0} {1}", a.EmailContactDisplayName, a.Name).Trim()));

            /////
            // Process the attendees.
            /////
            if (appointment.EventEmailAttendees != null)
            {
                emailMessage.EmTo = string.Join(";", attendees.ToArray( ));
            }

            ICalendarObjectList <ITimeZone> timeZones = iCal.TimeZones;

            /////
            // Store the time zone id so that any new proposed times will exist in the same time zone.
            /////
            if (timeZones != null)
            {
                ITimeZone timeZone = timeZones.FirstOrDefault( );

                if (timeZone != null)
                {
                    emailMessage.ICalTimeZone = timeZone.TzId;
                }
            }

            /////
            // Store a reference to the original event email entity.
            /////
            emailMessage.OwnerEventEmail = appointment.ICalAppt;

            emailMessage.SentFromEmailServer = emailServerSettings;

            emailMessage.Save( );

            return(emailMessage);
        }
 public SmtpEmailSender(TenantEmailSetting emailServerSettings)
 {
     _emailServerSettings = emailServerSettings;
 }