Question help with sending email attachments from a sql db

jbungay

New member
Joined
Jan 28, 2011
Messages
2
Programming Experience
Beginner
hi I am trying to attach a PDF to a email message where the PDF is stored in a Sql table.
I have the following code be

i am trying to attach an PDF from a DB where that PDF is stored in SQL Database.

I am using the following code but nothing happens. I get no error, no email. It just to fail on the mailattachment call.

What is it that I am missing?


Dim attachmentebill As System.Net.Mail.Attachment

Try
attache = MailAttachment("filename.pdf", CType(emailOptionsRow.D_Attachment, Byte()))]If Not (attache Is Nothing) Then
email.Attachments.Add(attache)
End If
Catch ex As Exception
MsgBox(ex)
End Try

Public Function MailAttachment(ByVal AttachmentName As String, ByVal AttachmentByte As Byte()) As System.Net.Mail.Attachment

If AttachmentByte.Length <= 0 Then

Return Nothing

Else

Dim AttachmentStream As New System.IO.MemoryStream(AttachmentByte, 0, UBound(AttachmentByte))
Dim Attachment As New System.Net.Mail.Attachment(AttachmentStream, AttachmentName)
AttachmentStream.Close()
AttachmentStream.Dispose()

Return Attachment

End If
 
I am using the following code but nothing happens. I get no error, no email. It just to fail on the mailattachment call.
Very dim, especially since you haven't posted any code to send mail. Anyway, you can't close an attachment stream until the mail has been sent, SmtpClient will throw an exception about that.
 
i did think i had to show the sentemail code - SMTPServer.Send(email) - . Since it was dying before it reached that point. I add it the try catch block hoping to catch exceptions that occured. There were no exceptions thrown.

Now there very well could be something up the attachment stream. But i thought everything in the stream was contain in the attachment so it was safe to close it down. Anyway the code appears to dies on the function call itself.
 
But i thought everything in the stream was contain in the attachment so it was safe to close it down
No, the stream isn't read until it is time to actually send it. That something of a point of using a stream, since you normally don't have to read all its source content into memory.
I don't see any problems elsewhere, given that the source is a Byte().
 
Back
Top