示例#1
0
        public RenderedComm RenderComm(Person person)
        {
            RenderedComm result = new RenderedComm();

            result[CommRenderPart.Subject]    = GetSubject();
            result[CommRenderPart.BodyText]   = GetBody();
            result[CommRenderPart.SenderName] = "Swarmops Administrative";
            result[CommRenderPart.SenderMail] = "*****@*****.**";

            return(result);
        }
        public void Transmit(PayloadEnvelope envelope, Person person)
        {
            if (envelope.PayloadClass != "Swarmops.Logic.Communications.Transmission.NotificationPayload")
            {
                throw new NotImplementedException();
            }

            ICommsRenderer renderer = NotificationPayload.FromXml(envelope.PayloadXml);

            RenderedComm comm = renderer.RenderComm(person);

            MailMessage mail = new MailMessage();

            // This is a rather simple mail (no images or stuff like that)

            mail.From = new MailAddress(comm[CommRenderPart.SenderMail], comm[CommRenderPart.SenderName], Encoding.UTF8);
            mail.To.Add(new MailAddress(person.Mail, person.Name));

            mail.Subject         = comm[CommRenderPart.Subject];
            mail.Body            = comm[CommRenderPart.BodyText];
            mail.SubjectEncoding = Encoding.UTF8;
            mail.BodyEncoding    = Encoding.UTF8;

            string smtpServer = _smtpServerCache;

            DateTime now = DateTime.Now;

            if (now > _cacheReloadTime)
            {
                smtpServer       = _smtpServerCache = Persistence.Key ["SmtpServer"];
                _cacheReloadTime = now.AddMinutes(5);
            }

            if (string.IsNullOrEmpty(smtpServer))
            {
                smtpServer       = "192.168.80.204"; // For development use only - invalidate cache instead of this, forcing re-reload
                _cacheReloadTime = DateTime.MinValue;
            }

            SmtpClient mailClient = new SmtpClient(smtpServer);

            // TODO: SMTP Server login credentials

            try
            {
                mailClient.Send(mail);
            }
            catch (Exception e)
            {
                _cacheReloadTime = DateTime.MinValue;
                throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
            }
        }
示例#3
0
        public RenderedComm RenderComm(Person person)
        {
            string culture = person.PreferredCulture;

            if (string.IsNullOrEmpty(culture))
            {
                culture = "en-US";
            }

            RenderedComm result = new RenderedComm();

            result[CommRenderPart.Subject]    = GetSubject(culture);
            result[CommRenderPart.BodyText]   = GetBody(culture);
            result[CommRenderPart.SenderName] = SystemSettings.AdminNotificationSender; // TODO: Make dependent on an enum
            result[CommRenderPart.SenderMail] = SystemSettings.AdminNotificationAddress;

            return(result);
        }
        public RenderedComm RenderComm(Person person)
        {
            string body    = BodyTemplate;
            string subject = SubjectTemplate;

            // If the template contains "[RandomPassword]", set the person's pwd to random and replace macro

            if (body.Contains("[RandomPassword]"))
            {
                string randomPassword = Authentication.CreateWeakSecret(24);
                person.SetPassword(randomPassword);
                body = body.Replace("[RandomPassword]", randomPassword);
            }

            // Replace each and every marker in template from lookup table

            foreach (
                MailPayloadString replacementString in (MailPayloadString[])Enum.GetValues(typeof(MailPayloadString))
                )
            {
                if (Strings.ContainsKey(replacementString))
                {
                    body = body.Replace("[" + replacementString.ToString() + "]",
                                        Strings[replacementString]);
                    subject = subject.Replace("[" + replacementString.ToString() + "]",
                                              Strings[replacementString]);
                }
            }

            if (!subject.StartsWith("[") && Strings.ContainsKey(MailPayloadString.OrganizationName))
            {
                // This is a little defensive programming as some subjects start with [Organization] and some don't - fix that here
                subject = "[" + Strings[MailPayloadString.OrganizationName] + "] " + subject;
            }

            RenderedComm result = new RenderedComm();

            result[CommRenderPart.BodyText]   = body;
            result[CommRenderPart.Subject]    = subject;
            result[CommRenderPart.SenderMail] = "*****@*****.**"; // TODO: FIX FIX FIX HACK
            result[CommRenderPart.SenderName] = "Swarmops Administrative";

            return(result);
        }
        public RenderedComm RenderComm(Person person)
        {
            string body    = BodyTemplate;
            string subject = SubjectTemplate;

            // If the template contains "[RandomPassword]", set the person's pwd to random and replace macro

            if (body.Contains("[RandomPassword]"))
            {
                string randomPassword = Authentication.CreateRandomPassword(24);
                person.SetPassword(randomPassword);
                body = body.Replace("[RandomPassword]", randomPassword);
            }

            // Replace each and every marker in template from lookup table

            foreach (
                MailPayloadString replacementString in (MailPayloadString[])Enum.GetValues(typeof(MailPayloadString))
                )
            {
                if (Strings.ContainsKey(replacementString))
                {
                    body = body.Replace("[" + replacementString.ToString() + "]",
                                        Strings[replacementString]);
                    subject = subject.Replace("[" + replacementString.ToString() + "]",
                                              Strings[replacementString]);
                }
            }

            RenderedComm result = new RenderedComm();

            result[CommRenderPart.BodyText]   = body;
            result[CommRenderPart.Subject]    = subject;
            result[CommRenderPart.SenderMail] = "*****@*****.**"; // TODO: FIX FIX FIX HACK
            result[CommRenderPart.SenderName] = "Swarmops Administrative";

            return(result);
        }
        public RenderedComm RenderComm(Person person)
        {
            string culture = person.PreferredCulture;

            if (string.IsNullOrEmpty(culture) || culture == "af-ZA")  // the af-ZA is the virtual in-place translator code, which is gibberish
            {
                culture = "en-US";
            }

            RenderedComm result = new RenderedComm();

            result[CommRenderPart.Subject]    = GetSubject(culture);
            result[CommRenderPart.BodyText]   = GetBody(culture);
            result[CommRenderPart.SenderName] = SystemSettings.AdminNotificationSender; // TODO: Make dependent on an enum
            result[CommRenderPart.SenderMail] = SystemSettings.AdminNotificationAddress;

            if (Strings.ContainsKey(NotificationString.OrganizationName))
            {
                result[CommRenderPart.Subject] = result[CommRenderPart.Subject];
            }

            return(result);
        }
