public static string ReplaceTokens(string emailBody, EmailMessageTokens tokens) { //If no tokens provided for replacement, return the email body as it is. if (tokens.Count == 0) { return(emailBody); } foreach (EmailMessageToken token in tokens) { emailBody = emailBody.Replace(token.TokenName, token.TokenValue); } return(emailBody); }
protected void SendShippingNoticeEmails(EmailNotificationFormModel model) { MessageBL MsgBL = new MessageBL(); TokenBL tBL = new TokenBL(); List<EmailAttachment> ListWithID = new List<EmailAttachment>(); int GID = 0; string Token = string.Empty; int billType = Convert.ToInt32(Session["mailerSelectedBillType"]); foreach (var mailItem in model.Items) { if (mailItem.IsSelected) { //Create File Group and get the GID GID = MsgBL.CreateFileAttachmentGroup(User.Identity.Name, model.BillNumber, billType); tBL.CreateToken(ref Token, TokenType.Shipping_Notice, mailItem.RecipientEmail, true, AppConstants.TOKEN_PERIOD_FILE_ACCESS); string Attachments = "<ul>"; foreach (var attachment in mailItem.Attachments) { if (attachment.FileID == 0 && attachment.IsSelected) { //Generate PDF files GeneratePDFFiles(attachment, ListWithID); MsgBL.LogFileAttachment(attachment.FileName, attachment.FileID, GID, mailItem.RecipientEmail); Attachments += "<li><a href='" + ConfigurationManager.AppSettings["URL.MailerRedirect.FileViewer"] + "?GID=" + GID + "&FileID=" + attachment.FileID + "&Token=" + Token + "&UserEmail=" + mailItem.RecipientEmail + "'>" + attachment.FileName + "</a></li>"; } } Attachments += "</ul>"; //Create Links //Create a Token //GET MessageTemplate string emailBody = Email.GetEmailTemplate(Server.MapPath(EmailConstants.PATH_TEMPLATE_SHIPPING_NOTICE)); string emailSubject = EmailConstants.SUBJECT_SHIPPING_NOTICE; if (!string.IsNullOrEmpty(mailItem.Subject)) { emailSubject = mailItem.Subject; } EmailMessageTokens tokens = new EmailMessageTokens(); var eltuser = GetCurrentELTUser(); string sender = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(eltuser.user_fname) + " " + CultureInfo.InvariantCulture.TextInfo.ToTitleCase(eltuser.user_lname); if (string.IsNullOrEmpty(sender.Trim())) sender = User.Identity.Name; tokens.Add(Email.SetEmailMessageToken("CompanyName", eltuser.company_name)); tokens.Add(Email.SetEmailMessageToken("SenderName", sender)); tokens.Add(Email.SetEmailMessageToken("BillNo", model.BillNumber)); tokens.Add(Email.SetEmailMessageToken("MessageBody", mailItem.TextMessage)); tokens.Add(Email.SetEmailMessageToken("Attachments", Attachments)); //Swap Tokens with data emailBody = Email.ReplaceTokens(emailBody, tokens); //Store Message string Folder = "Shipping Notice"; MsgBL.AddMessage(emailSubject, User.Identity.Name, mailItem.RecipientEmail, emailBody, Folder, false); //Store to Sent Itmes Folder = " Sent Items"; MsgBL.AddMessage(emailSubject, User.Identity.Name, mailItem.RecipientEmail, emailBody, Folder, false); //Send email //Get all the CC list List<string> CCList = new List<string>(); if (!string.IsNullOrEmpty(mailItem.CC)) { mailItem.CC = mailItem.CC.Replace(',', ';'); var ccs = mailItem.CC.Split(','); CCList.AddRange(ccs); } try { Email.SendSmtp(mailItem.RecipientEmail, emailSubject, emailBody, true, User.Identity.Name); ViewBag.Message = "Email Sent Successfully!"; } catch (Exception ex) { ViewBag.Message = ex.Message; } } } }