Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel e-mail attachment corrupts

Status
Not open for further replies.

lmohr79

Technical User
Jan 18, 2005
39
US
I've got a multiple step process, end goal is to send an Excel file by e-mail using VB.NET.

Original data is in .txt report, so using Monarch V9 to create an Excel file from a summary.

Then want to create either a Windows app or service to e-mail the report.

I can get the file to e-mail just fine, but can't open from Outlook - file is corrupted and I get the "Windows has encountered an error".

Original file is OK.

Here's the code:

Imports System.web.mail
Imports System.IO
Imports System.IO.File

Private Sub btnSendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendEmail.Click
Dim email As New System.Web.Mail.MailMessage
Dim attach As MailAttachment
Dim strPath As String
Dim strTo As String = "john.doe@hotmail.com"
Dim strAttach As String

attach = New MailAttachment("H:\file.xls")
email.To = strTo
email.From = "DoNotReply@mail.com"

email.Subject = "test excel"
email.Attachments.Clear()
email.Attachments.Add(attach)
System.Web.Mail.SmtpMail.SmtpServer = "MailServer"
System.Web.Mail.SmtpMail.Send(email)

End Sub

I know I must be missing some type of encoding or formatting. Can you help? I can cut & paste from the MS Windows error if that will shed any light.

Thanks!
 
If you go look at the file on your H drive is it corrupt? There is nothing inherently in the mail procedure that would corrupt the file. No setting you have to set or code you have to type.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen - sorry it took so long to get back to you - just love back to back meetings!!!!!

I can open the file on the H drive normally - works just fine. I can also attach the file through Outlook, e-mail to myself and then open with no problem.

I get the same results (corrupt file) running (Debug) the code in Visual Studio or compiling the code and running the .exe on a different PC.

And if I have the code e-mailed to a different user, when they try to open the file on their PC, the file is corrupt.

In the "Microsoft Excel for Windows has encountered a problem" message, here's some of the data:

AppName: excel.exe
AppVer: 9.0.0.6627
ModName: mso9.dll
ModVer: 9.0.0.6925
Offset: 00010ea9

This probably won't help, but thought I would share.

Thanks!

 
Thought I should add - when trying to open the file with Excel and then get the error, the EXCEL.EXE process does stay open/active in Task Manager. No entry for Excel in Applications, just in Processes.

Thanks!
 
When you say 'multiple step process' do you mean that the file is being created in the program run as some kind of background job? how are you creating the file?

I had a problem with similar symptoms - programatically creating a pdf file with pdf995 then emailing as attachment - if the program doesnt wait until the process running in the background to finish the file that is emailed is in a corrupt state


(go to the bottom for the solution - i just wait for the file to be closed properly by the background process - might be irrelevant)
 
I've never had that problem, but I could see it happening. A question becomes how/when do you create your Excel file. Also though it may seem obvious you are sending through the same mail server as your Outlook uses?

Other than that if that is pretty much your exact email code then I'm not sure. Some shots in the dark. Something corrupt in VS. I've never dealt with encryption, but does any part of you mail process require encryption? Maybe something is going wrong with that. That makes me think anti-virus. If running one you might see if you can by pass it for a test send just in case.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Here's the process:

I create the file in Monarch V9.0, exporting a summary using the "Project Exports" module - this is a separate process from the VB.NET code.

The VB.NET code just generates the e-mail - so don't believe that a background process is creating the problem.

I'm going to have someone here create a similar .exe and try it - maybe I do have something corrupt in VS - just what I want to do during the holidays - rebuild my PC!

I'll post a reply after this test.

Thanks!
 
I had another user write (copied in) the code in a new project on a different PC, but the same results.

Anything I should look at in regards to References? I did have to add the System.Web reference - here are the references that are loaded:

Office
System
System.Data
System.Drawing
System.Web
System.Windows.Form
System.XML
VBIDE

I'm still learning the nuts & bolts of VB.NET, so this could be something very basic that I'm missing.

Thanks!
 
If other people can run it and it is still corrupt then it is either something about your mail server/Anti-Virus/etc. or it is happening in code you are not showing. There is nothing in the mail process itself that would do it. There has to be something common to both of you outside what we see. There is no "don't break" you need to type as part of the normal process. It shouldn't make any difference since it happened on both machines, but if you are using .net 2.0 or higher you can try using System.Net.Mail instead and see what happens. I can't see that would make a difference, but I can't see any reason for it to be corrupt either so it couldn't hurt.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for the reply Sorwen - there isn't any other code in my project - very stripped down as I'm troubleshooting.

I'll check on the mail server & AV angle. One thing I'll try is sending a different type of file with the code - that might narrow down the problem.

If I have any success, I'll post the results.

 
Found the answer!

I needed to add a header to the email in code, with base64 encoding:

email.Headers.Add("Content-Transfer-Encoding", "base64")

Works like a charm!

Thanks for all the imput!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top