示例#7
0
        public void Transmit(PayloadEnvelope envelope, Person person)
        {
            // Create the renderer via reflection of the static FromXml method

            Assembly assembly = typeof(PayloadEnvelope).Assembly;

            Type payloadType = assembly.GetType(envelope.PayloadClass);

            if (payloadType == null)
            {
                NotificationCustomStrings customStrings = new NotificationCustomStrings();
                customStrings["UnrecognizedPayloadType"] = envelope.PayloadClass;
                OutboundComm.CreateNotification(null, NotificationResource.System_UnrecognizedPayload, customStrings);

                throw new OutboundCommTransmitException("Unrecognized or uninstantiable payload type: " + envelope.PayloadClass);
            }

            var methodInfo = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

            ICommsRenderer renderer = (ICommsRenderer)(methodInfo.Invoke(null, new object[] { envelope.PayloadXml }));
            RenderedComm   comm     = renderer.RenderComm(person);

            MailMessage mail = new MailMessage();

            // This is a rather simple mail (no images or stuff like that)

            mail.Subject         = (string)comm[CommRenderPart.Subject];
            mail.SubjectEncoding = Encoding.UTF8;

            try
            {
                mail.From = new MailAddress((string)comm[CommRenderPart.SenderMail], (string)comm[CommRenderPart.SenderName],
                                            Encoding.UTF8);

                // SPECIAL CASE for sandbox mails -- ugly code but wtf
                if (person.Identity == 1 && PilotInstallationIds.DevelopmentSandbox == SystemSettings.InstallationId && mail.Subject.Contains("|"))
                {
                    string[] separated = mail.Subject.Split('|');
                    mail.Subject = separated[0];
                    mail.To.Add(new MailAddress(separated[1], "Swarmops Sandbox Administrator"));
                }
                else // regular case to be used... like everywhere else except for the sandbox test
                {
                    if (string.IsNullOrWhiteSpace(person.Mail))
                    {
                        throw new OutboundCommTransmitException("No valid mail address for " + person.Canonical);
                    }

                    mail.To.Add(new MailAddress(person.Mail, person.Name));
                }
            }
            catch (ArgumentException e)
            {
                // Address failure -- either sender or recipient

                _cacheReloadTime = Constants.DateTimeLow;
                throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
            }

            string mailBodyText = (string)comm[CommRenderPart.BodyText];

            mailBodyText = mailBodyText.Replace("[Addressee]", person.Canonical);

            string mailBodyHtml = comm.ContainsKey(CommRenderPart.BodyHtml)? (string)comm[CommRenderPart.BodyHtml]: string.Empty;

            mailBodyHtml = mailBodyHtml.Replace("[Addressee]", person.Canonical);

            mail.Body         = mailBodyText;
            mail.BodyEncoding = Encoding.UTF8;

            string smtpServer   = _smtpServerCache;
            int    smtpPort     = _smtpPortCache;
            string smtpUser     = _smtpUserCache;
            string smtpPassword = _smtpPasswordCache;

            DateTime now = DateTime.Now;

            if (now > _cacheReloadTime)
            {
                smtpServer   = _smtpServerCache = SystemSettings.SmtpHost;
                smtpPort     = _smtpPortCache = SystemSettings.SmtpPort;
                smtpUser     = _smtpUserCache = SystemSettings.SmtpUser;
                smtpPassword = _smtpPasswordCache = SystemSettings.SmtpPassword;

                _cacheReloadTime = now.AddMinutes(5);
            }

            if (string.IsNullOrEmpty(smtpServer))
            {
                smtpServer = "localhost";
                smtpPort   = 25;
                // For development use only - invalidate cache instead of this, forcing re-reload
                _cacheReloadTime = Constants.DateTimeLow;
            }

            SmtpClient mailClient = new SmtpClient(smtpServer, smtpPort);

            if (!string.IsNullOrEmpty(smtpUser))
            {
                mailClient.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword);
            }

            try
            {
                mailClient.Send(mail);
            }
            catch (Exception e)
            {
                _cacheReloadTime = Constants.DateTimeLow;
                throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
            }
        }
