System.Net.Mail.SmtpClient and lost attachments

E

Eric Guthmann

We're running into an intermittent problem with the
System.Net.Mail.SmtpClient class in the .NET 2.0 Framework when working with
PDF attachments (it may be any attachment, but we're working with PDF
files). About 10% of the emails sent via this class are missing their
attachment. We have verified everything on our side prior to the handoff to
the SmtpClient.Send method, and have even tried resending those emails that
are received with no attachment and it subsequently works fine.

Has anyone else seen this issue, and if so, is there a workaround?
Dim ms As New MemoryStream

'' ....
'' Generate PDF document into the memory stream
'' ....

ms.Seek(0, SeekOrigin.Begin)

Dim mailAttachment As New Attachment(ms, "Attachment.pdf",
"application/pdf")
Dim msg As New MailMessage

msg.From = New MailAddress(emailFromAddress, emailFromName)
msg.Subject = subject
msg.To.Add(toAddress)
msg.Bcc.Add(bccAddress)
msg.Attachments.Add(mailAttachment)

Dim smtp As New SmtpClient(emailSmtpServer, emailSmtpPort)

Try
smtp.Send(msg)
Finally
'' we have also tried without the following dispose calls, but this did
not change the result

msg.Dispose()
mailAttachment.Dispose()
End Try

Here are the specifics:

1. The code is in a Windows Service running under the Network Service
account
2. It doesn't look like it has anything to do with the actual attachment
because emails that are missing the attachment can be resent without problem
3. We're communicating with an Exchange 2000 server
4. We have added code to verify that the PDF was actually generated and is
well-formed. Everything looks good until the SmtpClient.Send call. It is
in this call that the attachment is sometimes lost.
5. The loss of attachments seem to occur most when the system is under some
load.
 
C

Christopher Reed

You might want to verify the attachment cutoff size for the mail server.
Your problem may actually be associated with these PDFs being too large.
 
E

Eric Guthmann

Hi Christopher,

Thanks for the reply. The actual attachments are well below the attachment
cutoff size (they are always between 50KB and 150KB in size). I also don't
think this can be the problem since a resend of the very same email and
attachment works just fine.

At this point, I think it must be an issue in the SmtpClient class itself
since everything is sent to the Send method in the same manner each time,
and we have verified with additional code that the attachment is there each
time before calling the Send method.

Thanks,
Eric
 
C

Christopher Reed

Then your issue with network load may be the issue. You may be sending some
of these on top of each other because the network is loaded and slower.
What you might consider is putting a time lag in your Windows Service app to
ensure that each individual email has been sent and given enough time to be
received by the mail server.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top