0 votes

hi,
how can set the content-type for email?

by
The real question is why do you want to do that? In most cases it is not needed, as it is set automatically by Mail.dll.

1 Answer

0 votes

You can use following code to set the content-type header by hand:

IMail email = ...
email.Document.Root.ContentType = ContentType.TextHtml;

However in 99% of cases this is not needed and you need to know exactly why and what you are doing it.

Remember, that email containing attachments, must have its root content-type set to multipart/mixed, email with html and images: multipart/related.

If you use MailBuilder to create an email it will set email's content-type automatically.

var builder = new MailBuilder();
builder.Html = "some <strong>html</<strong>";
IMail email = builder.Create();
Assert.AreEqual(ContentType.TextHtml, email.Document.Root.ContentType);

Results in text/html email.

var builder = new MailBuilder();
builder.Text = "some text";
IMail email = builder.Create();
Assert.AreEqual(ContentType.TextPlain, email.Document.Root.ContentType);

Results in text/plain email.

var builder = new MailBuilder();
builder.Text = "some text";
builder.AddAttachment("c:\\report.pdf");
IMail email = builder.Create();
Assert.AreEqual(ContentType.MulitpartMixed, email.Document.Root.ContentType);

Results in multipart/mixed email.

by (301k points)
edited by
...