/// <summary> /// Build VMTA Mail header /// </summary> /// <param name="campaignId"></param> /// <param name="subject"></param> /// <param name="accountCode"></param> /// <param name="item"></param> /// <param name="returnPath"></param> /// <param name="unsubscribeLink"></param> /// <param name="fromName"></param> /// <param name="fromEmail"></param> /// <param name="vmtaEmail"></param> /// <param name="senderDomain"></param> /// <returns></returns> private static string BuildHeader(int campaignId, string subject, string accountCode, EmailRecipient item, string returnPath, string unsubscribeLink, string fromName, string fromEmail, string vmtaEmail, string senderDomain, byte?campaignTypeId) { var contentType = campaignTypeId == 132 ? "text/plain" : "text/html"; var mailType = "campaign"; Dictionary <string, string> utm_medium_sources = new Dictionary <string, string>(); utm_medium_sources.Add("1", "campaign"); utm_medium_sources.Add("2", "webinar"); utm_medium_sources.Add("3", "series"); utm_medium_sources.Add("4", "prospecting"); utm_medium_sources.Add("5", "reengagement"); utm_medium_sources.Add("6", "inventory"); utm_medium_sources.Each(s => { if (subject.Contains(s.Value)) { mailType = s.Value; } }); String headers = "Subject: " + subject + "\n" + "Content-Type: " + contentType + "; charset=\"utf-8\"\n" + "MIME-Version: 1.0" + "\n" + "X-Mailer: STMailer" + "\n" + "X-STCustomer: " + item.CampaignRecipientID.ToString() + "\n" + "X-Campaign: " + accountCode + "_" + campaignId + "\n" + "X-SearchCriteria: " + campaignId + "\n" + "List-Unsubscribe: " + "<" + unsubscribeLink + ">" + "\n" + "Return-Path: " + returnPath + "\n" + "Feedback-ID: " + campaignId.ToString() + ":" + mailType.ToString() + ":" + accountCode.ToString(); var includeVMTAEnvelope = ConfigurationManager.AppSettings["INCLUDE_VMTA_ENVELOPE"].ToString(); if (includeVMTAEnvelope == "YES") { headers = headers + "envelope-from: " + item.CampaignRecipientID.ToString() + "-" + accountCode + "_" + campaignId + "@bounce." + senderDomain + "\n"; } if (fromEmail == vmtaEmail) { headers = headers + "From: " + fromName + " <" + fromEmail + ">\n" + "To: " + item.EmailId + "\n" + "\n"; } else { headers = headers + "From: " + fromName + " <" + fromEmail + ">\n" + "To: " + item.EmailId + "\n" + "Sender: " + vmtaEmail + "\n" + "Reply-to: " + fromName + " <" + fromEmail + ">\n" + "\n"; } return(headers); }
/// <summary> /// Send Campaign through VMTA for test email /// </summary> /// <param name="campaignId"></param> /// <param name="campaignName"></param> /// <param name="contactTagIds"></param> /// <param name="searchDefinitionIds"></param> /// <param name="emails"></param> /// <param name="Companies">Obsolete</param> /// <param name="customFieldsValueOptions"></param> /// <param name="title"></param> /// <param name="subject"></param> /// <param name="content"></param> /// <param name="fromEmail"></param> /// <param name="fromName"></param> /// <param name="accountCode"></param> /// <param name="senderDomain"></param> /// <param name="accountDomain"></param> /// <param name="vmtaEmail"></param> /// <param name="accountAddress"></param> /// <param name="listName"></param> /// <param name="accountId"></param> /// <returns></returns> public IList <int> SendCampaignFromScheduler(int campaignId, string campaignName, List <int> contactTagIds, List <int> searchDefinitionIds, IEnumerable <EmailRecipient> emails , IEnumerable <Company> Companies, IEnumerable <FieldValueOption> customFieldsValueOptions, string title, string subject, string content, string fromEmail , string fromName, string accountCode, string senderDomain, string accountDomain, string vmtaEmail, string accountAddress, int accountId, byte?campaignTypeId, MailRegistrationDb mailRegistration, bool hasDisCliamer, string listName = null) { #region Variables Declaration var count = emails.Count(); string returnPath = string.Empty; EmailRecipient item = null; Message msg = null; IList <int> successfulRecipients = new List <int>(); #endregion var includeVMTAENVId = ConfigurationManager.AppSettings["INCLUDE_VMTA_ENVID"].ToString(); content = content.Replace("*|CAMPID|*", campaignId.ToString()).Replace("<o:p>", "").Replace("</o:p>", "");//.FormatHTML(); var contentType = campaignTypeId == 132 ? "text/plain" : "text/html"; var discliamarAccounts = ConfigurationManager.AppSettings["EXCLUDING_DISCLIAMAR_ACCOUNTS"].ToString(); bool accountFound = discliamarAccounts.Contains(accountId.ToString()); Connection con = new Connection(host, port, userName, password); if (con == null) { return(successfulRecipients); } var addressLine = "<p style='margin-bottom:2px;margin-top:2px;'> " + accountAddress + "</p>"; for (var i = 0; i < count; i++) { item = emails.ToArray()[i]; try { returnPath = item.CampaignRecipientID.ToString() + "-" + accountCode + "_" + campaignId + "@bounce." + senderDomain; msg = new Message(returnPath); if (msg == null) { continue; } msg.AddDateHeader(); msg.ReturnType = ReturnType.Headers; var index = mailRegistration.ImageDomain.IndexOf("//"); var dotCount = mailRegistration.ImageDomain.Count(d => d == '.'); var linkDomain = accountDomain; if (index >= 0 && dotCount == 1) { linkDomain = mailRegistration.ImageDomain.Insert(index + 2, accountCode + "."); } //var unsubscribeLink = "https://" + linkDomain + "/campaignUnsubscribe?crid=" + item.CampaignRecipientID.ToString() + "&acct=" + accountId; var unsubscribeLink = string.Format(linkDomain.IndexOf("http") >= 0 ? "{0}/campaignUnsubscribe?crid=[CRID]&acct={1}" : "https://{0}/campaignUnsubscribe?crid=[CRID]&acct={1}", linkDomain, accountId); String headers = BuildHeader(campaignId, subject, accountCode, item, returnPath, unsubscribeLink, fromName, fromEmail, vmtaEmail, senderDomain, campaignTypeId); if (includeVMTAENVId == "YES") { msg.EnvID = item.CampaignRecipientID.ToString(); } msg.Verp = true; msg.VirtualMTA = vmta; msg.JobID = accountCode + "/" + campaignId; msg.AddData(headers); var contentSb = new StringBuilder(content, content.Length * 2); contentSb.Replace("*|CID|*", item.ContactId.ToString()).Replace("*|CAMPID|*", campaignId.ToString()).Replace("*|CRID|*", item.CampaignRecipientID.ToString()).Replace("<o:p>", "").Replace("</o:p>", ""); var body = contentSb.ToString(); string mergedhtmlcontent = item.ContactFields != null?ReplaceMergeFields(body, item.ContactInfo, customFieldsValueOptions, null, item.ContactFields) : body; var unsubscribeLine = string.Empty; var recipientInfoLine = "<p style='margin-bottom:2px;margin-top:2px;'> This e-mail was sent to " + item.EmailId + " by " + fromName + " <" + fromEmail + ">. </p>"; if (accountFound && !hasDisCliamer) { unsubscribeLine = " <p style='margin-bottom:2px;margin-top:2px;'> <a href='" + unsubscribeLink + "' style='color: #808080;'>Click here to opt out from mailing list</a></p>"; } else { unsubscribeLine = " <p style='margin-bottom:2px;margin-top:2px;'> <a href='" + unsubscribeLink + "' style='color: #808080;'>Unsubscribe</a></p>"; } var htmlWrapper = "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html>\r\n" + "<head>\r\n" + "<meta name='robots' content='noindex'>\r\n" + "<meta http-equiv=\"Content-Type\" content=\"" + contentType + "; charset=utf-8\">\r\n<title></title>\r\n</head>\r\n<body>\r\n*|CONTENT|*\r\n</body>\r\n</html>"; var footer = "<div style='color: #808080; font-family: Arial, Verdana, Helvetica; font-size: 8pt; font-weight: bold;margin: auto;width: 80%;text-align: center;'>" + ((accountFound && !hasDisCliamer) ? "" : recipientInfoLine) + ((accountFound && !hasDisCliamer) ? "" : addressLine) + unsubscribeLine + "</div>"; mergedhtmlcontent = campaignTypeId == 132 ? body : htmlWrapper.Replace("*|CONTENT|*", mergedhtmlcontent + footer); msg.AddData(mergedhtmlcontent); String textHeaders = headers.Replace("Content-Type: text/html;", "Content-Type: text/plain;"); msg.BeginPart(2); msg.AddData(textHeaders); msg.AddData(mergedhtmlcontent); Recipient recipient = new Recipient(item.EmailId); msg.AddRecipient(recipient); SubmitWithTimeOut(con, msg, campaignId, subject); successfulRecipients.Add(item.CampaignRecipientID); } catch (Exception ex) { Logger.Current.Error("Unable to send campaign to: " + item.EmailId + ". Error:", ex); } } return(successfulRecipients); }