Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Send and save the mail to a DocumentSet


Topic Send and save the mail to a DocumentSet
OutputThe dedicated functions
RefFunction based on this post

Step Description
1 Create the SaveToDocumentSet function
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
 
 public static void SaveToDocumentSet(this MailMessage Message, DocumentSet targetDocumentSet, string nameWithoutExtension, bool overwrite = false)
        {
            if (targetDocumentSet == null) throw new ArgumentNullException("targetDocumentSet");
            if (nameWithoutExtension == null) throw new ArgumentNullException("nameWithoutExtension");
            if (string.IsNullOrWhiteSpace(nameWithoutExtension)) throw new ArgumentException("Argument cannot be empty", "nameWithoutExtension");

            Assembly assembly = typeof(SmtpClient).Assembly;
            Type _mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");

            using (MemoryStream ms = new MemoryStream())
            {
                // Get reflection info for MailWriter contructor
                ConstructorInfo _mailWriterContructor = _mailWriterType.GetConstructor(
                        BindingFlags.Instance | BindingFlags.NonPublic,
                        null,
                        new Type[] { typeof(Stream) },
                        null);

                // Construct MailWriter object with our MemoryStream
                object _mailWriter =
                  _mailWriterContructor.Invoke(new object[] { ms });

                // Get reflection info for Send() internal method on MailMessage
                MethodInfo _sendMethod = typeof(MailMessage).GetMethod(
                        "Send",
                        BindingFlags.Instance | BindingFlags.NonPublic);

                // Call method passing in MailWriter
                _sendMethod.Invoke(
                    Message,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { _mailWriter, true, true },
                    null);
    // Save stream to DocumentSet 
                string fileUrl = string.Format("{0}/{1}.eml", targetDocumentSet.Folder.Url, nameWithoutExtension);
                targetDocumentSet.Folder.Files.Add(fileUrl, ms, overwrite);
                
    // Finally get reflection info for Close() method on our MailWriter
                MethodInfo _closeMethod = _mailWriter.GetType().GetMethod(
                        "Close",
                        BindingFlags.Instance | BindingFlags.NonPublic);

                // Call close method
                _closeMethod.Invoke(
                    _mailWriter,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { },
                    null);


            }
        }

No comments:

Post a Comment

by Category