Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

ASP.NET with 2.0/3.5, Silverlight 2/3/4, WPF, WCF, nHibernet, Javascript, Jquery, Ajax, C#-2.0/3.0, Sql server 2005/2008, MySql 5.1., RIA WCF Service, Entity Framework.
Browse by Tags · View All
.Net 8
SQL 5
Sql Server 3
T SQL 3
linq 3
ORM 2
ORM 2
Browser 2
Silverlight 2
C# 2

Archive · View All
December 2011 9
January 2012 4
July 2012 3
October 2012 1
September 2012 1
August 2012 1
May 2012 1
April 2012 1
March 2012 1
February 2012 1

Send Email Attachment Using A Memory Stream in .Net

Dec 23 2011 11:56AM by Viral Sarvaiya   

Mainly we send attachment as a file which is already in the server and take that physical file, but from memory stream we can also send as a attachment in email, below is code for that,

Dim strMailServer As String = "SMTPServerName"
Dim fs As New FileStream("FilePath\FileName.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sReader As New StreamReader(fs)
Dim objMemoryStream As New MemoryStream()
Dim sb As New System.Text.StringBuilder("")
Dim str As String

'--read through template form, replace variables and add lines to string builder

Do While sReader.Peek() >= 0
    str = sReader.ReadLine()
    '--replace [Date_Time]
    'Replace string here
    sb.Append(str)
Loop

Dim Encoding As New UTF8Encoding
Dim arrByt() As Byte = Encoding.GetBytes(sb.ToString())
objMemoryStream.Write(arrByt, 0, arrByt.Length)
objMemoryStream.Position = 0

'--release file system resources
sReader.Close()
sReader.Dispose()
fs.Close()
fs.Dispose()

Dim objMailMessage As New MailMessage    
Dim objSMTP As New SmtpClient
Dim toAddress As New MailAddress("ToEmailAddress", "ToEmailName")

objMailMessage.To.Add(toAddress)
Dim fromAddress As New MailAddress("FromEmailAddress", "FromEmailName")
objMailMessage.From = fromAddress
objMailMessage.IsBodyHtml = False
objMailMessage.Priority = MailPriority.Normal    
objMailMessage.Subject = "EmailSubject"
objMailMessage.Body = "Email Body"

' add fax cover page as first file attachment    
objMailMessage.Attachments.Add(New Attachment(objMemoryStream, "FileName.txt"))

Try
    objSMTP.Host = strMailServer
    objSMTP.Send(objMailMessage)
Catch ex As Exception
    Throw ex
End Try

Like this we are not storing any file in server and send mail as a txt file as a attachment. like this we are also saving server space also...

Enjoy....

Jay Ganesh.

Tags: attachment, Attachment using Memory Stream, Memory Stream, Send Email, Send Email Attachment using a Memory Stream, Send Email using C#, .Net


Viral Sarvaiya
175 · 1% · 279
2
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Send Email Attachment Using A Memory Stream in .Net" rated 5 out of 5 by 2 readers
Send Email Attachment Using A Memory Stream in .Net , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]