/// <summary> /// Validates the rule /// </summary> /// <param name="routableMessage">The Message to be accepted</param> /// <returns>This rule will always return true. Use it when implementing a SMTP Relay to accept all messages and just route them another SMTP.</returns> public override bool Match(RoutableMessage routableMessage) { if (!String.IsNullOrEmpty(ReplaceMailFromAddress)) { routableMessage.MailFrom = new MailboxAddress(ReplaceMailFromAddress); } return(true); }
/// <summary> /// Checks if the <see cref="Regex">Regular Expression</see> defined on <see cref="RegexExpression"/> is valid. The comparison happens against the mail from. /// </summary> /// <param name="routableMessage">The Message to Check</param> /// <returns>A <see cref="bool"/> to inform whether the rule matches or not</returns> public override bool Match(RoutableMessage routableMessage) { // Get the MailFrom from the Message if (routableMessage.MailFrom == null) { return(false); } // Get the Domain of the Mail From return(Regex.Match(routableMessage.MailFrom.Address, RegexExpression).Success); }
/// <summary> /// Checks if the email sender belongs to the <see cref="Domain"/> /// </summary> /// <param name="routableMessage">The Message to Check</param> /// <returns>A <see cref="bool"/> to inform whether the rule matches or not</returns> public override bool Match(RoutableMessage routableMessage) { // Get the MailFrom from the Message if (routableMessage.MailFrom == null) { return(false); } // Get the Domain of the Mail From var mailFromContents = routableMessage.MailFrom.Address.Split('@'); if (mailFromContents.Length < 2) { return(false); } // Try to match rule return(mailFromContents[1].Trim().ToUpper() == Domain.Trim().ToUpper()); }
/// <summary> /// Validates the Rule /// </summary> /// <param name="routableMessage">Reference to the <see cref="MimeMessage"/></param> /// <returns>A <see cref="bool"/> to define whether the rule matches</returns> public abstract bool Match(RoutableMessage routableMessage);
/// <summary> /// Loads the Routable Message based on a file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static RoutableMessage LoadFromFile(string fileName) { // The Routable Message RoutableMessage routableMessage = new RoutableMessage(); routableMessage.FileName = fileName; try { // Creates a Stream with the remaining values MemoryStream messageStream = new MemoryStream(); // Read File using (FileStream fileStream = File.OpenRead(fileName)) { // Text File Line string line = ""; // Reader for the Text File using (StreamReader fileStreamReader = new StreamReader(fileStream, Encoding.GetEncoding(28592))) { // Writer for the Message Stream using (StreamWriter messageStreamWriter = new StreamWriter(messageStream, Encoding.GetEncoding(28592))) { // Flag to define whether the system is reading the header or not anymore bool inHeader = true; // Ensure the stream will have data messageStreamWriter.AutoFlush = true; while ((!fileStreamReader.EndOfStream) && (inHeader)) { line = fileStreamReader.ReadLine(); if (line.StartsWith(SMTPROUTER_HEADER)) { if ((!line.StartsWith(SMTPROUTER_HEADER_BEGIN)) && (!line.StartsWith(SMTPROUTER_HEADER_END))) { // Array with the contents string[] tempHeader = line.Split(':'); if (tempHeader.Length == 1) { throw new Exception($"Invalid Header: {line}"); } // Remove any special character from the string tempHeader[1] = SMTPRouter.Utils.RemoveSpecialCharacters(tempHeader[1]); // Check which type of header line is being processed if ((line.StartsWith(SMTPROUTER_HEADER_FROM)) || (line.StartsWith(SMTPROUTER_HEADER_TO))) { // Try to parse the address, if valid then add it to the list if (MailboxAddress.TryParse(tempHeader[1], out MailboxAddress _newMailboxAddress)) { if (line.StartsWith(SMTPROUTER_HEADER_FROM)) { routableMessage.MailFrom = _newMailboxAddress; } else if (line.StartsWith(SMTPROUTER_HEADER_TO)) { routableMessage.Recipients.Add(_newMailboxAddress); } } else { throw new Exception($"Unable to parse '{tempHeader[1]}' to a valid email address"); } } else if (line.StartsWith(SMTPROUTER_HEADER_CREATIONTIME)) { // Load the creation time if (DateTime.TryParseExact(tempHeader[1], SMTPROUTER_HEADER_CREATIONTIME_FORMAT, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime dateTime)) { routableMessage.CreationDateTime = dateTime; } else { routableMessage.CreationDateTime = DateTime.Now; } } else if (line.StartsWith(SMTPROUTER_HEADER_IPADDRESS)) { // Load the Sender IP Address routableMessage.IPAddress = tempHeader[1]; } else if (line.StartsWith(SMTPROUTER_HEADER_FORCEROUTING)) { // Sets the Force Routing Flag routableMessage.ForceRouting = true; } } else if (line.StartsWith(SMTPROUTER_HEADER_END)) { // No longer in header inHeader = false; } } } // Add remaining lines messageStreamWriter.Write(fileStreamReader.ReadToEnd()); // Load Message messageStream.Position = 0; routableMessage.Message = MimeMessage.Load(messageStream); } } } // Returns the Routable Message return(routableMessage); } catch (Exception e) { throw e; } }