set ContentDisposition and TransferEncoding for message parts

0 votes
asked Mar 8, 2011 by martin (990 points)
edited Apr 5, 2011

We want to load different message parts (AlternateViews and Attachments) that are saved in a DB to a Rebex.Mail.MailMessage. Purpose is uploading to mailserver. We have a saved ContentDisposition, TransferEncoding... that we want to use if we load the part into Rebex object. How can we do that and what is the right sequence for that? By the way. There are many wrong descriptions of the READONLY-properties; "Get or Set"- is often displayed, but they are only readable

Here's my testcode:

        For Each oBaseContent As EMailContent In oBaseEMail.colMailBody</p>

        Dim oAlternateView As Rebex.Mail.AlternateView = New Rebex.Mail.AlternateView()

        oAlternateView.Options = CType(oAlternateView.Options + Rebex.Mime.MimeOptions.AllowAnyTextCharacters, Rebex.Mime.MimeOptions)

        Dim oStream As New System.IO.MemoryStream(oBaseContent.asContentBody)

        'oAlternateView.SetContent(oBaseContent.sContentBody, oBaseContent.sContentType, _
        'System.Text.Encoding.GetEncoding(oBaseContent.sContentTypeCharSet), mlGetRebexTransferEncoding(oBaseContent.sContentTransferEncoding))

        oAlternateView.SetContent(oStream, oBaseContent.sContentType)

        If (oBaseContent.sContentTypeName &lt;&gt; String.Empty) Then
            oAlternateView.ContentDescription = oBaseContent.sContentTypeName
        End If

        'If (oBaseContent.sContentDisposition = "inline") Then
        '   oAlternateView.ContentDisposition = New ContentDisposition(oBaseContent.sContentDisposition)

        'Else
        '   oAlternateView.ContentDisposition.Inline = False
        'End If

        If (oBaseContent.sContentDispositionFileName &lt;&gt; String.Empty) Then
            oAlternateView.ContentDisposition.FileName = oBaseContent.sContentDispositionFileName
        End If

        If (oBaseContent.sContentID &lt;&gt; String.Empty) Then
            oAlternateView.ContentId = oBaseContent.sContentID
        End If

        'oAlternateView.TransferEncoding = mlGetRebexTransferEncoding(oBaseContent.sContentTransferEncoding)

        oAlternateView.ContentType.Boundary = oBaseContent.sContentTypeBoundary
        oAlternateView.ContentType.CharSet = oBaseContent.sContentTypeCharSet

        moMailMessage.AlternateViews.Add(oAlternateView)

    Next
Applies to: Rebex Secure Mail
commented Mar 8, 2011 by Martin Vobr (12,940 points)
edited Apr 5, 2011

Regarding readonly properties with "gets or sets" description: thanks for reporting it. It will be corrected in the next release.

6 Answers

+1 vote
answered Mar 9, 2011 by Lukas Pokorny (103,090 points)
edited Mar 9, 2011
 
Best answer

To convert an instance of System.Net.Mail.MailMessage into Rebex.Mail.MailMessage, you first have to save the .NET MailMessage into a file, which can be done using System.Net.Mail.SmtpClient class:

var client = new System.Net.Mail.SmtpClient();
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = directoryPath;
client.Send(message);

The file saved into the specified directory will have a randomly-generated filename, so make sure you are always saving to an empty directory and use System.IO.Directory.GetFiles to find the file.

Then, create an instance of Rebex MailMessage class and call its Load method to load the message from the file saved by .NET MailMessage.

+1 vote
answered Mar 8, 2011 by Lukas Pokorny (103,090 points)
edited Mar 8, 2011

