By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

Corrupt attachments using MailMessage.Attachments.Add()

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.