/// <summary> /// Envoyer Email (d'une manière asynchrone) avec un template dans le dossier du template (appsettings EmailTemplatesFolder dans le web.config) /// </summary> /// <param name="model">Le model de l'email</param> /// <param name="template">Le nom complet du template. Ne pas oublier de déployer le dossier du template avec l'application</param> public void SendEmail(EmailModelBase model, string template) { AsyncMethodCaller caller = new AsyncMethodCaller(SendMailInSeperateThread); AsyncCallback callbackHandler = new AsyncCallback(AsyncCallback); caller.BeginInvoke(model, template, callbackHandler, null); }
/// <summary> /// Gets info about changed fields and other attributes for particular user (if available). /// </summary> private string GetChangedFieldsInfoForUser( [NotNull] EmailModelBase model, [NotNull] User user) { if (!(model is IEmailWithUpdatedFieldsInfo mailWithFields)) { return(""); } //Add project fields that user has right to view var accessArguments = mailWithFields.FieldsContainer.GetAccessArguments(user.UserId); IEnumerable <MarkdownString> fieldString = mailWithFields .UpdatedFields .Where(f => f.HasViewAccess(accessArguments)) .Select(updatedField => new MarkdownString( $@"__**{updatedField.Field.FieldName}:**__ {MarkdownTransformations.HighlightDiffPlaceholder(updatedField.DisplayString, updatedField.PreviousDisplayString).Contents}")); //Add info about other changed atttributes (no access rights validation) IEnumerable <MarkdownString> otherAttributesStrings = mailWithFields .OtherChangedAttributes .Select(changedAttribute => new MarkdownString( $@"__**{changedAttribute.Key}:**__ {MarkdownTransformations.HighlightDiffPlaceholder(changedAttribute.Value.DisplayString, changedAttribute.Value.PreviousDisplayString).Contents}")); return(string.Join( "\n\n", otherAttributesStrings .Union(fieldString) .Select(x => x.ToHtmlString()))); }
/// <summary> /// Gets info about changed fields and other attributes for particular user (if available). /// </summary> private string GetChangedFieldsInfoForUser( [NotNull] EmailModelBase model, [NotNull] User user) { IEmailWithUpdatedFieldsInfo mailWithFields = model as IEmailWithUpdatedFieldsInfo; if (mailWithFields == null) { return(""); } //Add project fields that user has right to view Predicate <FieldWithValue> accessRightsPredicate = CustomFieldsExtensions.GetShowForUserPredicate(mailWithFields.FieldsContainer, user.UserId); IEnumerable <MarkdownString> fieldString = mailWithFields .UpdatedFields .Where(f => accessRightsPredicate(f)) .Select(updatedField => new MarkdownString( $@"__**{updatedField.Field.FieldName}:**__ {MarkDownHelper.HighlightDiffPlaceholder(updatedField.DisplayString, updatedField.PreviousDisplayString).Contents}")); //Add info about other changed atttributes (no access rights validation) IEnumerable <MarkdownString> otherAttributesStrings = mailWithFields .OtherChangedAttributes .Select(changedAttribute => new MarkdownString( $@"__**{changedAttribute.Key}:**__ {MarkDownHelper.HighlightDiffPlaceholder(changedAttribute.Value.DisplayString, changedAttribute.Value.PreviousDisplayString).Contents}")); return(string.Join( "\n\n", otherAttributesStrings .Union(fieldString) .Select(x => x.ToHtmlString()))); }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async Task SendEmailAsync(EmailModelBase email) #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { using (var client = new SmtpClient(this.emailSettings.SmtpHost)) { client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(this.emailSettings.SmtpUserName, this.emailSettings.SmtpPassword); client.Port = this.emailSettings.SmtpPort; client.EnableSsl = this.emailSettings.SmtpEnableSsl; client.DeliveryFormat = SmtpDeliveryFormat.International; client.Send(ToMailMessage(email)); } }
/// <summary> /// Use this method when no additional parameters are needed for users /// </summary> public static async Task SendEmail(this IEmailSendingService emailSendingService, EmailModelBase model, string subject, string body) { var projectEmailEnabled = model.GetEmailEnabled(); if (!projectEmailEnabled) { return; } var recipients = model.GetRecipients(); await emailSendingService.SendEmails(subject, new MarkdownString(body), model.Initiator.ToRecepientData(), recipients.Select(r => r.ToRecepientData()).ToList()); }
private MailMessage ToMailMessage(EmailModelBase model) { if (!model.IsValid) { throw new ValidationException(); } var ret = new MailMessage(); var displayName = string.IsNullOrEmpty(model.DisplayName) ? model.From : model.DisplayName; ret.From = new MailAddress(model.From, displayName); ret.IsBodyHtml = true; model.To.ForEach(to => ret.To.Add(to)); ret.Body = model.Body; ret.Subject = model.Subject; return(ret); }
public static string GetClaimEmailTitle(this EmailModelBase model, Claim claim) => $"{model.ProjectName}: {claim.Name}, игрок {claim.Player.GetDisplayName()}";
public static List <User> GetRecipients(this EmailModelBase model) => model.Recipients.Where(u => u != null && u.UserId != model.Initiator.UserId).Distinct().ToList();
public static bool GetEmailEnabled(this EmailModelBase model) => !model.ProjectName.Trim().StartsWith("NOEMAIL");
private bool SendMailInSeperateThread(EmailModelBase model, string template) { SmtpClient smtpClient = null; MailMessage email = null; try { // Generate an email using a dynamic model String myEmailkey = Guid.NewGuid().ToString(); var emailHtmlBody = RunCompile(TemplateFolderPath, template, myEmailkey, model); // Send the email email = new MailMessage() { Body = emailHtmlBody, IsBodyHtml = true, Subject = model.Subject }; email.From = new MailAddress(model.FromEmail, model.FromName); email.To.Add(new MailAddress(model.ToEmail, model.FullName)); foreach (var item in model.Attachments) { email.Attachments.Add(item); } smtpClient = new SmtpClient(_smtp); smtpClient.Port = _portSmtp; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.UseDefaultCredentials = false; System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(_sender, _password); smtpClient.EnableSsl = true; smtpClient.Timeout = _timeout; smtpClient.Credentials = credentials; smtpClient.Send(email); return(true); } catch (Exception ex) { _logger.LogError(ex, "Error sending email"); return(false); } finally { if (smtpClient != null) { smtpClient.Dispose(); } if (email != null) { email.Dispose(); } } }
public static List <User> GetRecepients(this EmailModelBase model) { return(model.Recepients.Where(u => u.UserId != model.Initiator.UserId).ToList()); }