Domanda

Ho ottenuto un oggetto MailMessage System.Net.mail e vuoi salvarlo su un documentazione dopo l'invio. Questo oggetto contiene mittente, a, cc, corpo html e allegati.

Come posso salvarlo in un documentazione?

Ho trovato alcuni modi per usare "Salva" -Methodes, ma tutti supportano il percorso locale come questo su codeplex .

Alla fine voglio solo un .eml o .msg nella mia cartella.

Grazie alla tua risposta ho ottenuto questa fini di lavoro:

MemoryStream stream = new MemoryStream();

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

ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
object mailWriter = mailWriterContructor.Invoke(new object[] { stream });
MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
sendMethod.Invoke(msg, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true }, null);

string emailTitle = string.Concat(documentSet.Folder.ServerRelativeUrl, name, ".eml");

stream.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);

SPFile file = documentSet.Folder.Files.Add(emailTitle, data, true);
file.Update();
.

È stato utile?

Soluzione

Judging from the link you reported, the Save method calls a FileStream object. Being a stream, the FileStream can be replaced by a MemoryStream.

// Create MemoryStream object
MemoryStream stream = new MemoryStream();

// Get reflection info for MailWriter contructor
// [omitted for brevity]

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

Then, it is just a matter of saving this stream to your document set, like this:

SPFolder ds = web.GetFolder("http://server/documentsetname");
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length); //make sure this is reliable, otherwise find other ways to convert to a byte array
ds.Files.Add("http://server/shared%20documents/documentsetname/your.msg", data);
ds.Update();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top