0 votes
by (370 points)

Hi

Im building a function that sends Rebex.Mail.MailMessage using Ews.SendMessage(). Before sending the message Have to copy all the properties from System.Net.Mail.MailMessage to Rebex.Mail.MailMessage. This works fine for everything except images. I do this by copying the AlternateViews. My function looks like this:

    For Each AlternateView As AlternateView In System.Net.Mail.AlternateViewCollection
        RebexAlternateViewCollection.Add(New Rebex.Mail.AlternateView(AlternateView.ContentStream))
     Next

Im basically copying the AlternateViews, using the ContentStream. This does not work. The email I receive has a broken image icon.

  1. Am I doing this correctly?
  2. Is there maybe an easy way to copy System.Net.Mail.MailMessage to Rebex.Mail.MailMessage?

Thanks
Izak

Applies to: Rebex Secure Mail

1 Answer

+1 vote
by (148k points)
selected by
 
Best answer

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);
    }
}
by (370 points)
Hi Lucas,

Thank you, The 2nd solution solves my issue.
...