MailMessageAppendChunk Method |
Namespace: MailBee.Mime
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | nextChunk is a null reference (Nothing in Visual Basic). |
This method can be used to assemble a message from smaller memory blocks (for instance, if the message is being read from the resource which cannot return all the data at once). However, since this method recreates the internal message data buffer on each call, it should be used only if the size of the data is not known until all the data received. If the size of the data is known from the very beginning, it's more effective to create a memory array of the required length, fill it with the data and then pass this array to LoadMessage(Byte) method. Or, if you're using streams, use LoadMessage(Stream) overload.
To assembly a message which naturally comes as a series of smaller e-mail messages (also called partial messages), use AppendPartialMessage(MailMessage) method.
Note |
---|
In real world applications, it's easier to load a message from a file/stream/memory using LoadMessage(String) method and its overloads. Also, real world apps shouldn't rely on the fact that FileStream.Read method of .NET framework always returns the requested number of bytes. They should rather check its return value and repeat reading attempt if the number of bytes read was less than requested. The code snippet in FileStream.Read topic in MSDN shows how this can be done. |
using System; using System.IO; using MailBee; using MailBee.Mime; class Sample { static void Main(string[] args) { MailMessage msg = new MailMessage(); string filename = @"C:\Docs\TestMail.eml"; // Open the file for reading. using (FileStream fs = new FileStream(filename, FileMode.Open)) { // Create a 100 bytes buffer. byte[] bytes = new byte[100]; for (int i = 0; i < 10; i++) { // Read a portion of data (100 bytes) from the file and append it to the message source. fs.Read(bytes, i * bytes.Length, bytes.Length); msg.AppendChunk(bytes); } } // The message will be parsed at this point (when we access any of its properties). Console.WriteLine(msg.Subject); } }