Skip to content

Commit

Permalink
Support for cc and bcc (#42)
Browse files Browse the repository at this point in the history
* added net8.0 to target frameworks, updated packages

* update build workflow to use .net 8

* Added support for cc and bcc

* Calculate bcc addresses correctly
  • Loading branch information
VILLAN3LL3 authored Mar 29, 2024
1 parent a7dac2f commit 259a987
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 442 deletions.
2 changes: 1 addition & 1 deletion netDumbster.Test/RepeatAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public RepeatAttribute(int count)

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
return Enumerable.Repeat(new object[0], times);
return Enumerable.Repeat(Array.Empty<object>(), times);
}
}

Expand Down
422 changes: 221 additions & 201 deletions netDumbster.Test/TestsBase.cs

Large diffs are not rendered by default.

38 changes: 15 additions & 23 deletions netDumbster/Configure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class Configuration
/// </summary>
internal Configuration()
{
this.UseMessageStore = true;
this.IPAddress = IPAddress.Any;
this.ReuseAddress = true;
UseMessageStore = true;
IPAddress = IPAddress.Any;
ReuseAddress = true;
}

/// <summary>
Expand Down Expand Up @@ -74,16 +74,16 @@ public static Configuration Configure()
public SimpleSmtpServer Build()
{
var config = this;
if (this.Port == 0)
if (Port == 0)
{
config = this.WithRandomPort();
config = WithRandomPort();
}
return SimpleSmtpServer.Start(config);
}

public Configuration DoNotReuseAddress()
{
this.ReuseAddress = false;
ReuseAddress = false;
return this;
}

Expand All @@ -93,7 +93,7 @@ public Configuration DoNotReuseAddress()
/// <returns></returns>
public Configuration WithRandomPort()
{
this.Port = Configuration.GetRandomUnusedPort();
Port = GetRandomUnusedPort();
return this;
}

Expand All @@ -104,7 +104,7 @@ public Configuration WithRandomPort()
/// <returns></returns>
public Configuration WithPort(int port)
{
this.Port = port;
Port = port;
return this;
}

Expand All @@ -115,7 +115,7 @@ public Configuration WithPort(int port)
/// <returns></returns>
public Configuration WithAddress(IPAddress address)
{
this.IPAddress = address;
IPAddress = address;
return this;
}

Expand All @@ -126,7 +126,7 @@ public Configuration WithAddress(IPAddress address)
/// <returns></returns>
public Configuration EnableMessageStore(bool enable)
{
this.UseMessageStore = enable;
UseMessageStore = enable;
return this;
}

Expand All @@ -136,19 +136,11 @@ public Configuration EnableMessageStore(bool enable)
/// <returns></returns>
private static int GetRandomUnusedPort()
{
try
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
catch
{
throw;
}
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}

}
40 changes: 28 additions & 12 deletions netDumbster/EmailAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace netDumbster.smtp
{
using System.Collections.Generic;
using System.Text.RegularExpressions;

/// <summary>
Expand All @@ -19,7 +20,7 @@ namespace netDumbster.smtp
/// </remarks>
public class EmailAddress
{
private Regex ILLEGAL_CHARACTERS = new Regex("[][)(@><\\\",;:]");
private readonly Regex ILLEGAL_CHARACTERS = new("[][)(@><\\\",;:]");

/// <summary>
/// Creates a new EmailAddress using a valid address.
Expand All @@ -40,10 +41,10 @@ public EmailAddress(string address)
throw new InvalidEmailAddressException("Invalid address. The address must be formatted as: username@domain.");
}

this.Username = addressParts[0];
this.Domain = addressParts[1];
Username = addressParts[0];
Domain = addressParts[1];

ValidateAddress(this.Username, this.Domain);
ValidateAddress(Username, Domain);
}

/// <summary>
Expand All @@ -54,10 +55,10 @@ public EmailAddress(string address)
/// </exception>
public EmailAddress(string username, string domain)
{
this.Username = username;
this.Domain = domain;
Username = username;
Domain = domain;

ValidateAddress(this.Username, this.Domain);
ValidateAddress(Username, Domain);
}

/// <summary>
Expand All @@ -70,7 +71,7 @@ public string Address
{
get
{
return this.Username + "@" + this.Domain;
return Username + "@" + Domain;
}
}

Expand Down Expand Up @@ -104,7 +105,7 @@ public string Username
/// <returns>Value of Address Property.</returns>
public override string ToString()
{
return this.Address;
return Address;
}

/// <summary>
Expand All @@ -117,7 +118,7 @@ public override string ToString()
/// </exception>
private void VerifySpecialCharacters(string data)
{
if (this.ILLEGAL_CHARACTERS.IsMatch(data))
if (ILLEGAL_CHARACTERS.IsMatch(data))
{
throw new InvalidEmailAddressException("Invalid address. The username and domain address parts can not contain any of the following characters: ( ) < > @ , ; : \\ \" . [ ]");
}
Expand All @@ -130,15 +131,30 @@ private void ValidateAddress(string username, string domain)
throw new InvalidEmailAddressException("Invalid username. Username must be at least one charecter");
}
// Verify that the username does not contain illegal characters.
this.VerifySpecialCharacters(username);
VerifySpecialCharacters(username);

if (domain == null || domain.Length < 5)
{
throw new InvalidEmailAddressException("Invalid domain. Domain must be at least 5 charecters (a.com, a.edu, etc...)");
}

