T
Trond A. S. Andersen
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
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