示例#8
0
        public void Transmit(PayloadEnvelope envelope, Person person)
        {
            // Create the renderer via reflection of the static FromXml method

            Assembly assembly = typeof(PayloadEnvelope).Assembly;

            Type payloadType = assembly.GetType(envelope.PayloadClass);
            var  methodInfo  = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

            ICommsRenderer renderer = (ICommsRenderer)(methodInfo.Invoke(null, new object[] { envelope.PayloadXml }));
            RenderedComm   comm     = renderer.RenderComm(person);

            MailMessage mail = new MailMessage();

            // This is a rather simple mail (no images or stuff like that)

            try
            {
                mail.From = new MailAddress((string)comm[CommRenderPart.SenderMail], (string)comm[CommRenderPart.SenderName],
                                            Encoding.UTF8);
                mail.To.Add(new MailAddress(person.Mail, person.Name));
            }
            catch (ArgumentException e)
            {
                // Address failure -- either sender or recipient

                _cacheReloadTime = DateTime.MinValue;
                throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
            }

            mail.Subject         = (string)comm[CommRenderPart.Subject];
            mail.Body            = (string)comm[CommRenderPart.BodyText];
            mail.SubjectEncoding = Encoding.UTF8;
            mail.BodyEncoding    = Encoding.UTF8;

            string smtpServer   = _smtpServerCache;
            int    smtpPort     = _smtpPortCache;
            string smtpUser     = _smtpUserCache;
            string smtpPassword = _smtpPasswordCache;

            DateTime now = DateTime.Now;

            if (now > _cacheReloadTime)
            {
                smtpServer   = _smtpServerCache = SystemSettings.SmtpHost;
                smtpPort     = _smtpPortCache = SystemSettings.SmtpPort;
                smtpUser     = _smtpUserCache = SystemSettings.SmtpUser;
                smtpPassword = _smtpPasswordCache = SystemSettings.SmtpPassword;

                _cacheReloadTime = now.AddMinutes(5);
            }

            if (string.IsNullOrEmpty(smtpServer))
            {
                smtpServer = "localhost";
                smtpPort   = 25;
                // For development use only - invalidate cache instead of this, forcing re-reload
                _cacheReloadTime = DateTime.MinValue;
            }

            SmtpClient mailClient = new SmtpClient(smtpServer, smtpPort);

            if (!string.IsNullOrEmpty(smtpUser))
            {
                mailClient.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword);
            }

            try
            {
                mailClient.Send(mail);
            }
            catch (Exception e)
            {
                _cacheReloadTime = DateTime.MinValue;
                throw new OutboundCommTransmitException("Cannot send mail to " + person.Mail, e);
            }
        }