// Verify that the domain does not contain illegal characters.
this.VerifySpecialCharacters(domain);
VerifySpecialCharacters(domain);
}
}

public class EmailAddressComparer : IEqualityComparer<EmailAddress>
{
public bool Equals(EmailAddress? x, EmailAddress? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null || y is null) return false;
return x.Address == y.Address;
}

public int GetHashCode(EmailAddress obj)
{
return obj.Address != null ? obj.Address.GetHashCode() : 0;
}
}
}
50 changes: 25 additions & 25 deletions netDumbster/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ConsoleLogger : ILog
{
private const string layout = "{0} - {1} - {2} - {3}";

private string type;
private readonly string type;

public ConsoleLogger(Type type)

Check warning on line 11 in netDumbster/Logging/ConsoleLogger.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Non-nullable field 'type' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 11 in netDumbster/Logging/ConsoleLogger.cs

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Non-nullable field 'type' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 11 in netDumbster/Logging/ConsoleLogger.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Non-nullable field 'type' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
Expand All @@ -15,102 +15,102 @@ public ConsoleLogger(Type type)

public void Debug(object message)
{
this.WriteToConsole("DEBUG", message);
WriteToConsole("DEBUG", message);
}

public void Debug(object message, Exception exception)
{
this.WriteToConsole("DEBUG", message, exception);
WriteToConsole("DEBUG", message, exception);
}

public void DebugFormat(string format, params object[] args)
{
this.WriteToConsole("DEBUG", format, args);
WriteToConsole("DEBUG", format, args);
}

public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
this.WriteToConsole("DEBUG", provider, format, args);
WriteToConsole("DEBUG", provider, format, args);
}

public void Error(object message)
{
this.WriteToConsole("ERROR", message);
WriteToConsole("ERROR", message);
}

public void Error(object message, Exception exception)
{
this.WriteToConsole("ERROR", message, exception);
WriteToConsole("ERROR", message, exception);
}

public void ErrorFormat(string format, params object[] args)
{
this.WriteToConsole("ERROR", format, args);
WriteToConsole("ERROR", format, args);
}

public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
this.WriteToConsole("ERROR", provider, format, args);
WriteToConsole("ERROR", provider, format, args);
}

public void Fatal(object message)
{
this.WriteToConsole("FATAL", message);
WriteToConsole("FATAL", message);
}

public void Fatal(object message, Exception exception)
{
this.WriteToConsole("FATAL", message, exception);
WriteToConsole("FATAL", message, exception);
}

public void FatalFormat(string format, params object[] args)
{
this.WriteToConsole("FATAL", format, args);
WriteToConsole("FATAL", format, args);
}

public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
this.WriteToConsole("FATAL", provider, format, args);
WriteToConsole("FATAL", provider, format, args);
}

public void Info(object message)
{
this.WriteToConsole("INFO", message);
WriteToConsole("INFO", message);
}

public void Info(object message, Exception exception)
{
this.WriteToConsole("INFO", message, exception);
WriteToConsole("INFO", message, exception);
}

public void InfoFormat(string format, params object[] args)
{
this.WriteToConsole("INFO", format, args);
WriteToConsole("INFO", format, args);
}

public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
this.WriteToConsole("INFO", provider, format, args);
WriteToConsole("INFO", provider, format, args);
}

public void Warn(object message)
{
this.WriteToConsole("WARNING", message);
WriteToConsole("WARNING", message);
}

public void Warn(object message, Exception exception)
{
this.WriteToConsole("WARNING", message, exception);
WriteToConsole("WARNING", message, exception);
}

public void WarnFormat(string format, params object[] args)
{
this.WriteToConsole("WARNING", format, args);
WriteToConsole("WARNING", format, args);
}

public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
this.WriteToConsole("WARNING", provider, format, args);
WriteToConsole("WARNING", provider, format, args);
}

private static string CurrentDateTime()
Expand All @@ -120,22 +120,22 @@ private static string CurrentDateTime()

private void WriteToConsole(string level, object message)
{
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, this.type, message));
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, type, message));
}

private void WriteToConsole(string level, object message, Exception exception)
{
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, this.type, string.Format("{0} Exception: {1}", message, exception.ToString())));
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, type, string.Format("{0} Exception: {1}", message, exception.ToString())));
}

private void WriteToConsole(string level, string format, params object[] args)
{
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, this.type, string.Format(format, args)));
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, type, string.Format(format, args)));
}

private void WriteToConsole(string level, IFormatProvider provider, string format, params object[] args)
{
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, this.type, string.Format(provider, format, args)));
Console.WriteLine(string.Format(layout, CurrentDateTime(), level, type, string.Format(provider, format, args)));
}
}
}
2 changes: 1 addition & 1 deletion netDumbster/MailMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static SmtpMessagePart[] Parts(this MailMessage mailMessage)
parts.Add(part);
}

return parts.ToArray();
return [.. parts];
}

private static string StreamToString(Stream stream)
Expand Down
2 changes: 1 addition & 1 deletion netDumbster/MailMessageMimeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class MailMessageMimeParser
{
public static MailMessage ParseMessage(string mimeMail)
{
var mimeMessage = MimeMessage.Load(new MemoryStream(UTF8Encoding.UTF8.GetBytes(mimeMail)));
var mimeMessage = MimeMessage.Load(new MemoryStream(Encoding.UTF8.GetBytes(mimeMail)));

return mimeMessage.ConvertToMailMessage();
}
Expand Down
Loading

0 comments on commit 259a987

Please sign in to comment.