473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Corrupt attachments using MailMessage.Attachments.Add()

Hi all,

I'm working on a project in which i need to send mail
attachments in the form of Excel documents from a .NET C#
application.
I've written a class which creates the Excel document
using the automation features provided by the
Excel.Application component. The Excel workbooks/documents
are stored locally before I call my SendExcelDocument()
method in order to attatch each document to an instance of
the System.Web.Mail.MailMessage class, and send the
message using System.Web.Mail.SmtpMail. The path of the
file representing the Excel document are stored in the
DocumentPath property of the class implementing the
SendExcelDocument() method, as you can see from the code
snippet below.

public void SendExcelDocument(string smtpServerId, string
from, string to, string subject)
{

if(this.DocumentPath.Length > 0)
{
System.IO.FileInfo fileinfo = new
System.IO.FileInfo(this.DocumentPath);

if(fileinfo.Exists)
{
System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
message.Attachments.Add(new
System.Web.Mail.MailAttachmen(this.DocumentPath));
message.From = from;
message.To = to;
message.Subject = subject;

System.Web.Mail.SmtpMail.SmtpServer =
smtpServerId;
System.Web.Mail.SmtpMail.Send(message);

}
else
{
throw new
Scandpower.NP.Exception.NPException(this.Formular + " do
not exist.");
}
}
}

The transmission of the mail massages are seemingly
running quite smoothly, but any attempt to open the Excel
attacments from MS Outlook as the messages arrive at the
receiving end, results in an Excel error message stating
that the "document is corrupt and cannot be opened". My
local version of the same file, however, can be opened by
Excel without any problems. I've also noticed that the
size of the Excel documents attatched to the mail messages
differ from the size of the the original files I've stored
locally before attaching them to mail messages. I've done
some experimenting on the overload of the
System.Web.Mail.MailMessage.Attachments.Add() method,
providing different System.Web.Mail.MailEncoding
parameters, but this effort does not seem to get my
anywhere. If any of you guys have an idea what's causing
this problem, and maybe even a suggestion for a
workaround, your advice would be highly appreciated.

Regards, Trond
Nov 15 '05 #1
1 8165
I had the same issue and it seemed to resolve itself when I qualified
the attachment with the FullName.ToString() attribute of the FileInfo
object.

Since you already instantiated fileinfo, try changing:
message.Attachments.Add(new
System.Web.Mail.MailAttachmen(this.DocumentPath));

to:
message.Attachments.Add(new
System.Web.Mail.MailAttachmen(fileinfo.FullName.To String()));

Hope it works for you,
CoDr

Trond A. S. Andersen wrote:
Hi all,

I'm working on a project in which i need to send mail
attachments in the form of Excel documents from a .NET C#
application.
I've written a class which creates the Excel document
using the automation features provided by the
Excel.Application component. The Excel workbooks/documents
are stored locally before I call my SendExcelDocument()
method in order to attatch each document to an instance of
the System.Web.Mail.MailMessage class, and send the
message using System.Web.Mail.SmtpMail. The path of the
file representing the Excel document are stored in the
DocumentPath property of the class implementing the
SendExcelDocument() method, as you can see from the code
snippet below.

public void SendExcelDocument(string smtpServerId, string
from, string to, string subject)
{

if(this.DocumentPath.Length > 0)
{
System.IO.FileInfo fileinfo = new
System.IO.FileInfo(this.DocumentPath);

if(fileinfo.Exists)
{
System.Web.Mail.MailMessage message = new
System.Web.Mail.MailMessage();
message.Attachments.Add(new
System.Web.Mail.MailAttachmen(this.DocumentPath));
message.From = from;
message.To = to;
message.Subject = subject;

System.Web.Mail.SmtpMail.SmtpServer =
smtpServerId;
System.Web.Mail.SmtpMail.Send(message);

}
else
{
throw new
Scandpower.NP.Exception.NPException(this.Formular + " do
not exist.");
}
}
}

The transmission of the mail massages are seemingly
running quite smoothly, but any attempt to open the Excel
attacments from MS Outlook as the messages arrive at the
receiving end, results in an Excel error message stating
that the "document is corrupt and cannot be opened". My
local version of the same file, however, can be opened by
Excel without any problems. I've also noticed that the
size of the Excel documents attatched to the mail messages
differ from the size of the the original files I've stored
locally before attaching them to mail messages. I've done
some experimenting on the overload of the
System.Web.Mail.MailMessage.Attachments.Add() method,
providing different System.Web.Mail.MailEncoding
parameters, but this effort does not seem to get my
anywhere. If any of you guys have an idea what's causing
this problem, and maybe even a suggestion for a
workaround, your advice would be highly appreciated.

Regards, Trond


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Knox | last post by:
Hi I have come across an apparant bug while using SmtpMail. The problem I have is that if a MailAttachment has a line in it that starts with a fullstop ( or period for those of you that speak...
0
by: Alejandro Penate-Diaz | last post by:
hi. I am sending attachments and for some reason the files are getting corrupted and a different size than the original file. I am sending word and excel documents. below is the code I am using ...
2
by: VB Programmer | last post by:
I know how to send emails using ASP.NET. How can I allow the user to attach a file to an email that they submit (via an ASPX online submission form)? Ex: Allow user to submit a resume along...
2
by: larry mckay | last post by:
I'm trying to send mail using the mail.smptserver, mailmessage, mailattachment objects I'm having trouble assigning an attachment to before doing an smpt.send using vb.net does anyone have...
2
by: Brad | last post by:
I have a web .Net app which sends emails with attachments. After the email is sent I clean up aftermyself and delete the attachments from disk. In upgrading to .Net 2 I changed the email logic...
8
by: Mark | last post by:
Using VB.net 2005, I am trying to send an email with attachments in Outlook. I have tried using this article from Microsoft, (http://support.microsoft.com/?kbid=313803), but I can't even get the...
0
by: bambi | last post by:
I am facing issues while adding attachments in an E-mail via Mapisend. The code I use is below. If I try to send , I am getting error messgae in Mailmessage.12. Mailmessage.12 is expected as numeric,...
2
by: Robson Siqueira | last post by:
Folks, I've been experiencing a weird problem. I have a web service which sends an email after its processing. The email always has an PDF attachment on it. The PDF size vary overtime. The...
1
by: budyerr | last post by:
All, I am trying to build a email submission form using asp.net. I currently have a web form page that will upload to my webhosting server, attach to email then delete the file after sending. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.