I was playing around with MailKit/MimeKit and something is bugging me. I wanted to send e-mails with attachments using streams. MimeKit offers the BodyBuilder
class which makes it very easy to create the body message and attach files:
public void SendEmail(string body, Stream attachment, string fileName)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Carl", "carl@site.com"));
message.To.Add(new MailboxAddress("Rick", "rick@site.com"));
message.Subject = "Things got messy...?";
var builder = new BodyBuilder();
builder.TextBody = body;
builder.Attachments.Add(fileName, attachment);
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
// code to send e-mail here...
}
}
I generate the stream elsewhere in my code and I don't close it, so I can pass it on to MimeKit. The thing that is not clear is: Do MimeKit disposes the stream? Basically (as far as I'm aware) the consumer usually is the responsible for disposing Streams. I'm also aware that calling dispose on a MemoryStream (which I'm using basically) won't free up any resources.. but will prevent from reading/writing to it. But if, in the future, the implementations change to another type of Stream, things can get more complex.
I also dig into MikeKit source code, and found that the Stream passed in to the AttachmentCollection.Add
gets 'added' into a MemoryBlockStream
which inherits from Stream (and implements Dispose) so I'm supposing it get's disposed but, at this point, I'm just guessing.
Any ideas?