Skip to content

Commit

Permalink
Added code to convert an eml file to an msg file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sicos2002 committed Jan 23, 2017
1 parent a77e425 commit 1914d46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
46 changes: 43 additions & 3 deletions MsgKit/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
//

using System;
using System.IO;
using System.Text;
using MimeKit;

namespace MsgKit
{
Expand All @@ -40,10 +43,47 @@ public static class Converter
/// <param name="msgFileName">The MSG file</param>
public static void ConvertEmlToMsg(string emlFileName, string msgFileName)
{
var eml = MimeKit.MimeMessage.Load(emlFileName);
var eml = MimeMessage.Load(emlFileName);
var sender = new Sender(eml.Sender.Address, eml.Sender.Name);
var msg = new MsgKit.Email(sender, eml.Subject);
throw new NotImplementedException("Not yet done");
var representing = new Representing(eml.ResentSender.Address, eml.ResentSender.Name);
var msg = new Email(sender, representing, eml.Subject);

foreach(var to in eml.To)
msg.Recipients.AddTo(to.ToString(), to.Name);

foreach(var cc in eml.Cc)
msg.Recipients.AddBcc(cc.ToString(), cc.Name);

foreach (var bcc in eml.Bcc)
msg.Recipients.AddBcc(bcc.ToString(), bcc.Name);

using (var headerStream = new MemoryStream())
{
eml.Headers.WriteTo(headerStream);
headerStream.Position = 0;
msg.TransportMessageHeaders = Encoding.ASCII.GetString(headerStream.ToArray());
}

msg.BodyHtml = eml.HtmlBody;
msg.BodyText = eml.TextBody;

foreach (var attachment in eml.Attachments)
{
if (!attachment.IsAttachment) continue;

using (var attachmentStream = new MemoryStream())
{
attachment.WriteTo(attachmentStream);
attachmentStream.Position = 0;

msg.Attachments.Add(
attachmentStream,
attachment.ContentDisposition.FileName,
-1,
attachment.ContentDisposition.Disposition.Equals("inline", StringComparison.InvariantCultureIgnoreCase),
attachment.ContentId);
}
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MsgKit/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public Attachments Attachments
/// the message has been sent outside an Exchange environment to another mailserver
/// <c>null</c> will be returned when not present
/// </summary>
public string TransportMessageHeaders { get; private set; }
public string TransportMessageHeaders { get; set; }
#endregion

#region Constructor
Expand Down

0 comments on commit 1914d46

Please sign in to comment.