public static void SaveAttachment(MimeKit.MimeEntity attachment, string filepath) { using (var stream = System.IO.File.Create(filepath)) { if (attachment is MimeKit.MessagePart) { var rfc822 = (MimeKit.MessagePart)attachment; rfc822.Message.WriteTo(stream); } else { var part = (MimeKit.MimePart)attachment; part.Content.DecodeTo(stream); } } }
// https://stackoverflow.com/questions/39912942/mailkit-sendmail-doubts // https://stackoverflow.com/questions/37853903/can-i-send-files-via-email-using-mailkit public static void SendMessageComplex() { string userName = ""; string userPass = ""; string subject = "How you doin?"; string plainText = @"Hey Alice, What are you up to this weekend? Monica is throwing one of her parties on Saturday and I was hoping you could make it. Will you be my +1? -- Joey "; string path = @"D:\testfile.pdf"; string host = "smtp.gmail.com"; int portNum = 443; bool useSsl = false; MimeKit.MimeMessage message = new MimeKit.MimeMessage(); message.From.Add(new MimeKit.MailboxAddress("Joey", "*****@*****.**")); message.To.Add(new MimeKit.MailboxAddress("Alice", "*****@*****.**")); // https://tools.ietf.org/html/rfc2076#page-16 // https://tools.ietf.org/html/rfc1911 // The case-insensitive values are "Personal" and "Private" // Normal, Confidential, // If a sensitivity header is present in the message, a conformant // system MUST prohibit the recipient from forwarding this message to // any other user. If the receiving system does not support privacy and // the sensitivity is one of "Personal" or "Private", the message MUST // be returned to the sender with an appropriate error code indicating // that privacy could not be assured and that the message was not // delivered [X400]. message.Headers.Add("Sensitivity", "Company-confidential"); string sTime = System.DateTime.Now.AddDays(-1).ToString("dd MMM yyyy") + " " + System.DateTime.Now.ToShortTimeString() + " +0100"; // Set a message expiration date // When the expiration date passes, the message remains visible // in the message list with a strikethrough. // It can still be opened, but the strikethrough gives a visual clue // that the message is out of date or no longer relevant. message.Headers.Add("expiry-date", sTime); MailKit.DeliveryStatusNotification delivery = MailKit.DeliveryStatusNotification.Delay | MailKit.DeliveryStatusNotification.Failure | // MailKit.DeliveryStatusNotification.Never | MailKit.DeliveryStatusNotification.Success; message.Headers.Add( new MimeKit.Header(MimeKit.HeaderId.ReturnReceiptTo, "*****@*****.**") ); // Delivery report message.ReplyTo.Add(new MimeKit.MailboxAddress("Alice", "*****@*****.**")); message.Cc.Add(new MimeKit.MailboxAddress("Alice", "*****@*****.**")); message.Bcc.Add(new MimeKit.MailboxAddress("Alice", "*****@*****.**")); // message.Date = new System.DateTimeOffset(System.DateTime.Now); message.Date = System.DateTimeOffset.Now; //message.Attachments message.Importance = MimeKit.MessageImportance.High; message.Priority = MimeKit.MessagePriority.Urgent; message.XPriority = MimeKit.XMessagePriority.Highest; // message.HtmlBody // message.TextBody // message.Body // message.InReplyTo // message.MessageId // message.Sign(); // message.Verify() // message.SignAndEncrypt(); // Body (Mensagem) MimeKit.BodyBuilder bodyBuilder = new MimeKit.BodyBuilder(); bodyBuilder.Attachments.Add( new MimeKit.MimePart("image", "gif") { Content = new MimeKit.MimeContent( System.IO.File.OpenRead(path) , MimeKit.ContentEncoding.Default ) , ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment) , ContentTransferEncoding = MimeKit.ContentEncoding.Base64 , FileName = System.IO.Path.GetFileName(path) } ); // bodyBuilder.LinkedResources.Add("fn", (byte[]) null, new MimeKit.ContentType("text", "html")); MimeKit.MimeEntity image = bodyBuilder.LinkedResources.Add("selfie.jpg" , (byte[])null , new MimeKit.ContentType("image", "jpeg") ); image.ContentId = MimeKit.Utils.MimeUtils.GenerateMessageId(); bodyBuilder.TextBody = "This is some plain text"; // bodyBuilder.HtmlBody = "<b>This is some html text</b>"; bodyBuilder.HtmlBody = string.Format(@"<p>Hey Alice,<br> <p>What are you up to this weekend? Monica is throwing one of her parties on Saturday and I was hoping you could make it.<br> <p>Will you be my +1?<br> <p>-- Joey<br> <center><img src=""cid:{0}""></center>", image.ContentId); message.Subject = subject; message.Body = bodyBuilder.ToMessageBody(); // http://www.mimekit.net/docs/html/Creating-Messages.htm // create our message text, just like before (except don't set it as the message.Body) // MimeKit.MimePart body = new MimeKit.TextPart ("plain") { Text = plainText }; // MimeKit.MimePart body = new MimeKit.TextPart("html") { Text = "<b>Test Message</b>" }; // create an image attachment for the file located at path //MimeKit.MimePart attachment = new MimeKit.MimePart ("image", "gif") //{ // Content = new MimeKit.MimeContent( // System.IO.File.OpenRead(path) // , MimeKit.ContentEncoding.Default // ) // ,ContentDisposition = new MimeKit.ContentDisposition (MimeKit.ContentDisposition.Attachment) // ,ContentTransferEncoding = MimeKit.ContentEncoding.Base64 // ,FileName = System.IO.Path.GetFileName (path) //}; //// now create the multipart/mixed container to hold the message text and the //// image attachment //MimeKit.Multipart multipart = new MimeKit.Multipart ("mixed"); //multipart.Add (body); //multipart.Add (attachment); // now set the multipart/mixed as the message body // message.Body = multipart; // https://stackoverflow.com/questions/30507362/c-sharp-mailkit-delivery-status-notification // https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SmtpProcessReadReceipt // using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient()) using (MailKit.Net.Smtp.SmtpClient client = new SmtpClientWithStatusNotification()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(host, portNum, useSsl); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication client.Authenticate(userName, userPass); client.Send(message); client.Disconnect(true); } // End Using client }
static void AddBodyPart(MailMessage message, MimeKit.MimeEntity entity) { if (entity is MimeKit.MessagePart) { // FIXME: how should this be converted into a MailMessage? } else if (entity is MimeKit.Multipart) { var multipart = (MimeKit.Multipart)entity; if (multipart.ContentType.IsMimeType("multipart", "alternative")) { foreach (var part in multipart.OfType <MimeKit.MimePart>()) { // clone the content var content = new MemoryStream(); part.Content.DecodeTo(content); content.Position = 0; var view = new AlternateView(content, GetContentType(part.ContentType)); view.TransferEncoding = GetTransferEncoding(part.ContentTransferEncoding); if (!string.IsNullOrEmpty(part.ContentId)) { view.ContentId = part.ContentId; } message.AlternateViews.Add(view); } } else { foreach (var part in multipart) { AddBodyPart(message, part); } } } else { var part = (MimeKit.MimePart)entity; if (part.IsAttachment || !string.IsNullOrEmpty(message.Body) || !(part is MimeKit.TextPart)) { // clone the content var content = new MemoryStream(); part.Content.DecodeTo(content); content.Position = 0; var attachment = new Attachment(content, GetContentType(part.ContentType)); if (part.ContentDisposition != null) { attachment.ContentDisposition.DispositionType = part.ContentDisposition.Disposition; foreach (var param in part.ContentDisposition.Parameters) { attachment.ContentDisposition.Parameters.Add(param.Name, param.Value); } } attachment.TransferEncoding = GetTransferEncoding(part.ContentTransferEncoding); if (!string.IsNullOrEmpty(part.ContentId)) { attachment.ContentId = part.ContentId; } message.Attachments.Add(attachment); } else { message.IsBodyHtml = part.ContentType.IsMimeType("text", "html"); message.Body = ((MimeKit.TextPart)part).Text; } } }