Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send MailKit email with an attachment from memoryStream

How to send an email with an attachment from memoryStream using MailKit? Currently, I'm sending using regular SMTP and attaching the file using the below code, but couldn't find any proper example to send it using MailKit package. I have gone through this two docs, but couldn't find a proper solution. http://www.mimekit.net/docs/html/M_MimeKit_AttachmentCollection_Add_6.htm

using System.Net.Mail;
MemoryStream memoryStream = new MemoryStream(bytes);
                    message.Attachments.Add(new Attachment(memoryStream, "Receipt.pdf", MediaTypeNames.Application.Pdf));

This is my MailKit email code:

#region MailKit
                string fromEmail = GlobalVariable.FromEmail;
                string fromEmailPwd = "";//add sender password
                var email = new MimeKit.MimeMessage();
                email.From.Add(new MimeKit.MailboxAddress("Sender", fromEmail));

                email.To.Add(new MimeKit.MailboxAddress("receiver", "[email protected]"));
                var emailBody = new MimeKit.BodyBuilder
                {
                    HtmlBody = htmlString
                };
                email.Subject = "test Booking";
                email.Body = emailBody.ToMessageBody();
                //bytes is parameter.
                //MemoryStream memoryStream = new MemoryStream(bytes);
                //message.Attachments.Add(new Attachment(memoryStream, "Receipt.pdf", MediaTypeNames.Application.Pdf));

                using (var smtp = new MailKit.Net.Smtp.SmtpClient())
                {
                    smtp.Connect("smtp.gmail.com", 465, true);
                    smtp.Authenticate(fromEmail, fromEmailPwd);
                    smtp.Send(email);
                    smtp.Disconnect(true);
                }
                #endregion
like image 398
dawncode Avatar asked Oct 20 '25 02:10

dawncode


2 Answers

If you'd like to stick with the MimeKit's BodyBuilder to build your message body, you can do something like this:

var emailBody = new MimeKit.BodyBuilder
{
    HtmlBody = htmlString
};
emailBody.Attachments.Add ("Receipt.pdf", bytes);

// If you find that MimeKit does not properly auto-detect the mime-type based on the
// filename, you can specify a mime-type like this:
//emailBody.Attachments.Add ("Receipt.pdf", bytes, ContentType.Parse (MediaTypeNames.Application.Pdf));

message.Body = emailBody.ToMessageBody ();
like image 51
jstedfast Avatar answered Oct 21 '25 15:10

jstedfast


This is how it's done: you need to create a TextPart for the string content and a MimePart for the attachment and add both to a Multipart which is the Body of the MimeMessage

I assumed that you want to send an HTML string textContent and a PDF file with name filename which is already read using any stream named stream.

var multipart = new Multipart("mixed");
var textPart = new TextPart(TextFormat.Html)
{
    Text = textContent,
    ContentTransferEncoding = ContentEncoding.Base64,
};
multipart.Add(textPart);

stream.Position = 0; // you MUST reset stream position

var attachmentPart = new MimePart(MediaTypeNames.Application.Pdf)
{
    Content = new MimeContent(stream),
    ContentId = filename,
    ContentTransferEncoding = ContentEncoding.Base64,
    FileName = filename
};
multipart.Add(attachmentPart);

mimeMessage.Body = multipart;

Note that as for contentType I used MediaTypeNames.Application.Pdf from DLL System.Net.Mail and namespace System.Net.Mime, which equals the string "application/pdf". You may instead use any other library that you like, or write your own.

like image 40
Bizhan Avatar answered Oct 21 '25 15:10

Bizhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!