public void Test()
        {
            string messageIdLocalPart;

            var sentMessage = new SentEmailMessage
            {
                EmTo      = "*****@*****.**",
                EmFrom    = "*****@*****.**",
                EmBody    = "body",
                EmIsHtml  = false,
                EmSubject = "subject"
            };

            sentMessage.Save( );

            messageIdLocalPart = sentMessage.SemSequenceNumber;

            var receivedMessage = new ReceivedEmailMessage
            {
                EmTo         = "*****@*****.**",
                EmFrom       = "*****@*****.**",
                EmSubject    = "Re: " + sentMessage.EmSubject,
                EmBody       = "body",
                EmIsHtml     = false,
                EmReferences = EmailHelper.GenerateMessageId(messageIdLocalPart, "localhost")
            };

            var matchAction = new MatchSentToReceivedEmailsAction( );

            Action postSaveAction;

            Assert.IsFalse(matchAction.BeforeSave(receivedMessage, out postSaveAction));
            Assert.IsNull(postSaveAction);
            Assert.IsNotNull(receivedMessage.OriginalMessage, "The original message has been found.");
            Assert.AreEqual(sentMessage.Id, receivedMessage.OriginalMessage.Id, "The original message has been set correctly.");
        }
Beispiel #2
0
        } = new EmailRouter();                                                // TODO: switch to DI

        public void Send(IEntity notifier, Notification notification, IEnumerable <IEntity> people, bool expectReply)
        {
            //
            // TODO: Refactor this and SendEmail
            // TODO: Deal with send failures
            // TODO: deal with replies
            //

            var sends = people.Select(p =>
            {
                var sendRecord = Entity.Create <SendRecord>();
                //receipt.NrReceiptToken = CryptoHelper.GetRandomPrintableString(16);               // TODO handle matching email responses
                sendRecord.SrToPerson         = p?.Cast <Person>();
                sendRecord.SendToNotification = notification;
                return(sendRecord);
            });

            var emailNotifier = notifier.Cast <EmailNotifier>();

            var inbox = notifier.GetRelationships("core:emailNotifierInbox").FirstOrDefault()?.As <Inbox>();

            if (inbox == null)
            {
                throw new ArgumentException("core:emailNotifierInbox");
            }

            var mail = new SentEmailMessage()
            {
                EmIsHtml = true, EmSentDate = DateTime.UtcNow,
            };

            var addresses = FetchAddresses(people, emailNotifier.EnEmailAddressExpression);

            sends = sends.Zip <SendRecord, string, SendRecord>(addresses, (send, address) =>
            {
                if (address == null)
                {
                    send.SrErrorMessage = MissingEmailAddressMessage;
                }
                return(send);
            });

            mail.EmTo = String.Join(",", addresses.Where(a => a != null));

            if (!string.IsNullOrEmpty(mail.EmTo))
            {
                mail.EmSubject = notifier.GetField <string>("core:enSubject");

                if (!string.IsNullOrEmpty(notification.NMessage))
                {
                    mail.EmBody = notification.NMessage;
                }

                var emailServerSettings = Entity.Get <TenantEmailSetting>("core:tenantEmailSettingsInstance");

                mail.EmFrom = inbox?.InboxEmailAddress;
                mail.SentFromEmailServer = emailServerSettings;
                mail.EmSentDate          = DateTime.UtcNow;
                mail.Save();

                var mailMessages = mail.Cast <SentEmailMessage>().ToMailMessage().ToEnumerable().ToList();

                var emailsender = new SmtpEmailSender(emailServerSettings);
                emailsender.SendMessages(mailMessages);
            }

            Entity.Save(sends);
        }
Beispiel #3
0
        /// <summary>
        ///     Send reset password Email.
        /// </summary>
        /// <param name="displayName">The display name.</param>
        /// <returns></returns>
        private bool SendEmail(UserAccount account, string email, string tenant)
        {
            bool successSentEmail = false;

            try
            {
                account = account.AsWritable <UserAccount>();

                //only check current account is available or not
                if (account == null)
                {
                    return(successSentEmail);
                }

                string key = EDC.Security.CryptoHelper.GetMd5Hash(account.Id.ToString());
                if (account.PasswordReset != null)
                {
                    account.PasswordReset.AsWritable <PasswordResetRecord>();
                    account.PasswordReset.Delete();
                }


                account.PasswordReset = new PasswordResetRecord {
                    PasswordResetKey = key, PasswordResetExpiry = DateTime.UtcNow.AddDays(1.0)
                };
                account.Save();

                EventLog.Application.WriteInformation("set password reset key {0}", key);

                var mail = new SentEmailMessage()
                {
                    EmIsHtml = true, EmSentDate = DateTime.UtcNow,
                };

                string accountFullName = account.AccountHolder.FirstName + " " + account.AccountHolder.LastName;
                if (accountFullName.Trim().Length == 0)
                {
                    accountFullName = account.AccountHolder.Name;
                }

                //string toMail = email;
                const string subject   = "Reset Password - ReadiNow";
                string       link      = string.Format("{0}#/{1}/?key={2}&type=reset", Request.Headers.Referrer.ToString(), tenant, key);
                string       mailBoday = string.Format("Hi {0} <br> <br> We've received a request to reset your account {1} password for email address {2} <br> If you didn't make the request, please contact your administrator. <br> Otherwise, you can reset your password using this link: {3} <br><br>Best Regards, <br>ReadiNow", accountFullName, account.Name, email, link);

                var emailServer = ReadiNow.Model.Entity.Get <TenantEmailSetting>("tenantEmailSettingsInstance", TenantEmailSetting.AllFields);

                if (!string.IsNullOrEmpty(emailServer.SmtpServer) && !string.IsNullOrEmpty(emailServer.EmailNoReplyAddress))
                {
                    EventLog.Application.WriteInformation("Send user {0} reset password email to account '{1}' by email '{2}'", accountFullName, account.Name, email);

                    var sentMessage = new SentEmailMessage()
                    {
                        EmTo                = email,
                        EmFrom              = emailServer.EmailNoReplyAddress,
                        EmSubject           = subject,
                        EmBody              = mailBoday,
                        EmIsHtml            = true,
                        EmSentDate          = DateTime.UtcNow,
                        SentFromEmailServer = emailServer
                    };
                    sentMessage.Save();

                    var sender = new SmtpEmailSender(emailServer);
                    sender.SendMessages(sentMessage.ToMailMessage().ToEnumerable().ToList());
                }

                successSentEmail = true;
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("Send Email: Unhandled internal exception: " + ex.ToString());
            }

            return(successSentEmail);
        }