Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Place display name between quotes after decoding. #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions OpaqueMail/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace OpaqueMail
Expand Down Expand Up @@ -221,7 +222,7 @@ public static string Decode(string input, string contentTransferEncoding, string
/// </summary>
/// <param name="header">Email header to be decoded.</param>
/// <returns>The decoded email header.</returns>
public static string DecodeMailHeader(string header)
public static string DecodeMailHeader(string header, bool wrapInQuotes=false)
{
if (!string.IsNullOrEmpty(header))
{
Expand Down Expand Up @@ -267,7 +268,10 @@ public static string DecodeMailHeader(string header)
}

// Append the decoded string.
headerBuilder.Append(Encoding.UTF8.GetString(Encoding.Convert(encoding, Encoding.UTF8, encodedBytes)));
string decodedAddress = Encoding.UTF8.GetString(Encoding.Convert(encoding, Encoding.UTF8, encodedBytes));
if (wrapInQuotes)
decodedAddress = NormalizeQuotes(decodedAddress);
headerBuilder.Append(decodedAddress);

cursor = endCursor + 2;
}
Expand Down Expand Up @@ -945,6 +949,16 @@ public static string NormalizeCharSet(string charSet)
return charSet;
}

/// <summary>
/// Ensures that the given address is between quotes
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static string NormalizeQuotes(string address)
{
return '"' + Regex.Replace(address, @"('|"")", "") + '"';
}

/// <summary>
/// Returns string representation of message sent over stream.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion OpaqueMail/MailAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public class MailAddressCollection : Collection<MailAddress>
public static MailAddressCollection Parse(string addresses)
{
// Escape embedded encoding.
addresses = Functions.DecodeMailHeader(addresses);
addresses = Functions.DecodeMailHeader(addresses, wrapInQuotes: true);

// Create a new collection of MailAddresses to be returned.
MailAddressCollection addressCollection = new MailAddressCollection();
Expand Down