473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Corrupt attachments using MailMessage.Att achments.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.Applicati on component. The Excel workbooks/documents
are stored locally before I call my SendExcelDocume nt()
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
SendExcelDocume nt() method, as you can see from the code
snippet below.

public void SendExcelDocume nt(string smtpServerId, string
from, string to, string subject)
{

if(this.Documen tPath.Length > 0)
{
System.IO.FileI nfo fileinfo = new
System.IO.FileI nfo(this.Docume ntPath);

if(fileinfo.Exi sts)
{
System.Web.Mail .MailMessage message = new
System.Web.Mail .MailMessage();
message.Attachm ents.Add(new
System.Web.Mail .MailAttachmen( this.DocumentPa th));
message.From = from;
message.To = to;
message.Subject = subject;

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

}
else
{
throw new
Scandpower.NP.E xception.NPExce ption(this.Form ular + " 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.At tachments.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 8234
I had the same issue and it seemed to resolve itself when I qualified
the attachment with the FullName.ToStri ng() attribute of the FileInfo
object.

Since you already instantiated fileinfo, try changing:
message.Attachm ents.Add(new
System.Web.Mail .MailAttachmen( this.DocumentPa th));

to:
message.Attachm ents.Add(new
System.Web.Mail .MailAttachmen( fileinfo.FullNa me.ToString())) ;

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.Applicati on component. The Excel workbooks/documents
are stored locally before I call my SendExcelDocume nt()
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
SendExcelDocume nt() method, as you can see from the code
snippet below.

public void SendExcelDocume nt(string smtpServerId, string
from, string to, string subject)
{

if(this.Documen tPath.Length > 0)
{
System.IO.FileI nfo fileinfo = new
System.IO.FileI nfo(this.Docume ntPath);

if(fileinfo.Exi sts)
{
System.Web.Mail .MailMessage message = new
System.Web.Mail .MailMessage();
message.Attachm ents.Add(new
System.Web.Mail .MailAttachmen( this.DocumentPa th));
message.From = from;
message.To = to;
message.Subject = subject;

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

}
else
{
throw new
Scandpower.NP.E xception.NPExce ption(this.Form ular + " 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.At tachments.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
1817
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 American english ) then the fullstop is removed. This is an example of how I am using SmtpMail and MailMessage: MailMessage mail = new MailMessage(); mail.From = "from@test.com"; mail.To ="to@test.com";
0
987
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 MailMessage Message = new MailMessage();
2
1058
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 with their info.
2
1500
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 any coding samples on how to programatically add mail attachments using .net
2
4532
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 from using system.web.mail to system.net.mail (of course vs is telling me system.web.mail is obsolete). In .Net 1.1, the files deleted with no problem but in .Net 2 when I attempt to delete I receive The process cannot access the file ...'...
8
1847
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 dim statements to work. It says it needs to be declared and I have used the reference 0utlook 11.0. Also, I used Imports Microsoft.Office.Interop.Outlook. Any ideas?
0
3776
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, I do not know what should it be. If I Provide '0', it is throwing out error. Can any one help what shoudl I do to fix this. I am able to send E-mail sucessfully without Attachments.. Code (Please refer comments near Mailmessage.12) OPTIONS...
2
5265
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 problem is that everytime the PDF size is equal to or less than 200 KB, the email is just not being sent. There are no error messages, nothing. Here is a piece of code.
1
3876
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. This works great with one attachment. I am requiring that a file be attach before submitting. Now I am trying to add the ability to add multiple attachments and I am able to create this however, it will error out if all of the attachment is not...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.