I am creating an Excel worksheet dynamically in C# codebehind and i want that Excel worksheet to send as an email as an attachment. I dont want to save the worksheet. Currently am saving the worksheet, then attaching the saved worksheet, then send email, then delete the saved worksheet.
But i want to create dynamically Excel worksheet and then attach that worksheet without saving.
Is there any solution to this.
Thanks in Advance
Vishwas
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
crystal boyPosted Mar 13, 2013, 11:38 AM
It will replace the old file so need to worry about disk usage
Imports System.Net.Mail
Imports System.Net
Imports Outlook = Microsoft.Office.Interop.Outlook
Try
' Create the Outlook application.
Dim oApp As New Outlook.Application()
' Create a new mail item.
Dim oMsg As Outlook.MailItem = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
' Set HTMLBody.
'add the body of the email
oMsg.HTMLBody = "Hi Team,
Kindly Find the attachment and do the proceedings.
*** Please Donot reply to this mail ***
Thanks and Regards
Support "
'Add an attachment.
Dim sDisplayName As [String] = "MyAttachment"
Dim iPosition As Integer = CInt(oMsg.Body.Length) + 1
Dim iAttachType As Integer = CInt(Outlook.OlAttachmentType.olByValue)
'now attached the file
Dim oAttach As Outlook.Attachment = oMsg.Attachments.Add("C:\ss.xls", iAttachType, iPosition, sDisplayName)
'Subject line
oMsg.Subject = "Regarding Database Monitoring you have got mail"
' Add a recipient.
Dim oRecips As Outlook.Recipients = DirectCast(oMsg.Recipients, Outlook.Recipients)
' Change the recipient in the next line if necessary.
Dim oRecip As Outlook.Recipient = DirectCast(oRecips.Add("mailid 1"), Outlook.Recipient)
Dim oRecip1 As Outlook.Recipient = DirectCast(oRecips.Add("mailid 2"), Outlook.Recipient)
Dim oRecip2 As Outlook.Recipient = DirectCast(oRecips.Add("mailid 3"), Outlook.Recipient)
oRecip.Resolve()
oRecip1.Resolve()
oRecip2.Resolve()
' Send.
oMsg.Send()
' Clean up.
oRecip = Nothing
oRecip1 = Nothing
oRecip2 = Nothing
oRecips = Nothing
oMsg = Nothing
oApp = Nothing
'end of try block
Catch ex As Exception
End Try