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); }
} = 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); }
/// <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); }