Setting and getting the message parts

Top  Previous  Next

To set or get a part of a MimeMessage invoke the AddPart and GetPart methods respectively. Adding a part to a multipart MimeMessage generates a boundary string. Also, a Content-Type field is set for this MimeMessage to multipart/mixed to reflect multipart nature of the message.

 

Example

This example creates a base message, reads the contents of a text file to add as a part to the base message, then displays the content of the message part. It is assumed that the filename value is a valid text file.

 

[C#]

// create main message

MimeMessage message = new MimeMessage();

// read file data for new message part

byte[] data = null;

StreamReader sr = File.OpenText(filename);

data = Encoding.Default.GetBytes(sr.ReadToEnd());

sr.Close();

// create and add part to message

message.AddPart(new MimeMessage(data));

// get message part

MimeMessage messagePart = message.GetPart(0);

// display body of message

Console.WriteLine(messagePart.GetBody());

 

[Visual Basic]

Dim message As MimeMessage = Nothing

' read file data for new message part

Dim sr As StreamReader = File.OpenText(filename)

Dim data As Byte() = Encoding.Default.GetBytes(sr.ReadToEnd())

sr.Close()

' create and add part to message

message.AddPart(New MimeMessage(data));

' get message part

Dim messagePart As MimeMessage = message.GetPart(0)

' display body of message

Console.WriteLine(messagePart.GetBody())

 

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC