Skip to content

Commit

Permalink
Add support for .net 5 or greater
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees committed Sep 11, 2023
1 parent 7f22630 commit 3b963d9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 65 deletions.
24 changes: 24 additions & 0 deletions MsgReaderCore/Helpers/MailMessageExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static void WriteTo(this MailMessage mail, Stream stream)
var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter") ??
throw new Exception("Failed to find internal constructor for MailWriterType");

#if(NET5_0_OR_GREATER)
var mailWriterConstructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
new[] { typeof(Stream), typeof(bool) }, null) ??
throw new Exception("Failed to find internal constructor for MailWriter");
Expand All @@ -38,6 +39,29 @@ internal static void WriteTo(this MailMessage mail, Stream stream)
else
throw
new Exception("Failed to find internal 'Close' method on MailWriter");

return;
#elif(NETFRAMEWORK)
var mailWriterConstructor =
mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null) ??
throw new Exception("Failed to find internal constructor for MailWriter");

var mailWriter = mailWriterConstructor.Invoke(new object[] { stream });
var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic) ??
throw new Exception("Failed to find internal 'Send' method on MailMessage");

sendMethod.Invoke(mail, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null!);

var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);

if (closeMethod != null)
closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null!);
else
throw
new Exception("Failed to find internal 'Close' method on MailWriter");
#else
throw new Exception("Unsupported platform");
#endif
}
#endregion
}
76 changes: 26 additions & 50 deletions MsgReaderCore/Mime/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,64 +399,42 @@ private MailMessage ToMailMessage()
// Construct an empty MailMessage to which we will gradually build up to look like the current Message object (this)
var message = new MailMessage { Subject = Headers.Subject, SubjectEncoding = Encoding.UTF8 };

// The HTML version should take precedent over the plain text if it is available
var preferredVersion = new FindFirstMessagePartWithMediaType().VisitMessage(this, "text/html");
if (preferredVersion != null)
{
// Make sure that the IsBodyHtml property is being set correctly for our content
message.IsBodyHtml = true;
}
else
{
// otherwise use the first plain text version as the body, if it exists
preferredVersion = new FindFirstMessagePartWithMediaType().VisitMessage(this, "text/plain");
}
AlternateView htmlView = null;

if (preferredVersion != null)
if (HtmlBody != null)
{
message.Body = preferredVersion.GetBodyAsText();
message.BodyEncoding = preferredVersion.BodyEncoding;
htmlView = AlternateView.CreateAlternateViewFromString(HtmlBody.GetBodyAsText(), Encoding.UTF8, "text/html");
message.AlternateViews.Add(htmlView);
}

// Add body and alternative views (html and such) to the message
IEnumerable<MessagePart> textVersions = new TextVersionFinder().VisitMessage(this);
foreach (var textVersion in textVersions)
{
// The textVersions also contain the preferred version, therefore
// we should skip that one
if (textVersion == preferredVersion)
continue;
if (TextBody != null)
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(TextBody.GetBodyAsText(), Encoding.UTF8, "text/plain"));

var stream = new MemoryStream(textVersion.Body);
var alternative = new AlternateView(stream)
{
ContentId = null!,
ContentType = null!,
TransferEncoding = TransferEncoding.QuotedPrintable,
BaseUri = null
};
alternative.ContentId = textVersion.ContentId;
alternative.ContentType = textVersion.ContentType;
message.AlternateViews.Add(alternative);
}

// Add attachments to the message
IEnumerable<MessagePart> attachments = new AttachmentFinder().VisitMessage(this);
foreach (var attachmentMessagePart in attachments)
foreach (var attachmentMessagePart in Attachments)
{
using var memoryStream = StreamHelpers.Manager.GetStream("Message.cs", attachmentMessagePart.Body, 0, attachmentMessagePart.Body.Length);
var memoryStream = StreamHelpers.Manager.GetStream("Message.cs", attachmentMessagePart.Body, 0, attachmentMessagePart.Body.Length);

var attachment = new Attachment(memoryStream, attachmentMessagePart.ContentType)
if (attachmentMessagePart.IsInline && htmlView != null)
{
ContentId = attachmentMessagePart.ContentId
};
var linkedResource = new LinkedResource(memoryStream, attachmentMessagePart.ContentType)
{
ContentId = attachmentMessagePart.ContentId
};

attachment.Name = string.IsNullOrEmpty(attachment.Name) ? attachmentMessagePart.FileName : attachment.Name;
attachment.ContentDisposition.FileName = string.IsNullOrEmpty(attachment.ContentDisposition.FileName)
? attachmentMessagePart.FileName
: attachment.ContentDisposition.FileName;
htmlView.LinkedResources.Add(linkedResource);
}
else
{
var attachment = new Attachment(memoryStream, attachmentMessagePart.ContentType)
{
ContentId = attachmentMessagePart.ContentId,
Name = attachmentMessagePart.FileName
};

message.Attachments.Add(attachment);
message.Attachments.Add(attachment);
}
}

if (Headers.From is { HasValidMailAddress: true })
Expand Down Expand Up @@ -495,8 +473,7 @@ public void Save(FileInfo file)
if (file == null)
throw new ArgumentNullException(nameof(file));

using var fileStream = new FileStream(file.FullName, FileMode.Create);
Save(fileStream);
Save(file.OpenWrite());
}

/// <summary>
Expand Down Expand Up @@ -542,8 +519,7 @@ public static Message Load(FileInfo file)
if (!file.Exists)
throw new FileNotFoundException("Cannot load message from non-existent file", file.FullName);

using var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
return Load(fileStream);
return Load(file.OpenRead());
}

/// <summary>
Expand Down
38 changes: 23 additions & 15 deletions MsgViewer/ViewerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@
using System.Threading;
using System.Windows.Forms;

/*
Copyright 2013-2018 Kees van Spelde
Licensed under The Code Project Open License (CPOL) 1.02;
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.codeproject.com/info/cpol10.aspx
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// Author: Kees van Spelde <[email protected]>
//
// Copyright (c) 2013-2023 Magic-Sessions. (www.magic-sessions.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

namespace MsgViewer
{
Expand Down

0 comments on commit 3b963d9

Please sign in to comment.