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", "receiver@gmail.com"));
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