My friend recently ran into a strange requirement from his client.
There is a gird on the webpage and user want a button which will extract data from grid into the excel sheet and create an email attach excel sheet to that email and provides a pop up box that will open the email in outlook which user can edit , add email address and send it to any one he desire.
Following code spinet can be used for achieving it (with an exception that user will have to open and forward the email)
Add a ASP.Net button to the page and paste the code in click event.
Dim OutLookEmailDirectory As String = "d:\test\OutlookEmail\email\"
Dim DirName As String 'New System.Guid
'' getting a temp guid which will be used for creating a temp directory.
'' This temp directory is required as we don;t the name of the file with which the SMTP will save email to.
Dim g As New System.Guid
DirName = System.Guid.NewGuid.ToString.Replace("-", "_")
'Create the temp Directory
If Not System.IO.Directory.Exists(OutLookEmailDirectory & DirName) Then
System.IO.Directory.CreateDirectory(OutLookEmailDirectory & DirName)
End If
'Create Mail message object
Dim x As New System.Net.Mail.MailMessage
x.To.Add("youremail@domain.com")
x.Subject = "testing"
x.Body = "testing"
Dim a As New System.Net.Mail.MailAddress("youremail@domain.com")
x.From = a
''Specify the path of file.
''Please modify this to suit your needs
Dim y As New System.Net.Mail.Attachment("c:\outlooktest.xlsx")
''Add the attachment to the email
x.Attachments.Add(y)
'' Create email object and specify the deliver method as pickup directory
Dim m As New System.Net.Mail.SmtpClient
m.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory
m.PickupDirectoryLocation = OutLookEmailDirectory & DirName '"c:\email"
m.Send(x)
'' get the name of email file from the temp directory.
'' As this is a temp directory we have created, we will have only one file which can be
Dim objDir As New System.IO.DirectoryInfo(OutLookEmailDirectory & DirName)
Dim GeneratedEmailName As String = String.Empty
For Each Str As System.IO.FileInfo In objDir.GetFiles("*.eml")
GeneratedEmailName = Str.Name
Exit For
Next
''Move the email to the temp directory of your choice, (probably same where you have generated the attachments.
'' Moving to directory where clean up operation is done at later stage
If System.IO.File.Exists(OutLookEmailDirectory & DirName & "\" & GeneratedEmailName) Then
System.IO.File.Move(OutLookEmailDirectory & DirName & "\" & GeneratedEmailName, OutLookEmailDirectory & GeneratedEmailName)
'GeneratedEmailName = OutLookEmailDirectory & "\" & GeneratedEmailName
End If
'' Delete the previously crated temp directory
If System.IO.Directory.Exists(OutLookEmailDirectory & DirName) Then
System.IO.Directory.Delete(OutLookEmailDirectory & DirName)
End If
'' Show the pop up box to use in order to open the email in outlook.
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" & GeneratedEmailName)
Response.WriteFile(OutLookEmailDirectory & GeneratedEmailName)
Response.[End]()
Dim DirName As String 'New System.Guid
'' getting a temp guid which will be used for creating a temp directory.
'' This temp directory is required as we don;t the name of the file with which the SMTP will save email to.
Dim g As New System.Guid
DirName = System.Guid.NewGuid.ToString.Replace("-", "_")
'Create the temp Directory
If Not System.IO.Directory.Exists("d:\test\OutlookEmail\email\" & DirName) Then
System.IO.Directory.CreateDirectory("d:\test\OutlookEmail\email\" & DirName)
End If
'Create Mail message object
Dim x As New System.Net.Mail.MailMessage
x.To.Add("deepti.pare@gmail.com")
x.Subject = "testing"
x.Body = "testing"
Dim a As New System.Net.Mail.MailAddress("deepti.pare@gmail.com")
x.From = a
''Specify the path of file.
''Please modify this to suit your needs
Dim y As New System.Net.Mail.Attachment("c:\outlooktest.xlsx")
''Add the attachment to the email
x.Attachments.Add(y)
'' Create email object and specify the deliver method as pickup directory
Dim m As New System.Net.Mail.SmtpClient
m.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory
m.PickupDirectoryLocation = "d:\test\OutlookEmail\email\" & DirName '"c:\email"
m.Send(x)
'' get the name of email file from the temp directory.
'' As this is a temp directory we have created, we will have only one file which can be
Dim objDir As New System.IO.DirectoryInfo("d:\test\OutlookEmail\email\" & DirName)
Dim GeneratedEmailName As String = String.Empty
For Each Str As System.IO.FileInfo In objDir.GetFiles("*.eml")
GeneratedEmailName = Str.Name
Exit For
Next
''Move the email to the temp directory of your choice, (probably same where you have generated the attachments.
'' Moving to directory where clean up operation is done at later stage
If System.IO.File.Exists("d:\test\OutlookEmail\email\" & DirName & "\" & GeneratedEmailName) Then
System.IO.File.Move("d:\test\OutlookEmail\email\" & DirName & "\" & GeneratedEmailName, "d:\test\OutlookEmail\email\" & GeneratedEmailName)
'GeneratedEmailName = "d:\test\OutlookEmail\email\" & "\" & GeneratedEmailName
End If
'' Delete the previously crated temp directory
If System.IO.Directory.Exists("d:\test\OutlookEmail\email\" & DirName) Then
System.IO.Directory.Delete("d:\test\OutlookEmail\email\" & DirName)
End If
'' Show the pop up box to use in order to open the email in outlook.
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" & GeneratedEmailName)
Response.WriteFile("d:\test\OutlookEmail\email\" & GeneratedEmailName)
Response.[End]()