Classes in Rebex.Mail are intended to provide a high-level API that shields developers from having to deal with MIME internals directly. This means, for example:

  • You can't set ContentDisposition of an AlternateView because it is mainly intended for attachments, not for views (and a view with a ContentDisposition of "attachment" might cause problems at the receiving end)
  • If a message part is an "attachment", it should simply go to MailMessage.Attachments, and if it's "inline", it should either go to MailMessage.AlternateViews (if it's a view) or to MailMessage.Resources (if it's an image or something else).
  • MailMessage'sContentDispositionandContentType` properties are not supposed to be set, although this is not enforced at the moment (although it should).

In short, MailMessage class is proably not what you want when you want to re-construct a MIME message from pre-existing MIME headers.

Have you looked into Rebex.Mime namespace? This is a low-level MIME API on top of which Rebex.Mail is built, and offers much more freedom. I would post some sample code, but I'm not sure what some of your fields mean. Would it be possible to post some sample values of sContentType and sContentTypeName?

Also, setting a boundary of a view or an attachment doesn't make much sense - a boundary is a property of multipart MIME entities - those that encapsulate views or attachments.)

0 votes
answered Mar 8, 2011 by martin (990 points)
edited Mar 8, 2011

for example i have following values in db, seperated by comma here first db entry is a inline gif (in signature) to add to mail message second is body text

ContentIDProvider,ContentType,ContentTypeCharset,ContentTypeName,ContentTypeBoundary,ContentTransferEncoding,ContentDisposition,ContentDispositionFileName,Data

part1.01050309.05030400@arcor.de,image/gif,NULL,DieseNachrichtalsIMAGE.gif,NULL,base64,inline,DieseNachrichtalsIMAGE.gif,Binary NULL,text/plain,iso-8859-2,NULL,NULL,7bit,NULL,NULL,Binary

commented Mar 8, 2011 by Lukas Pokorny (103,090 points)
Thanks! One more question: How is the structure of the original MIME tree persisted in the DB? Or, how do you persist multipart entities, and how do you decide which part goes into which multipart entity?
commented Mar 8, 2011 by martin (990 points)
oh sorry, that is what i don't understand too. there's some old complex logic in the code. i can't tell you that at the moment.
commented Mar 8, 2011 by Lukas Pokorny (103,090 points)
OK, we'll try to handle it somehow :-)
commented Mar 8, 2011 by martin (990 points)
looks a bit complicated at the moment for me; but as a workaround i found a way to load all db informations into a System.Net.Mail.MailMessage object - is there a way to transform from that .NET-class?
commented Mar 9, 2011 by Lukas Pokorny (103,090 points)
Please see http://forum.rebex.net/questions/817/set-contentdisposition-and-transferencoding-for-message-parts/826#826 for a way which describes how to convert .NET MailMessage to Rebex MailMessage.
0 votes
answered Mar 9, 2011 by martin (990 points)
edited Mar 9, 2011

workaround possible? i found a way to load all db informations into a System.Net.Mail.MailMessage object

but is there a way to load it from there into mime or mail message?

commented Mar 9, 2011 by Lukas Pokorny (103,090 points)
Please see http://forum.rebex.net/questions/817/set-contentdisposition-and-transferencoding-for-message-parts/826#826 for a way which describes how to convert .NET MailMessage to Rebex MailMessage.
0 votes
answered Mar 10, 2011 by martin (990 points)
edited Mar 10, 2011

Thank you - but in my opinion this is really not a nice solution. I'll give it a try.

0 votes
answered Mar 10, 2011 by martin (990 points)
edited Mar 10, 2011

i think i should go the other way over mime-entities. but can you please add a small example how to use this - i mean, create some mime's then add it to mimemessage and do i have to convert this mimemessage to a mailmessage for upload?

commented Mar 16, 2011 by Lukas Pokorny (103,090 points)
Sorry, we only noticed this question now! 1) We will post a small example to add MimeEntities into MimeMessage or another MimeEntity soon. 2) No, you don't have to convert MimeMessages to MailMessage because they both use MIME format when saved or transported.
commented Mar 17, 2011 by martin (990 points)
ok, small example would be really nice. thank you in advance
...