How to send a single part message with other content type?

+1 vote
asked Mar 28, 2014 by obartelt (820 points)
edited Mar 28, 2014

I need to send a message with a single part with content type "text/calendar" (an event invitation/reply). If I construct an AlternateView and add that to the message, it is always sent as multipart/alternative.

I found that using Rebex.Mime.MimeOptions.DisableSinglePartHtmlWorkaround will give me what I want, but that's not what is was meant for ;-)

Is there any other way to accomplish this?

Applies to: Rebex Secure Mail

2 Answers

0 votes
answered Mar 28, 2014 by Lukas Pokorny (99,970 points)
edited Mar 28, 2014
Best answer

MailMessage uses MimeMessage internally and adds additional features such as MSG support and MIME tree normalization, but these featurs are not needed for creating vCalendar entries. On the other hand, MimeMessage/MimeEntity offers much more control over the message structure. Most of the headers such as From, To, Subject, Date are supported by both MailMessage and MimeMessage. MailMessage's AlternateViews, Resources and Attachments are abstractions and don't directly resemble the message structure. A MaiMessage with a single AlternateView might actually result in a MIME message with two MIME entities or a single MIME entity, depending on whether the DisableSinglePathHtmlWorkaround option was specified. With MimeMessage/MimeEntity, there is no such abstraction. The objects directly correspond to MIME entities.

DisableSinglePathHtmlWorkaround is not needed for MimeMessage because you have complete control over the message structure. A single instance of MimeMessage or MimeEntity represents a single MIME tree node, and child entities are only present if you explicitly add them to MimeMessage.Parts (or MimeEntity.Parts) collection.

Calling .SetContent("content", "text/calendar") will result in a MIME entity with a content of "content", content type of "text/calendar" and with no child entities, which seems to be exactly what you need.

commented Mar 28, 2014 by obartelt (820 points)
edited Mar 28, 2014

Ok, thanks for clarifying!

0 votes
answered Mar 28, 2014 by Tomas Knopp (58,890 points)
edited Mar 28, 2014

You can create such non-letter MIME with the help of MimeMessage and MimeEntity classes. Unlike MailMessage, the MimeMessage class will give you a complete control over the MIME structure.

The fact that you were able to achieve the desired structure with the MailMessage + MimeOptions.DisableSinglePartHtmlWorkaround was a bit of a luck. (The MailMessage with one view and nothing else has the same structure as the one you need. However, if you added an Attachment, or Resource to that, it would stop working).

So you need a MIME message with single part with a specific content type. Please try this code to generate it:

MimeMessage mime = new MimeMessage();
mime.SetContent("content", "text/calendar");

commented Mar 28, 2014 by obartelt (820 points)
edited Mar 28, 2014

Does MimeMessage have all the features of the MailMessage class? I need to construct a message containing a vCalendar entry as body (with the content type "text/calendar") and send it by e-mail, so it has to have from, to, and subject as well.

Or is there any chance of either adding a method to specify the content type of the MailMessage/its body text or adding an option to force the same behavior as with DisableSinglePathHtmlWorkaround, if there's only one "alternate" view? (I'm not even settings BodyText and BodyHtml, just adding one AlternateView)