Добавить вложение в mimekit mimemessage из потока памяти в C#? - PullRequest
1 голос
/ 05 марта 2020

Я хочу добавить xml вложение в сообщение mimekit. У меня уже есть поток памяти и тип контента. Как я могу создать вложение и добавить его в сообщение MIME?

var message = new MimeMessage();
message.From.Add(new MailboxAddress(myEmail));
message.To.Add(new MailboxAddress(myEmail));
message.Subject = "Test Email";
message.Body = new TextPart("plain") { Text = @"This email is for testing purpose." };
if (!string.IsNullOrEmpty(xml))
{
    /* Attaching the xml without save it in disk */
    using (MemoryStream memoryStream = new MemoryStream())
    {
        byte[] contentAsBytes = Encoding.UTF8.GetBytes(xml);
        memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);

        // Set the position to the beginning of the stream.
        memoryStream.Seek(0, SeekOrigin.Begin);

        // Create attachment
        System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType
        {
            MediaType = MediaTypeNames.Application.Octet,
            Name = file
        };

        //here i would i like to add the memorystream and contenttype to mimemessage
        return;
    }
    using (var smptpClient = new MailKit.Net.Smtp.SmtpClient())
    {
        smptpClient.Connect(smtp, port, SecureSocketOptions.Auto);
        smptpClient.Authenticate(myEmail, myPass);
        smptpClient.Send(message);
        smptpClient.Disconnect(true);
    }
}
...