Email attachment gets corrupted when sending using SMTP server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a PDF file that I am trying to send as an attachment through a C#
program. Even though the PDF file can be opened by itself, sometimes the
same
file cannot be opened as an attachment. If I try and open the original PDF
file I have no problems.

I have used System.Web.Mail.MailMessage to create an
email and an SMTPMail object to send the mail.
I tried to change the encoding to Base64 but even that has not helped. There
are still ocassions when I am unable to open the attachment.

Has anybody experienced this problem or do you have any suggestions .

Thanks!!
 
New User said:
I have a PDF file that I am trying to send as an attachment through a C#
program. Even though the PDF file can be opened by itself, sometimes the
same
file cannot be opened as an attachment. If I try and open the original PDF
file I have no problems.

I have used System.Web.Mail.MailMessage to create an
email and an SMTPMail object to send the mail.
I tried to change the encoding to Base64 but even that has not helped. There
are still ocassions when I am unable to open the attachment.

Has anybody experienced this problem or do you have any suggestions .

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
New said:
Hi,

I have a PDF file that I am trying to send as an attachment through a C#
program. Even though the PDF file can be opened by itself, sometimes the
same
file cannot be opened as an attachment. If I try and open the original PDF
file I have no problems.

I have used System.Web.Mail.MailMessage to create an
email and an SMTPMail object to send the mail.
I tried to change the encoding to Base64 but even that has not helped. There
are still ocassions when I am unable to open the attachment.

Has anybody experienced this problem or do you have any suggestions .

Thanks!!


In my experience, sometimes the "System.Web.Mail" will use a incorrect
attachment encoding even you had set it to "Base64". It seems the
problem is related to some version of "CDO" object installed in the
system by other application(e.g. MS office).


I solve the problem to force the encoding by changing the mail header.


MailMessage message = new MailMessage();
....
MailAttachment attach;
attach = new MailAttachment(file,MailEncoding.Base64);
message.Attachments.Add(attach);
....
//-> add this line
message.Headers.Add("Content-Transfer-Encoding","base64");
....
 
Back
Top