Custom Search
Logiclabz
  • Home
  • Asp.Net
  • Asp.Net Email Attachment on Fly using UTF32Encoding Class and MemoryStream Class

Asp.Net Email Attachment on Fly using UTF32Encoding Class and MemoryStream Class

  

Asp.Net Email Attachment on Fly using UTF32Encoding Class & MemoryStream Class

UTF32Encoding Class in System.Text namespace represents a UTF-32 encoding of Unicode characters. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. GetBytes method performs the actual encoding. GetBytes method encodes all the characters in the specified String into a sequence of bytes.

The encoded bytes from GetBytes method of UTF32Encoding Class is used in creating MemoryStream.

MemoryStream Class in System.IO namespace creates a stream whose backing store is memory. The MemoryStream class creates streams that have memory as a backing store instead of a disk. MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application.

Attachment Class in System.Net.Mail namespace represents an attachment to an e-mail. Attachment content can be a String, Stream, or file name. In this case its Stream.

Namespace used:

	System.Text
	System.IO
	System.Net.Mail
[Visual Basic]
'Dim stream As New MemoryStream(UTF32Encoding.Default.GetBytes(data))

Private Sub AddAttachmentFromStream(ByVal message As _
MailMessage,ByVal data As String,ByVal attachmentName As String)

Dim stream As New MemoryStream(UTF32Encoding.Default.GetBytes(data))

' Rewind the stream.
stream.Position = 0

' Create a new attachment, and
' add the attachment to the supplied
' message.
Dim att As New Attachment(stream, attachmentName)
 message.Attachments.Add(att)

End Sub

[C#]

//MemoryStream stream = new MemoryStream(UTF32Encoding.Default.GetBytes(data));

private void AddAttachmentFromStream(MailMessage message, String data, String attachmentName)
{
	MemoryStream stream = new MemoryStream(UTF32Encoding.Default.GetBytes(data));
	// Rewind the stream.
		stream.Position = 0;
	
	// Create a new attachment, and
	// add the attachment to the supplied
	// message.
	Attachment att = new Attachment(stream, attachmentName);
	message.Attachments.Add(att);
}



  


Leave a reply




Do you like this post?