1. No, this is not correct. AlternateViews
only contains the message text in different formats. Images are present in Resources
collection, and attachments in Attachments
.
Also, in addition to the stream content of views (or resources and attachments), you would have to copy essential properties (representing headers such as Content-Type, Content-Id and Content-Disposition) as well (or more, depending on whether you wish to preserve them).
2. The following approach uses .NET's SmtpClient
class, which is actually capable of saving .NET's MailMessage
into a file. The only problem in that API is that the file name cannot be specified, but the method below works around that by creating a temporary folder (and removing it when done):
public static Rebex.Mail.MailMessage DotNetMailMessageToRebex(System.Net.Mail.MailMessage dotNetMessage)
{
// create a temporary directory for the message file
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
try
{
// save .NET MailMessage into a randomly-named file in the temporary directory
var dummyClient = new System.Net.Mail.SmtpClient();
dummyClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
dummyClient.PickupDirectoryLocation = tempPath;
dummyClient.Send(dotNetMessage);
// make sure that exactly one file was saved
string[] files = Directory.GetFiles(tempPath);
if (files.Length <= 0)
throw new Exception("Message not found.");
if (files.Length != 1)
throw new Exception("Too many messages found.");
// load the file into Rebex MailMessage
var mail = new Rebex.Mail.MailMessage();
mail.Load(files[0]);
return mail;
}
finally
{
Directory.Delete(tempPath, true);
}
}