private static void SendMail(EmailParts ep) {//, List<EmailAttachment> attachments) { Email mail = new Email(); int smtpPort; if (String.IsNullOrEmpty(_EmailFrom)) { throw new Exception("Please provide an email address for the EmailFrom setting in the web.config file"); } int.TryParse(System.Configuration.ConfigurationManager.AppSettings["SMTPPort"], out smtpPort); mail.SMTPHost = System.Configuration.ConfigurationManager.AppSettings["SMTPHost"]; mail.SMTPPort = smtpPort; mail.FromAddress = _EmailFrom; mail.AddToAddresses(ep.ToAddresses); mail.AddCCAddresses(ep.CcAddresses); mail.AddBCCAddresses(ep.BccAddresses); mail.Subject = ep.Subject; mail.Body = ep.Body; //if a log of emails being sent from this application is beneficial, uncomment the following line and add any conditional checking if content security/privacy is of concern //LogEmailAttempt(ep); //if (attachments != null && attachments.Count > 0){ // mail.SendAttachment(attachments); //}else{ mail.SendMail(); //} }
public void GenerateReturnsRandomEmailAddressUsingFirstNameLastNameAndDomainOfContext() { var parts = new EmailParts { FirstName = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), LastName = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), Domain = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) }; var buildChain = new BuildHistory(); var executeStrategy = Substitute.For <IExecuteStrategy>(); executeStrategy.BuildChain.Returns(buildChain); buildChain.Push(parts); var sut = new Wrapper(); var actual = (string)sut.RunGenerate(typeof(string), "email", executeStrategy); var firstName = EmailValueGenerator.SpecialCharacters.Replace(parts.FirstName, string.Empty); var lastName = EmailValueGenerator.SpecialCharacters.Replace(parts.LastName, string.Empty); var expected = (firstName + "." + lastName + "@" + parts.Domain).ToLowerInvariant(); actual.Should().Be(expected); }
private static EmailParts GetEmailTemplate(string templateFile, string currentUser) { EmailParts ep = new EmailParts(); templateFile = System.Web.HttpContext.Current.Server.MapPath(_Path + templateFile); if (System.IO.File.Exists(templateFile)) { using (System.IO.StreamReader sr = new System.IO.StreamReader(templateFile)) { //ep.ToAddresses = sr.ReadLine ep.ToAddresses = string.Format("{0}@sandridgeenergy.com", currentUser); ep.CcAddresses = sr.ReadLine(); ep.BccAddresses = sr.ReadLine(); ep.Subject = sr.ReadLine(); ep.Template = sr.ReadToEnd().Replace("\r\n", "<br />"); //assumes this email to be in HTML format if (string.IsNullOrEmpty(ep.CcAddresses)) { //ensure the address is not an empty string ep.CcAddresses = null; } if (string.IsNullOrEmpty(ep.BccAddresses)) { //ensure the address is not an empty string ep.BccAddresses = null; } } return(ep); } return(null); }
public static void SendDemoEmail(string currentUser, List <EmployeeInformation> users) { //, List<EmailAttachment> attachments){ EmailParts ep = GetEmailTemplate("SampleEmail.txt", currentUser); ReplaceInfo(users, ep); // , attachments); SendMail(ep); }
private static void LogEmailAttempt(EmailParts ep) { string logLocation = System.Configuration.ConfigurationManager.AppSettings["LogLocation"]; if (!String.IsNullOrEmpty(logLocation)) { ErrorLogger.UriBase = logLocation; } ErrorLogger.AdditionalInformation = String.Format("From: {0}{1}To: {2}{3}CC: {4}{5}BCC: {6}{7}Subject: {8}{9}Body: {10}", ep.FromAddress, Environment.NewLine, ep.ToAddresses, Environment.NewLine, ep.CcAddresses, Environment.NewLine, ep.BccAddresses, Environment.NewLine, ep.Subject, Environment.NewLine, ep.Body); ErrorLogger.LogInformation(System.Configuration.ConfigurationManager.AppSettings["AppName"], "Email"); }
/// <summary> /// Get the parts of a email, e.g. "com" or "co.uk" /// </summary> public EmailParts ParseEmail(string email) { if (!initialised) { throw new Exception("Domain parser not initialised"); } if (email == null) { throw new ArgumentNullException(nameof(email)); } int index = email.IndexOf("@", StringComparison.Ordinal); if (index < 0) { throw new ArgumentException("Email does not contain '@'"); } string domainPart = email.Substring(index + 1); string localPart = email.Substring(0, index); string[] domainParts = domainPart.Split('.'); for (int pos = 0; pos < domainParts.Length; pos++) { string tld = string.Join(".", domainParts, pos, domainParts.Length - pos); if (suffixes.Contains(tld)) { UrlSuffix s = suffixes[tld]; if (s.Starred) { pos--; tld = string.Join(".", domainParts, pos, domainParts.Length - pos); } pos--; string domain = string.Join(".", domainParts, pos, domainParts.Length - pos); EmailParts result = new EmailParts(localPart, domainPart, domain, tld); return(result); } } throw new ArgumentException("Suffix not found for: " + email); }
public void GenerateReturnsRandomEmailAddressUsingDomainOfContext() { var parts = new EmailParts { Domain = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) }; var buildChain = new BuildHistory(); var executeStrategy = Substitute.For <IExecuteStrategy>(); executeStrategy.BuildChain.Returns(buildChain); buildChain.Push(parts); var sut = new Wrapper(); var actual = (string)sut.RunGenerate(typeof(string), "email", executeStrategy); actual.Should().EndWith(parts.Domain); }
private static void ReplaceInfo(List <EmployeeInformation> users, EmailParts ep) { System.Text.StringBuilder emps = new System.Text.StringBuilder(); emps.Append("<ul>"); foreach (var u in users) { emps.AppendFormat("<li>{0} {1}</li>", u.FirstName, u.LastName); } emps.Append("</ul>"); if (DateTime.Now.Day % 2 == 0) { ep.Template = ReplaceBlockText(ep.Template, GetBlockTextContents(ep.Template, "<EVENDATES>", "</EVENDATES>"), "<EVENDATES>", "</EVENDATES>"); ep.Template = ReplaceBlockText(ep.Template, string.Empty, "<ODDDATES>", "</ODDDATES>"); } else { ep.Template = ReplaceBlockText(ep.Template, string.Empty, "<EVENDATES>", "</EVENDATES>"); ep.Template = ReplaceBlockText(ep.Template, GetBlockTextContents(ep.Template, "<ODDDATES>", "</ODDDATES>"), "<ODDDATES>", "</ODDDATES>"); } ep.Subject = ep.Subject.Replace("{URL}", GetServer()); ep.Body = ep.Template.Replace("{Employees}", emps.ToString()); }