PDF gets corrupted when sent as an email attachment

  • Thread starter Thread starter Jim Covington
  • Start date Start date
J

Jim Covington

I have an app that creates customer information and creates a pdf
report. It ecrypts it and password protects it. The report is sent as
an email attachment. THe report is confidetial.

All of the pdfs open fine on local machine but customer gets errors when
opened. This is documented at
http://www.adobe.com/support/techdocs/322743.html

Attaching a pdf in an email somehow corrupts it. Is there another way a
confidential report can be generated, encrypted and password protected
other than pdf?





jwc
 
Jim said:
I have an app that creates customer information and creates a pdf
report. It ecrypts it and password protects it. The report is sent as
an email attachment. THe report is confidetial.

All of the pdfs open fine on local machine but customer gets errors when
opened. This is documented at
http://www.adobe.com/support/techdocs/322743.html

Attaching a pdf in an email somehow corrupts it.

Well, if it does in your case, I'm sure this is the problem you should try
to solve. I have sent PDF files by email very many times without problems
- I'm pretty sure every mail program out there knows how to do that
correctly, because it's such a widely used feature.

The article you are referring actually gives a hint - the encoding of the
file when sent by mail may not be correct. I reckon if you were sending
the PDF file in some kind of text encoding (as opposed to a binary
encoding), this could certainly be the cause of the damage. You don't
mention how the "sending as email attachment" is actually done. Do you do
it from code? If so, show us the code, please!


Oliver Sturm
 
Private Sub btPDF_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'create & instantiate mail object
Try
SmtpMail.SmtpServer = "Exchange"
Dim _mail As New MailMessage
Dim _att As New MailAttachment(Application.StartupPath & "\"
& Trim(AchInfoRow.Item("TrsNme")) & ".zi3")
'attach to, subject, attachment & body
_mail.Attachments.Add(_att)
_mail.From = "(e-mail address removed)"
'_mail.To = AchInfoRow.Item("EmailAddress")
'_mail.To = "(e-mail address removed)"
_mail.To = "(e-mail address removed)"
'_mail.To = "(e-mail address removed)"
_mail.Subject = "Your Report of Transactions"
_mail.Cc = "(e-mail address removed)"

' set the default smtp server & send it
SmtpMail.Send(_mail)
MessageBox.Show("eMail to " &
Trim(AchInfoRow.Item("trName")) & " successful.")
Catch Exp As Exception
MessageBox.Show(Exp.Message)
MessageBox.Show(Exp.ToString)
Finally
cn.Close()
End Try
End Sub

jwc
 
Jim Bob wrote:

<code cut>

Okay, this shows the problem. I have just made some tests with .NET 1.1
and it seems that the attachment functionality is a bit buggy there (don't
know about v2, I haven't tested it there yet). The problem in your sample
code is that you don't have any body text at all. This results in mail
messages that don't use any encoding for the PDF attachment at all, but
just use the application/pdf type for the whole message - just insert
something in the message's body (maybe a string like "here's your
document") and things should work fine for you.

The other funny thing I noticed is that the encoding that I set for the
attachment doesn't seem to be honored, whatever I do. MSDN docs say that
UUEncode should be default - nevertheless, once you get message to use an
encoding by having something in the body, it looks like Base64 is always
being used. Funny... just a side note. I'd expect this to work better in
..NET 2.



Oliver Sturm
 
Private Sub btPDF_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'create & instantiate mail object
Try
SmtpMail.SmtpServer = "Exchange"
Dim _mail As New MailMessage
Dim _att As New MailAttachment(Application.StartupPath & "\"
& Trim(AchInfoRow.Item("TrsNme")) & ".zi3")
'attach to, subject, attachment & body
_mail.Attachments.Add(_att)
_mail.From = "(e-mail address removed)"
'_mail.To = AchInfoRow.Item("EmailAddress")
'_mail.To = "(e-mail address removed)"
_mail.To = "(e-mail address removed)"
'_mail.To = "(e-mail address removed)"
_mail.Subject = "Your Report of Transactions"
_mail.Cc = "(e-mail address removed)"

' set the default smtp server & send it
SmtpMail.Send(_mail)
MessageBox.Show("eMail to " &
Trim(AchInfoRow.Item("trName")) & " successful.")
Catch Exp As Exception
MessageBox.Show(Exp.Message)
MessageBox.Show(Exp.ToString)
Finally
cn.Close()
End Try
End Sub

jwc
 
Oliver:

I did as you suggested but the problem remains. Mostly I get an
"Unregognized token" error. Is there anything else I can try? Thanks.


jwc
 
Jim said:
I did as you suggested but the problem remains. Mostly I get an
"Unregognized token" error. Is there anything else I can try?

Hm, that's interesting - I had no problems once I inserted some body text.
You could try using the two different encodings that are available,
although that didn't seem to make any difference in my tests. Just pass
the second parameter to the constructor of the MailAttachment class.

You are using .NET 1.1, aren't you? Not 1.0, by any chance?


Oliver Sturm
 
Oliver:

Thank-you for your assistance. I changed the encoding to
uuEncode and it appears now to be working.

I am on v1.1 of the Framework.


jwc
 
Hi Jim,

I have previously looked into creating PDF programatically with password
protection and was lead to believe it could not be done. Do you have any
sources for password protecting pdf files in c#?

Thanks
Gav
 
Back
Top