Skip to content

Commit

Permalink
Fix: Bug #170 - "just send 8-bit” messages shown with wrong encoding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rnwood authored Mar 9, 2024
1 parent d6c5bfd commit bdc30ac
Show file tree
Hide file tree
Showing 22 changed files with 878 additions and 297 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.gitignore
.vs
.vscode
.idea
**/bin
**/obj
Rnwood.Smtp4dev/ClientApp/node_modules
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.user
*.userosscache
*.sln.docstates
.idea

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "coreclr",
"request": "launch",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Rnwood.Smtp4dev/bin/Debug/netcoreapp3.1/Rnwood.Smtp4dev.dll",
"program": "${workspaceFolder}/Rnwood.Smtp4dev/bin/Debug/net8.0/Rnwood.Smtp4dev.dll",
"cwd": "${workspaceFolder}/Rnwood.Smtp4dev",
"args": "--urls http://localhost:5000",
"stopAtEntry": false,
Expand Down
24 changes: 21 additions & 3 deletions Rnwood.Smtp4dev/ApiModel/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;

namespace Rnwood.Smtp4dev.ApiModel
Expand All @@ -20,6 +21,8 @@ public Message(DbModel.Message dbMessage)
ReceivedDate = dbMessage.ReceivedDate;
Subject = dbMessage.Subject;
SecureConnection = dbMessage.SecureConnection;
SessionEncoding = dbMessage.SessionEncoding;
EightBitTransport = dbMessage.EightBitTransport;

Parts = new List<MessageEntitySummary>(1);
RelayError = dbMessage.RelayError;
Expand Down Expand Up @@ -72,6 +75,10 @@ public Message(DbModel.Message dbMessage)
}
}

public string SessionEncoding { get; set; }

public bool? EightBitTransport { get; set; }


private MessageEntitySummary HandleMimeEntity(MimeEntity entity)
{
Expand Down Expand Up @@ -154,23 +161,34 @@ internal static FileStreamResult GetPartContent(Message result, string cid)
internal static string GetPartContentAsText(Message result, string id)
{
var contentEntity = GetPart(result, id);

if (contentEntity is MimePart part)
{
using var reader = new StreamReader(part.Content.Open());
var encoding = part.ContentType.CharsetEncoding ?? ApiModel.Message.GetSessionEncodingOrAssumed(result);
using var reader = new StreamReader(part.Content.Open(), encoding);
return reader.ReadToEnd();
}

return contentEntity.ToString();

}

internal static Encoding GetSessionEncodingOrAssumed(Message result)
{
return !string.IsNullOrEmpty(result.SessionEncoding) ? Encoding.GetEncoding(result.SessionEncoding) : Encoding.Latin1;
}


internal static string GetPartSource(Message message, string id)
{
var contentEntity = GetPart(message, id);
return contentEntity.ToString();
using (MemoryStream ms = new MemoryStream())
{
contentEntity.WriteTo(ms, false);
var encoding = contentEntity.ContentType.CharsetEncoding ??ApiModel.Message.GetSessionEncodingOrAssumed(message);
return encoding.GetString(ms.GetBuffer());
}

}


Expand Down
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/ClientApp/src/components/messagesource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
} else {
this.sourceurl = new MessagesController().getMessageSource_url(this.message.id);
this.source = await new MessagesController().getMessageSourceRaw(this.message.id);
this.source = await new MessagesController().getMessageSource(this.message.id);
}
}
Expand Down
Loading

0 comments on commit bdc30ac

Please sign in to comment.