+1 vote

Hello,
I want to add an attachment to my mail, and I want to add 3 headers to its part. How can I do that?
Thank you.

by
retagged by

1 Answer

0 votes
 
Best answer

To create new email use MailBuilder class. It exposes AddAttachment method. It has are 3 overloads: one taking MimeData instance, two other taking byte array or file name, which return MimeData instance.

Use MimeData.Headers collection to add or modify any MIME headers of the attachment MIME part.

MailBuilder builder = new MailBuilder();

MimeData att1 = builder.AddAttachment("attachment.zip");
att1.ContentType = ContentType.ApplicationZip;
att1.ContentId = "attachment@example.org";
att1.Headers["X-CustomHeader"] = "Value";

IMail mail = builder.Create();

Please note that common headers (such as content-type or content-id) are exposed as properties on MimeData class.

by (301k points)
Thanks for your interest,
I suppose that "att1.ContentType = ContentType.ApplicationZip;" line adds "Content-Type" header. So is it possible that I add the "Content-Type" header manually?
Like att1.Headers["Content-Type"]="image/jpeg; name=asd.jpeg; image-type=asd-image";
Sure you can do that. Please note Mail.dll is smart enough to recognize common file extensions, so if you are creating email attachment from file on disk, in most cases, contant-type will be set to correct value. You can also use overloaded ConentType constructor, and use either defined types and subtypes or create new one: new ContentType(MimeType.Image, new MimeSubtype("png")).
...